Skip to content

Commit

Permalink
Merge pull request #45 from SimonB50/eval
Browse files Browse the repository at this point in the history
New Feature | Eval command
  • Loading branch information
DakshNamdev committed Apr 8, 2022
2 parents a85e891 + 16a1115 commit df25f76
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
106 changes: 106 additions & 0 deletions Commands/Admin/eval.js
@@ -0,0 +1,106 @@
const Utils = require("../../Modules/Utils"),
{ lang, config, commands } = require("../../index"),
moment = require("moment");

module.exports = {
name: "eval",
type: "admin",
commandData: commands.Admin.Eval,
};

/**
*
* @param {Discord.Client} bot
* @param {Discord.Message} message
* @param {Array} args
*/
module.exports.run = async (bot, message, args) => {
// Define code to eval
const input = args.join(" ");
// Send error message if code is empty
if (!input) return message.reply(Utils.setupMessage({
configPath: lang.Presets.Error,
variables: [
{ searchFor: /{error}/g, replaceWith: "You need to define code, which should be executed!" },
...Utils.userVariables(message.member),
...Utils.botVariables(bot),
],
}));
// Send error message if input includes bot.token
if (input.includes("token")) return message.reply(Utils.setupMessage({
configPath: lang.Presets.Error,
variables: [
{ searchFor: /{error}/g, replaceWith: "You can't execute code, which includes bot token!" },
...Utils.userVariables(message.member),
...Utils.botVariables(bot),
],
}));
// Try to execute code
try {
var output = await eval(input);
} catch (e) {
var output = e;
}
// Send error message if output includes token
if (output.toString().includes(bot.token)) return message.reply(Utils.setupMessage({
configPath: lang.Presets.Error,
variables: [
{ searchFor: /{error}/g, replaceWith: "You can't execute code, which includes bot token!" },
...Utils.userVariables(message.member),
...Utils.botVariables(bot),
],
}));
// Send message with eval output
message.reply(Utils.setupMessage({
configPath: lang.Admin.Eval,
variables: [
{ searchFor: /{input}/g, replaceWith: input },
{ searchFor: /{output}/g, replaceWith: output ? output : "Check console" },
...Utils.userVariables(message.member),
...Utils.botVariables(bot),
],
}));
};
/**
*
* @param {Discord.Client} bot
* @param {Discord.Interaction} interaction
*/
module.exports.runSlash = async (bot, interaction) => {
// Define code to eval
const input = interaction.options.getString("code");
// Send error message if code is empty
if (!input) return interaction.reply(Utils.setupMessage({
configPath: lang.Presets.Error,
variables: [
{ searchFor: /{error}/g, replaceWith: "You need to define code, which should be executed!" },
...Utils.userVariables(interaction.member),
...Utils.botVariables(bot),
],
}));
// Send error message if input includes bot.token
if (input.includes("bot.token")) return interaction.reply(Utils.setupMessage({
configPath: lang.Presets.Error,
variables: [
{ searchFor: /{error}/g, replaceWith: "You can't execute code, which includes bot token!" },
...Utils.userVariables(interaction.member),
...Utils.botVariables(bot),
],
}));
// Try to execute code
try {
var output = await eval(input);
} catch (e) {
var output = e;
}
// Send message with eval output
interaction.reply(Utils.setupMessage({
configPath: lang.Admin.Eval,
variables: [
{ searchFor: /{input}/g, replaceWith: input },
{ searchFor: /{output}/g, replaceWith: output ? output : "Check console" },
...Utils.userVariables(interaction.member),
...Utils.botVariables(bot),
],
}));
};
18 changes: 18 additions & 0 deletions commands.yml
Expand Up @@ -75,3 +75,21 @@ General:
- Type: "User"
Name: "target"
Description: "User Mention"
Admin:
Eval:
Description: "Runs JavaScript code"
Usage: "eval <code>"
Aliases: []
Permission:
- "Zorino#1110"
AllowedChannels: false
SlashCommand:
Enabled: true
Data:
Name: eval
Description: "Runs JavaScript code"
Options:
- Type: "String"
Name: "code"
Description: "JavaScript code"
Required: true
16 changes: 16 additions & 0 deletions lang.yml
Expand Up @@ -86,6 +86,22 @@ General:
Style: Link
Label: Avatar
Link: '{link}'
Admin:
Eval:
Embeds:
- Title: '{bot-username} Eval'
Fields:
- Name: ⬇️ | Input
Value: |-
```js
{input}```
- Name: ⬆️ | Output
Value: |-
```js
{output}```
Footer: '{user-tag}'
FooterIcon: '{user-pfp}'
Timestamp: true

Presets:
NoPermission:
Expand Down

0 comments on commit df25f76

Please sign in to comment.