nyx

The first CODM discrod bot -- cath.exe Template
git clone https://codeberg.org/night0721/nyx
Log | Files | Refs | LICENSE

interactionCreate.js (7220B)


      1 const client = require("../");
      2 const cooldown = require("../models/cooldown");
      3 const utils = require("../util/functions/function");
      4 const { EmbedBuilder } = require("discord.js");
      5 client.on("interactionCreate", async interaction => {
      6   if (interaction.isCommand()) {
      7     await interaction.deferReply({ ephemeral: false }).catch(() => {});
      8     const cmd = client.slashCommands.get(interaction.commandName);
      9     if (!cmd) return;
     10     const args = [];
     11     for (const option of interaction.options.data) {
     12       if (option.type === 2) {
     13         if (option.name) args.push(option.name);
     14         option.options?.forEach(x => {
     15           if (x.type === 1) {
     16             if (x.name) args.push(x.name);
     17             x.options?.forEach(y => {
     18               if (y.value) args.push(y.value);
     19             });
     20           } else if (x.value) {
     21             args.push(x.value);
     22           }
     23           if (x.value) args.push(x.value);
     24         });
     25       }
     26       if (option.type === 1) {
     27         if (option.name) args.push(option.name);
     28         option.options?.forEach(x => {
     29           if (x.value) args.push(x.value);
     30         });
     31       } else if (option.value) {
     32         args.push(option.value);
     33       }
     34     }
     35     interaction.member = interaction.guild.members.cache.get(
     36       interaction.user.id
     37     );
     38     const data = {};
     39     const guildDB = await client.data.getGuild(interaction.guild.id);
     40     if (!guildDB) return;
     41     const userDB = await client.data.getUser(interaction.user.id);
     42     if (!userDB) return;
     43     data.Guild = guildDB;
     44     data.User = userDB;
     45     if (!guildDB) await client.data.CreateGuild(interaction.guild.id);
     46     if (data.User?.Blacklist)
     47       return interaction.followUp({
     48         content:
     49           "You have been blacklisted from the bot, please contact the developers to appeal",
     50       });
     51     if (cmd.Owner && !client.owners.includes(interaction.user.id)) return;
     52     if (cmd.Premium && !data.User.Premium) {
     53       return interaction.followUp({
     54         embeds: [
     55           new EmbedBuilder()
     56             .setURL(client.web)
     57             .setAuthor({
     58               name: interaction.user.tag,
     59               iconURL: interaction.user.displayAvatarURL({ dynamic: true }),
     60             })
     61             .setColor(client.color)
     62             .setDescription(
     63               `You aren't a premium user. You can either boost support server or subscribe to developer's team [Ko-fi](https://ko-fi.com/cathteam) or gift a nitro to one of the developer team to be premium user`
     64             )
     65             .setTimestamp()
     66             .setFooter({
     67               text: `Made by ${client.author}`,
     68               iconURL: client.user.displayAvatarURL({ dynamic: true }),
     69             }),
     70         ],
     71       });
     72     }
     73     if (data.Guild?.Category) {
     74       if (data.Guild.Category.includes(cmd.directory)) {
     75         return interaction.followUp({
     76           content: "This command has been disabled in this server",
     77         });
     78       }
     79     }
     80     if (data.Guild?.Commands) {
     81       if (data.Guild.Commands.includes(cmd.name)) {
     82         return interaction.followUp({
     83           content: "This command has been disabled in this server",
     84         });
     85       }
     86     }
     87 
     88     if (cmd.timeout) {
     89       const current_time = Date.now();
     90       const cooldown_amount = cmd.timeout;
     91       cooldown.findOne(
     92         { User: interaction.user.id, CMD: cmd.name },
     93         async (er, d) => {
     94           if (d) {
     95             const expiration_time = d.Time + cooldown_amount;
     96             if (current_time < expiration_time) {
     97               if (data.Guild.Tips) utils.tips(interaction, client);
     98               utils.cooldown(d.Time, cooldown_amount, interaction);
     99             } else {
    100               if (data.Guild.Tips) utils.tips(interaction, client);
    101               await cooldown.findOneAndUpdate(
    102                 { User: interaction.user.id, CMD: cmd.name },
    103                 { Time: current_time }
    104               );
    105               cmd
    106                 .run(client, interaction, args, utils, data)
    107                 .catch(e => sendE(e, interaction));
    108               client.channels.cache.get(client.config.CMDLog).send({
    109                 content: `\`${interaction.user.tag}(${interaction.user.id})\`\n has used \n**${cmd.name}**\n command in \n\`${interaction.guild.name}(${interaction.guild.id})\``,
    110               });
    111             }
    112           } else {
    113             if (data.Guild.Tips) utils.tips(interaction, client);
    114             cmd
    115               .run(client, interaction, args, utils, data)
    116               .catch(e => sendE(e, interaction));
    117             client.channels.cache.get(client.config.CMDLog).send({
    118               content: `\`${interaction.user.tag}(${interaction.user.id})\`\n has used \n**${cmd.name}**\n command in \n\`${interaction.guild.name}(${interaction.guild.id})\``,
    119             });
    120             new cooldown({
    121               User: interaction.user.id,
    122               CMD: cmd.name,
    123               Time: current_time,
    124               Cooldown: cmd.timeout,
    125             }).save();
    126           }
    127         }
    128       );
    129     } else {
    130       if (data.Guild.Tips) utils.tips(interaction, client);
    131       cmd
    132         .run(client, interaction, args, utils, data)
    133         .catch(e => sendE(e, interaction));
    134       client.channels.cache.get(client.config.CMDLog).send({
    135         content: `\`${interaction.user.tag}(${interaction.user.id})\`\n has used \n**${cmd.name}**\n command in \n\`${interaction.guild.name}(${interaction.guild.id})\``,
    136       });
    137     }
    138   }
    139   if (interaction.isContextMenuCommand()) {
    140     await interaction.deferReply({ ephemeral: false });
    141     const command = client.slashCommands.get(interaction.commandName);
    142     if (command) command.run(client, interaction);
    143   }
    144 });
    145 client.on("interactionCreate", async interaction => {
    146   if (interaction.isCommand()) {
    147     await interaction.deferReply({ ephemeral: false }).catch(() => {});
    148     const ownercmd = client.hide.get(interaction.commandName);
    149     if (!ownercmd) return;
    150     const args = [];
    151     for (const option of interaction.options.data) {
    152       if (option.type === 1) {
    153         if (option.name) args.push(option.name);
    154         option.options?.forEach(x => {
    155           if (x.value) args.push(x.value);
    156         });
    157       } else if (option.value) {
    158         args.push(option.value);
    159       }
    160     }
    161     interaction.member = interaction.guild.members.cache.get(
    162       interaction.user.id
    163     );
    164     ownercmd
    165       .run(client, interaction, args, utils)
    166       .catch(e => sendE(e, interaction));
    167     client.channels.cache.get(client.config.CMDLog).send({
    168       content: `\`${interaction.user.tag}(${interaction.user.id})\`\n has used \n**${ownercmd.name}**\n command in \n\`${interaction.guild.name}(${interaction.guild.id})\``,
    169     });
    170   }
    171 });
    172 function sendE(e, i) {
    173   console.error(e.stack);
    174   const embed = new EmbedBuilder()
    175     .setTitle("Command Error")
    176     .setDescription(`\`\`\`yaml\n${e.stack}\`\`\``)
    177     .setTimestamp()
    178     .setColor(client.color)
    179     .setFooter({ text: client.user.username });
    180   i.channel.send({ embeds: [embed] });
    181   client.channels.cache.get(client.config.ErrorLog).send({ embeds: [embed] });
    182 }