Matheus Motta
Matheus Motta

Reputation: 19

How do I unban user with commands using Autocode?

I don't understand what's wrong with my coding but when I try to unban someone it doesn't work.

I have configured the bot to unban a member with an id but it doesn't work.

When I try the command but putting the username of the concerned member, the bot sends me an error message like "Invalid command, use: w!unban <user ID>". This is normal because I configured it so that it sends this message when the command has been written wrong. However, when I do the command with the ID, I have no error message as if it had worked, but yet the member is not unbanned.

// Secret Token
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});

// Check to see if guild is using prefix commands
let CommandType = process.env.COMMANDTYPE;
if (CommandType === 'prefix' || CommandType === 'both') {
  
  // Checks to see if the message starts with (prefixunban)
  let message = `${context.params.event.content}`;
  if (message.startsWith(`${process.env.COMMANDPREFIX}unban`)) {
    
    // Retreive the user that triggered the command
    let serverMember = await lib.discord.guilds['@0.1.0'].members.retrieve({
      user_id: `${context.params.event.author.id}`,
      guild_id: `${context.params.event.guild_id}`,
    });
    
    // Gets guild owner
    let guild = await lib.discord.guilds['@0.1.0'].retrieve({
      guild_id: `${context.params.event.guild_id}`,
    });
    let guildOwner = guild.owner_id;


    // checks to see if you have access to the command
    if (
      serverMember.roles.includes(`${process.env.ADMINROLE}`) ||
      context.params.event.author.id === guildOwner
    ) {
      
      // Gets the Current Time
      const now = new Date();
      const m = now.getMonth() + 1; // 0 index based
      const d = now.getDate();
      const h = now.getHours();
      const min = now.getMinutes();

      const dateText = `${m}/${d}, ${h}:${min}`; // -> "6/6, 15:19"


      // Tests to make sure that the message follows the right format
      let regEx = /.+unban \d+/;
      if (regEx.test(message)) {
        
        // Grabs the user for the unban
        let userId = message.split(' ').slice(1).join(' ');
        
        // Sends a DM to notify the user that they were Unmuted
        await lib.discord.users['@0.1.1'].dms.create({
          recipient_id: `${userId}`,
          content: `**${process.env.SERVERNAME} Alerte**`,
          tts: false,
          embed: {
            type: 'rich',
            title: 'Unban',
            description: `Hey ! Tu as été unban de ${process.env.SERVERNAME}.`,
            color: 0x2ec4b6,
          },
        });
        
        // Logs the unban to the Log Channel
        await lib.discord.channels['@0.1.1'].messages.create({
          channel_id: `921936072945041408`,
          content: '**Moderation logs |** Unban',
          tts: false,
          embed: {
            type: 'rich',
            title: 'Unban',
            description: `Un membre a été unban ${process.env.SERVERNAME}!`,
            color: 0x9be564,
            fields: [
              {
                name: 'Utilisateur | 👤',
                value: `<@!${userId}>`,
              },
              {
                name: 'Modérateur | 🔒',
                value: `<@!${context.params.event.author.id}>`,
              },
              {
                name: 'À | ⏱️',
                value: `${dateText} UTC`,
              },
            ],
          },
        });

        // Unbans the specified user in the guild
        let result = await lib.discord.guilds['@0.1.0'].bans.destroy({
          user_id: `${userId}`,
          guild_id: `${context.params.event.guild_id}`,
        });

      } else {
        
        // Gives an error message due to not having correct Formatting
        await lib.discord.channels['@0.2.0'].messages.create({
          channel_id: `${context.params.event.channel_id}`,
          content: `> Commande invalide, utilises : **${process.env.COMMANDPREFIX}unban <ID utilisateur>**`,
          message_reference: {
            message_id: `${context.params.event.id}`,
            channel_id: `${context.params.event.channel_id}`,
            guild_id: `${context.params.event.guild_id}`,
          },
        });
      }
    } else {
      
      // Gives an error message due to not having correct Permssions
      await lib.discord.channels['@0.2.0'].messages.create({
        channel_id: `${context.params.event.channel_id}`,
        content: `> <@!${context.params.event.author.id}>** | Désolé, tu n'as pas la permission pour utiliser \`${process.env.COMMANDPREFIX}unban\`**`,
        message_reference: {
          message_id: `${context.params.event.id}`,
          channel_id: `${context.params.event.channel_id}`,
          guild_id: `${context.params.event.guild_id}`,
        },
      });
    }
  }
}

I'm using Autocode.

Upvotes: 1

Views: 95

Answers (0)

Related Questions