javascript 为什么用discord.js踢人不起作用

zengzsys  于 2023-05-27  发布在  Java
关注(0)|答案(3)|浏览(117)
const Discord = require("discord.js"),
    bot = new Discord.Client();
let pre = "?";
bot.on("message", async (msg) => {
    var msgArray = msg.content.split(" ");
    var args = msgArray.slice(1);
    var prisonerRole = msg.guild.roles.find("name", "Prisoner");
    let command = msgArray[0];
    if (command == `${pre}roll`) {
        if (!msg.member.roles.has(prisonerRole.id)) {
            roll = Math.floor(Math.random() * 6) + 1;
            msg.reply(`You rolled a ${roll}`);
        } else {
            msg.reply(`HaHa NOOB, you're in prison you don't get priveleges!`);
        }
    }
    if (command == `${pre}kick`) {
        var leaderRole = msg.guild.roles.find("name", "LEADER");
        var co_leaderRole = msg.guild.roles.find("name", "CO-LEADER");
        if (
            msg.member.roles.has(leaderRole.id) ||
            msg.member.roles.has(co_leaderRole.id)
        ) {
            var kickUser = msg.guild.member(msg.mentions.users.first());
            var kickReason = args.join(" ").slice(22);
            msg.guild.member(kickUser).kick();
            msg.channel.send(
                `${msg.author} has kicked ${kickUser}\nReason: ${kickReason}`
            );
        } else {
            return msg.reply("Ya pleb, you can't kick people!");
        }
    }
});
bot.login("token").then(
    function () {
        console.log("Good!");
    },
    function (err) {
        console.log("Still good, as long as the process now exits.");
        bot.destroy();
    }
);

除了真的踢到那个人之外,一切都很有效。这个信息发送坚果它不踢人。例如,当我输入?kick @BobNuggets#4576 inactive时,它说
@rishabhase has kicked @BobNuggets Reason: inactive
但实际上它并没有踢用户,这很奇怪,你能帮我吗?

w41d8nur

w41d8nur1#

变更

msg.guild.member(kickUser).kick();

kickUser.kick();

此外,确保机器人在层次结构中得到提升

cbeh67ev

cbeh67ev2#

使用kickUser.kick();
我建议使用命令处理程序来整理代码。你不希望所有的命令都在一个.js文件中。

o75abkj4

o75abkj43#

对Ban命令本身尝试类似的操作。我用这个作为我的Bot:

client.on("message", (message) => {
if (message.content.startsWith("!ban")) {
    if (!message.member.roles.find("name", "Role that can use this bot"))
        return;
    // Easy way to get member object though mentions.
    var member = message.mentions.members.first();
    // ban
    member
        .ban()
        .then((member) => {
            // Successmessage
            message.channel.send(
                ":wave: " +
                    member.displayName +
                    " has been successfully banned :point_right: "
            );
        })
        .catch(() => {
            // Failmessage
            message.channel.send("Access Denied");
        });
}
});

这应该可以工作,设置您想要使用它的角色(cAsE sEnSiTiVe)并更改!禁止使用任何你想使用的东西。如果你把这里所有的“禁止”都改成踢,效果是一样的。

相关问题