Skip to content

Commit

Permalink
feat: create data view command
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Mar 31, 2024
1 parent d5a097c commit b074d61
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
13 changes: 3 additions & 10 deletions src/commands/info/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Category, PermissionGuard } from "@discordx/utilities";
import { ApplicationCommandOptionType, type CommandInteraction } from "discord.js";

import { GuildData } from "../../data.js";
import { RequirePermissions } from "../../utility.js";
import Embed from "../../embed-presets.js";

@Discord()
Expand All @@ -14,11 +15,7 @@ import Embed from "../../embed-presets.js";
export class Tags {
@Slash({ description: "Create a new tag" })
@SlashGroup("tags")
@Guard(
PermissionGuard([ "ManageGuild" ], {
embeds: [Embed.noPermissions()]
})
)
@Guard(RequirePermissions(["ManageGuild"]))
public async create(
@SlashOption({
description: "The name of the tag. Will be used to fetch the tag",
Expand Down Expand Up @@ -46,11 +43,7 @@ export class Tags {

@Slash({ description: "Delete an existing tag" })
@SlashGroup("tags")
@Guard(
PermissionGuard([ "ManageGuild" ], {
embeds: [Embed.noPermissions()]
})
)
@Guard(RequirePermissions(["ManageGuild"]))
public async delete(
@SlashOption({
description: "The name of the tag to delete",
Expand Down
45 changes: 45 additions & 0 deletions src/commands/meta/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Discord, Guard, Slash, SlashGroup, SlashOption } from "discordx";
import { Category, PermissionGuard } from "@discordx/utilities";
import { ApplicationCommandOptionType, type CommandInteraction } from "discord.js";

import { Firebase } from "../../firebase.js";
import { RequirePermissions } from "../../utility.js";
import Embed from "../../embed-presets.js";

@Discord()
@Category("Meta")
@Guard(RequirePermissions(["Administrator"]))
@SlashGroup({
name: "data",
description: "Commands to look at or modify data (soon)"
})
export class Data {
@Slash({ description: "View the data at `path`" })
@SlashGroup("data")
public async view(
@SlashOption({
description: "The path of the data",
name: "path",
required: true,
type: ApplicationCommandOptionType.String,
minLength: 3,
})
path: string,
command: CommandInteraction
): Promise<void> {
if (!command.channel) return;

const pathParts = path.split("/");
const root = pathParts.shift()!;
const db = new Firebase(root, process.env.FIREBASE_URL!);
const result = await db.get(pathParts.join("/"));
if (result === undefined)
return void await command.reply({
embeds: [Embed.error(`There is no data at path \`${path}\`.`)]
});

await command.reply({
embeds: [Embed.success(`\`\`\`json\n${JSON.stringify(result)}\`\`\``)]
});
}
}
6 changes: 3 additions & 3 deletions src/embed-presets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBuilder } from "discord.js";
import { EmbedBuilder, type PermissionsString } from "discord.js";

export default class Embed {
public static success(message: string): EmbedBuilder {
Expand All @@ -7,8 +7,8 @@ export default class Embed {
.setDescription(message);
}

public static noPermissions(): EmbedBuilder {
return this.error("You do not have permissions to use this command!");
public static noPermissions(requiredPermissions: PermissionsString[]): EmbedBuilder {
return this.error(`You do not have permissions to use this command!\n${requiredPermissions.join(", ")}`);
}

public static error(message: string): EmbedBuilder {
Expand Down
14 changes: 14 additions & 0 deletions src/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ export class Firebase {
}

private getEndpoint(path?: string): string {
path = this.fixPath(path);
return this.baseURL + this.name + encodeURIComponent(path === undefined ? "" : `/${path}`) + this.auth;
}

private fixPath(path?: string): string {
if (path === undefined) return "";
path = this.removeExtraSlash(path);
return path;
}

private removeExtraSlash(path: string): string {
if (path.endsWith("/"))
path = path.slice(0, -1);

return path.endsWith("/") ? this.removeExtraSlash(path) : path;
}
}
6 changes: 5 additions & 1 deletion src/utility.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { PermissionGuard } from "@discordx/utilities";
import type { Client } from "discordx";
import type { Message } from "discord.js";
import type { Message, PermissionsString } from "discord.js";

import Log from "./logger.js";
import Embed from "./embed-presets.js";

export const RequirePermissions = (permissions: PermissionsString[]) => PermissionGuard(permissions, { embeds: [Embed.noPermissions(permissions)] });

/**
* Checks if a message is deletable, and if so deletes it after a specified amount of time (or 0 seconds).
Expand Down

0 comments on commit b074d61

Please sign in to comment.