0
votes

I have a Discord bot I want to use so I can ban a user from all of the servers the bot is in, aka the Network I own.

I need this command as I have other projects and stuff to work on and this seems to be the one that is giving me the most trouble. It would only need to work in the main guild or I switch it to role ID instead of name.

if(command === "sban") {
// this is the role of the moderator which would be used in the main guild to ban from all guilds,
    if(!message.member.roles.some(r=>["Moderator"].includes(r.name)) )
      return message.reply("Sorry, you don't have permissions to use this!");

    let member = message.mentions.members.first();
    if(!member)
      return message.reply("Please mention a valid member of this server");
    if(!member.bannable) 
      return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");

    let reason = args.slice(1).join(' ');
    if(!reason) reason = "No reason provided";

    await member.ban(reason)
      .catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
    message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);

I know the command itself has worked, I just need a way for it to work in at least 8+ Guilds at a time when I need to ban someone.

1

1 Answers

0
votes

If I have understood, you are trying to ban a user from all servers your bot is in.

To do that, do a forEach loop on client.guilds and ban him from the guild.

Even if the user is not in the guild, you can still ban him by their ID. Here's an example I made:

    function getUserFromMention(mention) {
        if (!mention) return false;
        if (mention.startsWith("<@") && mention.endsWith(">")) {
            mention = mention.slice(2, -1);
            if (mention.startsWith("!")) {
                mention.slice(1);
                return client.users.get(mention); // Retun the user from mention.
            };
        };
        if (client.users.get(mention)) {
            return client.users.get(mention); // Return the user if the author provided an ID.
        };
    };

    const UserToBan = getUserFromMention(arguments[0])
    const Reason = arguments.slice(1, 2000).join(" ")

    if (!UserToBan) return message.reply("Please mention a user or provide an id.");
    if (!Reason) return message.reply("Please provide a reason for the ban.");

    client.guilds.forEach(guild => {
        if (guild.members.get(UserToBan.id)) {
            // The user is in the guild.
            message.reply(`Banned the user ${UserToBan} in ${guild.name}`);
            guild.ban(UserToBan, Reason);
        } else {
            // The user is not in the guild, we have to ban him by id.
            message.reply(`Banned the user with the id ${UserToBan.id} in ${guild.name}!`);
            guild.ban(UserToBan.id, Reason);
        }
    });

You should modify the code a bit. It will send a message for each guild.

This is only an example. You should check if the user is bannable in that guild by the boolean property .bannable of GuildMember.