Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

javascript - Unban everyone command

I have made this code for unbanning everyone. But it is not working. What is wrong in this? I am a beginner.

let discord = require("discord.js")
let client = new discord.Client()

client.on('message', message => {
  const member = message.member;
  switch (message.content.toLowerCase()) {
    case (PREFIX + "-unban all"):
      if (member.hasPermission('MANAGE_SERVER')) {
        async function ubAll() {
          const users = await message.guild.fetchBans()
          for (const user of users.array()) {
            await message.guild.unban(user)
          }
        }
        let embed = new discord.MessageEmbed()
          .setTitle("Done!")
          .setDescription("Successfully Unbanned everyone.")
          .setColor("Green")
          .setThumbnail("https://riyazapp.com/wp-content/uploads/2020/04/checkmark.gif")

      }
      if (!member.hasPermission('MANAGE_SERVER')) {
        message.reply("You do not have enough permissions for this command!")
      }
  }
})

client.login("not showing")

   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
let discord = require("discord.js");
let client = new discord.Client();

client.on('message', message => {
  const member = message.member;
  switch (message.content.toLowerCase()) {
    case ("-unban all"):
      if (member.hasPermission('MANAGE_SERVER')) {
        async function ubAll() {
            let users = await message.guild.fetchBans();
            let userArr = users.array();
            let unbanUsers = [];
            for(var i = 0; i < userArr.length; i++) {
                unbanUsers.push(userArr[i].user.id);
            }
            for(var j = 0; j < unbanUsers.length; j++) {
                message.guild.members.unban(unbanUsers[j]);
            }
        }
        ubAll();
        let embed = new discord.MessageEmbed()
          .setTitle("Done!")
          .setDescription("Successfully Unbanned everyone.")
          .setColor("Green")
          .setThumbnail("https://riyazapp.com/wp-content/uploads/2020/04/checkmark.gif")
        message.channel.send(embed);

      }
      else {
        message.reply("You do not have enough permissions for this command!")
      }
      break;
  }
})

client.login("not showing")

Hello, there were many mistakes in your code. You have used PREFIX without defining it. I guessed your prefix is - as I saw it in the first case of your switch-case. Anyways I also fixed your ubAll function. It looks a bit more complicated now, but don't worry it's not that hard to understand. As I'm a fan of classic for-loops, I used them in the new function.

So what does the function?
In users all the information from the banned users are stored. In userArr we simply store all the information we get from users in an array. Then I've created an empty array unbanUsers were we would like to store all the users we would like to unban (everyone in our case). Then in the first for-loop we iterate throug all values in our users array and push the users ID's into our unbanUsers array (because we need the ID of a user to unban him). Now the ID's from every user we found in the first for-loop are stored in unbanUsers. In the second for-loop, we iterate throug our unbanUsers array and unban every user.

Then you have to call the function, because if you don't do that, it simply can't work. You also forgot the break; in your case. For every case in a switch-case you need a break;, because otherwise it will search until he finds the first break; and if there is no one it will create an endless loop. And at the and you have to send the embed you've created into the channel.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...