nyx/command/Owner/eval.js

130 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2021-09-06 12:38:25 +02:00
const Discord = require("discord.js");
module.exports = {
name: "eval",
category: "Owner",
usage: "(Code)",
Owner: true,
description: "Eval something",
2022-10-26 12:38:28 +02:00
2021-09-06 12:38:25 +02:00
options: [
{
type: 3,
name: "code",
description: "The code to eval",
required: true,
},
],
run: async (client, interaction, args, utils) => {
const code = args[0];
2021-09-06 12:38:25 +02:00
function CheckFilter(object) {
if (typeof object === "string") {
object = object.replace(
new RegExp(`${client.token || process.env.TOKEN}`, "gi"),
2021-09-06 12:38:25 +02:00
"Cannot eval Token"
);
} else if (typeof object === "object") {
if (Array.isArray(object)) {
for (let i = 0; i < object.length; i++) {
object[i] = CheckFilter(object[i]);
}
} else {
for (const key in object) {
2021-09-06 12:38:25 +02:00
object[key] = CheckFilter(object[key]);
}
}
}
return object;
}
const oldSend = Discord.TextChannel.prototype.send;
2021-09-06 12:38:25 +02:00
Discord.TextChannel.prototype.send = async function send(content, options) {
return oldSend.bind(this)(CheckFilter(content), CheckFilter(options));
};
let evaled;
try {
evaled = eval(code);
if (evaled instanceof Promise) evaled = await evaled;
} catch (err) {
evaled = err;
}
if (typeof evaled !== "string") evaled = require("util").inspect(evaled);
2022-01-31 20:01:40 +01:00
evaled = chunk([evaled], 750);
2021-09-06 12:38:25 +02:00
let reactions = ["❌", "⏪", "◀️", "⏹️", "▶️", "⏩"],
page = 0,
2022-10-26 12:38:28 +02:00
evaledEmbed = new Discord.EmbedBuilder()
2021-09-06 12:38:25 +02:00
.setColor(client.color)
.setDescription(`\`\`\`js\n${evaled[0]}\n\`\`\``)
.setTimestamp()
2022-01-31 20:01:40 +01:00
.setAuthor({
name: `Evaled by ${interaction.user.tag}`,
iconURL: interaction.user.displayAvatarURL({ dynamic: true }),
});
const mainMessage = await interaction.channel.send({
embeds: [evaledEmbed],
});
2021-09-06 12:38:25 +02:00
Discord.TextChannel.prototype.send = oldSend;
await Promise.all(
(evaled.length === 1 ? ["❌", "⏹️"] : reactions).map(r =>
mainMessage.react(r)
)
);
const filter = (reaction, user) =>
2021-09-06 12:38:25 +02:00
(evaled.length === 1 ? ["❌", "⏹️"] : reactions).some(
e => e === reaction.emoji.name
) && user.id === interaction.user.id;
const collector = mainMessage.createReactionCollector({
filter,
time: 999999,
2021-09-06 12:38:25 +02:00
});
collector.on("collect", async reaction => {
2021-09-06 12:38:25 +02:00
switch (reaction.emoji.name) {
case "❌":
await collector.stop();
return mainMessage.delete();
case "⏪":
if (evaled.length === 1 || page === 0) return;
page = 0;
break;
case "◀️":
if (evaled.length === 1) return;
if (page === 0) {
page = evaled.length - 1;
} else {
page -= 1;
}
break;
case "⏹️":
await collector.stop();
for (const reaction of mainMessage.reactions.cache.map(e => e)) {
2021-09-06 12:38:25 +02:00
await reaction.users.remove(client.user.id);
}
return;
case "▶️":
if (evaled.length === 1) return;
if (page === evaled.length - 1) {
page = 0;
} else {
page += 1;
}
break;
case "⏩":
if (evaled.length === 1 || page === evaled.length - 1) return;
page = evaled.length - 1;
break;
}
2022-10-26 12:38:28 +02:00
evaledEmbed = new Discord.EmbedBuilder()
2021-09-06 12:38:25 +02:00
.setColor(interaction.guild.me.displayColor)
2022-01-31 20:01:40 +01:00
.setDescription(`\`\`\`js\n${evaled[page]}\n\`\`\``);
2021-09-06 12:38:25 +02:00
await mainMessage.edit({
embeds: [evaledEmbed],
});
});
},
};
2022-01-31 20:01:40 +01:00
function chunk(arr, size) {
let c;
Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => {
c = arr.slice(i * size, i * size + size);
});
return c;
}