Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
chore: updates to eval command
Browse files Browse the repository at this point in the history
  • Loading branch information
janleigh committed Jul 4, 2023
1 parent d154a66 commit b2a7e28
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 20 deletions.
16 changes: 16 additions & 0 deletions .sapphirerc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"projectLanguage": "ts",
"locations": {
"base": "src",
"arguments": "arguments",
"commands": "commands",
"listeners": "listeners",
"preconditions": "preconditions",
"interaction-handlers": "interaction-handlers",
"routes": ""
},
"customFileTemplates": {
"enabled": false,
"location": ""
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"version": "1.0.0",
"author": {
"name": "janleigh",
"url": "https://github.com/janleigh/"
"url": "https://github.com/janleigh/servant"
},
"license": "MIT",
"main": "build/index.js",
"scripts": {
"build": "yarn build:clean && yarn format:check && yarn compile",
"build:production": "yarn build:clean && yarn format:check && yarn:lint yarn compile",
"build": "yarn build:clean && yarn compile",
"build:production": "yarn build:clean && yarn format:check && yarn lint && yarn compile",
"build:clean": "rm -rvf ./build/",
"compile": "npx sucrase src -d build --transforms typescript,imports",
"format": "prettier --write src",
Expand Down
33 changes: 22 additions & 11 deletions src/commands/developer/EvaluateCommand.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
import { ChatInputCommand, Command } from "@sapphire/framework";
import { BaseEmbedBuilder } from "../../libraries/structures/components/BaseEmbedBuilder";
import { DEV_USER_IDS } from "../../config";
import { ComponentType } from "discord.js";
import { deleteBtn } from "../../libraries/structures/components/Buttons";

export class EvalCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
fullCategory: ["Developer"]
fullCategory: ["Developer"],
preconditions: ["DeveloperOnlyPrecondition"]
});
}

public override registerApplicationCommands(registry: ChatInputCommand.Registry) {
registry.registerChatInputCommand((builder) =>
builder
.setName("eval")
.setDescription("Execute some javascript.")
.setDescription("Execute some raw JavaScript code.")
.addStringOption((option) =>
option.setName("input").setDescription("The code to execute").setRequired(true)
option.setName("input").setDescription("The code to execute.").setRequired(true)
)
);
}

public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const input = interaction.options.getString("input");
const embed = new BaseEmbedBuilder();
let content = "";

try {
if (!DEV_USER_IDS.includes(interaction.user.id)) {
throw Error("This command is dev only.");
}

const evaled = eval(input as string);
embed.isSuccessEmbed().setDescription(`\`\`\`xl\n${evaled}\`\`\``);
const timeTaken = ((Date.now() - interaction.createdTimestamp) / 1000).toFixed(3);

content = `:bricks: **EVAL COMPLETE** (${timeTaken}s) :bricks:\n\`\`\`xl\n${evaled}\`\`\``;
} catch (err) {
embed.setTitle("Error Occured").isErrorEmbed().setDescription(`${err}`);
this.container.logger.error(`[EvalCommand] ${err}`);
embed.isErrorEmbed().setDescription("An error occurred. Check the console for more details.");
}

return interaction.reply({ embeds: [embed] });
return interaction.reply({
content: content,
embeds: embed.data.description ? [embed] : [],
components: [
{
type: ComponentType.ActionRow,
components: [deleteBtn]
}
]
});
}
}
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ClientOptions, GatewayIntentsString } from "discord.js";
* @description The dev server IDs. The only places where developer commands will be registered.
* @type {string[]}
*/
export const DEV_SERVER_IDS: string[] = ["853812920919261235"];
export const DEV_SERVER_IDS: string[] = ["853812920919261235", "1125589968694280273"];

/**
* @description The dev user IDs. The only users who can use developer commands.
Expand Down
23 changes: 23 additions & 0 deletions src/interaction-handlers/EvalDeleteButtonHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { InteractionHandler, InteractionHandlerTypes, PieceContext } from "@sapphire/framework";
import type { ButtonInteraction } from "discord.js";

export class EvalDeleteButtonHandler extends InteractionHandler {
public constructor(ctx: PieceContext, options: InteractionHandler.Options) {
super(ctx, {
...options,
interactionHandlerType: InteractionHandlerTypes.Button
});
}

public override parse(interaction: ButtonInteraction) {
if (interaction.customId !== "evalDelete") return this.none();

return this.some();
}

public async run(interaction: ButtonInteraction) {
await interaction.deferUpdate();

return interaction.message.delete();
}
}
6 changes: 6 additions & 0 deletions src/libraries/declarations/augments.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ export declare global {
}
}
}

declare module "@sapphire/framework" {
interface Preconditions {
DeveloperOnlyPrecondition: never;
}
}
26 changes: 24 additions & 2 deletions src/libraries/structures/components/BaseEmbedBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ export class BaseEmbedBuilder extends EmbedBuilder {
/**
* @description Whether the embed is an error embed.
* @type {boolean}
* @default false
*/
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
public errorEmbed: boolean = false;

/**
* @description Whether the embed has a checkmark.
* @type {boolean}
* @default false
*/
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
public hasCheckmark: boolean = false;

/**
* @description The embed constructor.
* @constructor
Expand All @@ -16,9 +25,20 @@ export class BaseEmbedBuilder extends EmbedBuilder {
super();
}

/**
* @override
* @description Sets the embed description.
* @returns {this}
*/
public override setDescription(description: string | null): this {
if (description === null) return this;
if (this.errorEmbed === true) return super.setDescription(`<:kekpoint:917360277647925268> **${description}**`);
if (this.errorEmbed === true) {
return super.setDescription(`<:crossmark:1125590268419244042> **${description}**`);
}
if (this.hasCheckmark === true) {
return super.setDescription(`<:checkmark:1125590254313811998> **${description}**`);
}

return super.setDescription(description);
}

Expand All @@ -34,10 +54,12 @@ export class BaseEmbedBuilder extends EmbedBuilder {

/**
* @description Sets the embed color to green.
* @param {boolean} emoji Whether to add the checkmark emoji to the embed description.
* @returns {this}
*/
public isSuccessEmbed(): this {
public isSuccessEmbed(emoji?: boolean): this {
this.setColor("#1ED760");
emoji === true ? (this.hasCheckmark = true) : (this.hasCheckmark = false);
return this;
}
}
6 changes: 6 additions & 0 deletions src/libraries/structures/components/Buttons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ButtonBuilder, ButtonStyle } from "discord.js";

export const deleteBtn = new ButtonBuilder()
.setCustomId("evalDelete")
.setLabel("Delete Output")
.setStyle(ButtonStyle.Danger);
6 changes: 3 additions & 3 deletions src/listeners/ReadyListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export class ReadyListener extends Listener {
activities: [
{
name:
this.container.client.user?.id === "1091917720641081374"
process.env.NODE_ENV === "development"
? "my devs test in production"
: `${CLIENT_OPTIONS.defaultPrefix}help`,
type: ActivityType.Watching
type: ActivityType.Listening
}
],
status: "dnd"
status: "idle"
});
}
}
18 changes: 18 additions & 0 deletions src/preconditions/DeveloperOnlyPrecondition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Command, Precondition } from "@sapphire/framework";
import { DEV_USER_IDS } from "../config";

export class DeveloperOnlyPrecondition extends Precondition {
public chatInputRun(interaction: Command.ChatInputCommandInteraction) {
return this.checkDev(interaction.user.id);
}

public contextMenuRun(interaction: Command.ContextMenuCommandInteraction) {
return this.checkDev(interaction.user.id);
}

checkDev(userId: string) {
return DEV_USER_IDS.includes(userId)
? this.ok()
: this.error({ message: "You are not allowed to use this command." });
}
}

0 comments on commit b2a7e28

Please sign in to comment.