【问题标题】:Banning user who is replied to (Discord JS)禁止回复的用户(Discord JS)
【发布时间】:2022-02-14 18:22:55
【问题描述】:

正如标题所说,我正在尝试编写一些代码,如果他们的消息被用ban 这个词回复,将禁止用户。我认为我获取原始消息 id 的方式是错误的,但我不确定如何实现它。我得到的错误是“TypeError:无法读取未定义的属性(读取'id')”

client.on("messageCreate", (message) => {
    if (message.content == "ban" && message.type == 'REPLY') {
        const member = message.reference.author.id;
        message.guild.members.ban(member).then(console.log)
        .catch(console.error);
}
})

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    将您当前的代码更改为:

     const client = new Discord.Client({ intents: [ "GUILDS",
     "GUILD_MESSAGES",
     "GUILD_MEMBERS" ] });
    
    client.on("messageCreate", (message) => {
      if (message.content == "ban" && message.type == 'REPLY') {
         const member = message.author.id;
         message.guild.members.ban(member).then(console.log)
         .catch(console.error);
     }
    })
    

    访问Server Members Intent

    1. 转到不和谐/开发者
    2. 应用程序中,点击您的项目/机器人
    3. 点击BOT部分
    4. 向下滚动,直到看到 Privileged Gateway Intents
    5. 查找服务器成员意图
    6. 启用它

    现在再次尝试脚本

    【讨论】:

    • 这也不起作用,现在它给了我这个错误。 DiscordAPIError: Missing Permissions at RequestHandler.execute 我假设是因为这个机器人现在只是想禁止我,不是吗?
    • 你的机器人有封禁权限吗?
    • 拥有管理员权限。
    • 我可以看看你的意图吗?
    • const client = new Discord.Client({ intents: [ "GUILDS", "GUILD_MESSAGES" ] })
    【解决方案2】:

    我建议你这样做:

    const client = new Discord.Client({ intents: [ "GUILDS",
     "GUILD_MESSAGES",
     "GUILD_MEMBERS" ] });
    
    client.on("messageCreate", async (message) => {
      if (message.content == "ban" && message.reference) {
         const msg = await message.channel.messages.fetch(message.reference.messageId);//cache the message that replied to
         const member = msg.member//cache the author of replied to
         member.ban(member).then(console.log)//ban the user
         .catch(console.error);
     }
    })
    

    【讨论】:

    • 这最终成功了,谢谢。
    • 如果这篇文章解决了你的问题,你可以标记这篇文章。 :)
    猜你喜欢
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 2021-12-09
    • 2015-06-18
    • 2021-10-31
    • 1970-01-01
    相关资源
    最近更新 更多