Skip to content

Commit

Permalink
🎢 Run test javascript code
Browse files Browse the repository at this point in the history
  • Loading branch information
Maseshi committed May 2, 2024
1 parent bbc3447 commit 678b45f
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions source/commands/tools/eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const {
SlashCommandBuilder,
EmbedBuilder,
Colors,
PermissionFlagsBits,
} = require('discord.js')

module.exports = {
permissions: [PermissionFlagsBits.SendMessages],
data: new SlashCommandBuilder()
.setName('eval')
.setDescription('Evaluate the Javascript code for testing.')
.setDescriptionLocalizations({
th: 'ประเมินรหัส JavaScript สำหรับทดสอบผลการทำงาน',
})
.setDefaultMemberPermissions()
.setDMPermission(true)
.addStringOption((option) =>
option
.setName('script')
.setDescription('The Javascript code to be evaluated.')
.setRequired(true)
),
async execute(interaction) {
const inputScript = interaction.options.getString('script')

const resultEmbed = new EmbedBuilder().setTitle('🔭 ผลการทำงาน')
const secureEval = (obj) => eval?.(`"use strict";(${obj})`)

try {
resultEmbed
.setDescription(`\`\`\`JavaScript\n${secureEval(inputScript)}\n\`\`\``)
.setColor(Colors.Green)
} catch (error) {
resultEmbed
.setDescription(`\`\`\`JavaScript\n${error.toString()}\n\`\`\``)
.setColor(Colors.Red)
}

await interaction.reply({ embeds: [resultEmbed] })
},
}

0 comments on commit 678b45f

Please sign in to comment.