如何发出Discord机器人命令?

时间:2019-09-14 02:16:24

标签: javascript discord.js

我有一个想要使用的Discord僵尸程序,因此可以禁止该僵尸程序所在的所有服务器(也就是我拥有的网络)中的用户。

我需要此命令,因为我还有其他项目和工作需要进行,这似乎给我带来了最大的麻烦。它只需要在主行会中工作,或者我将其切换为角色ID而不是名称。

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}`);

我知道命令本身已经起作用,我只需要一种方法就可以在需要禁止某人的时候至少在8个以上的公会中起作用。

1 个答案:

答案 0 :(得分:0)

据我了解,您正试图禁止用户访问您的漫游器所在的所有服务器。

要这样做,请在forEach上进行client.guilds循环,并禁止他加入公会。

即使用户不在行会中,您仍然可以通过其ID禁止他。这是我制作的一个示例:

    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);
        }
    });

您应该稍微修改一下代码。它将为每个公会发送一条消息。

这只是一个例子。您应该通过boolean的{​​{1}}属性.bannable检查该行中的用户是否可以接受。