Skip to content

Commit

Permalink
feat: dev cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
DaStormer committed Nov 27, 2023
1 parent 4d753be commit e811e5b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
73 changes: 73 additions & 0 deletions bot/interactions/textcommands/eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const TextCommand = require("../../../structures/base/BaseTextCommand");
const { EmbedBuilder } = require("discord.js");
const { inspect } = require("util");

class EvalCommand extends TextCommand {
/**
* @param {import("../../index.js")} client MentorQ's Discord Client.
*/
constructor(client) {
super(client, {
name: "eval",
category: "dev",
description: "Evaluate code.",
args: true,
});
}

/**
* @param {import("discord.js").Message} message
* @param {Array<string>} args
* @returns
*/
async run(message, args) {

const code = args.join(" ");

const hrStart = process.hrtime();

try {

let evaled = eval(`(async () => {${code}})()`);
const hrDiff = process.hrtime(hrStart);

if (evaled.then && evaled.catch) evaled = await evaled;
if (typeof evaled !== "string") evaled = inspect(evaled, { depth: 0 });

const result = evaled.length > 4000 ? evaled.substring(0, 4000) : evaled;

const resultEmbed = new EmbedBuilder()
.setAuthor({ name: "Eval | " + message.author.username, iconURL: message.author.displayAvatarURL() })
.setTitle("**``EVAL RESULT``**")
.setDescription("```js\n" + result + "```")
.setColor("Blurple")
.setFooter({ text: `Executed in ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ""}${hrDiff[1] / 1000000}ms` })
.setTimestamp();

message.channel.send({ embeds: [resultEmbed] });

} catch (err) {

const hrDiff = process.hrtime(hrStart);

const result = err.stack.length > 4000 ? err.stack.substring(0, 4000) : err.stack;

const resultEmbed = new EmbedBuilder()
.setAuthor({ name: "Eval | " + message.author.username, iconURL: message.author.displayAvatarURL() })
.setTitle("**``EVAL ERROR``**")
.setDescription("```js\n" + result + "```")
.setColor("Red")
.setFooter({ text: `Executed in ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ""}${hrDiff[1] / 1000000}ms` })
.setTimestamp();

message.channel.send({ embeds: [resultEmbed] });

}

return;

}

}

module.exports = EvalCommand;
52 changes: 52 additions & 0 deletions bot/interactions/textcommands/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { ChildProcess } = require("child_process");
const TextCommand = require("../../../structures/base/BaseTextCommand");
const { EmbedBuilder } = require("discord.js");

class ExecCommand extends TextCommand {
/**
* @param {import("../../index.js")} client MentorQ's Discord Client.
*/
constructor(client) {
super(client, {
name: "exec",
category: "dev",
description: "Run commands in the terminal.",
args: true,
});
}

/**
* @param {import("discord.js").Message} message
* @param {Array<string>} args
* @returns
*/
async run(message, args) {

const hrStart = process.hrtime();

ChildProcess.exec(args.join(" "), (error, stdout) => {

const hrDiff = process.hrtime(hrStart);

let result = error?.toString() ?? stdout.toString();
result = result.length > 4000 ? result.substring(0, 4000) : result;

const execEmbed = new EmbedBuilder()
.setAuthor({ name: `Exec | ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTitle(error ? "**__``EXEC ERROR``__**" : "**__``EXEC RESULT``__**")
.setColor(error ? "Red" : "Blurple")
.setDescription(result)
.setFooter({ text: `Executed in ${hrDiff[0] > 0 ? `${hrDiff[0]}s ` : ""}${hrDiff[1] / 1000000}ms` })
.setTimestamp();

message.channel.send({ embeds: [execEmbed] });

});

return;

}

}

module.exports = ExecCommand;

0 comments on commit e811e5b

Please sign in to comment.