【问题标题】:How can a Discord bot reply to only certain users?Discord 机器人如何只回复特定用户?
【发布时间】:2023-03-22 16:18:02
【问题描述】:

我正在寻找一种方法来制作只对某些用户做出反应或回复的 Discord 机器人。它可以通过角色或 ID 选择用户,但我似乎无法让它工作。这是我尝试过的:

if (message.author.id === 'myDiscordID') {
        message.reply('hello!').then(r => {
        });
    }

如果有帮助,我正在使用 Discord JS 进行编码。这是整个 index.js 文件:

const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();

client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', message => {

    if (message.author.id === 'myDiscordID') {
        message.reply('hello!').then(r => {
        });
    }

});
client.login(token);

文件运行良好,机器人上线,然后打印“Ready!”但是,对于控制台,其余代码似乎不起作用。

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    我看起来这必须有效。

    您确定您的服务器中有机器人并且有权阅读消息吗? 试试这个准备好的块

    client.once('ready', () => {
        console.log('Ready!');
        const guildList = client.guilds.cache.map(guild => guild.name)
        console.join(`Bot go online in ${guildList.length}`)
        console.log(guildList.join('\n'))
    }); 
    

    检查 user.id 或角色的一些解决方案包括:

    console.log(message.content) 用于检查此事件触发器。

    client.on('message', message => {
        console.log(message.content)
        if (message.author.id === '') {
            message.reply('author contain')
        }
    
        if (message.member.roles.cache.has('ROLE ID')) {
            message.reply('role contain')
        }
    
        if(message.member.roles.cache.some(role => ['ID1', 'ID2'].includes(role.id)) {
            message.reply('some role contain')
        }   
    
    });
    

    【讨论】:

      【解决方案2】:

      我也有这个问题。这是对我有用的解决方案。

      const userID = "The_user's_id";
      
      bot.on("message", function(message) {
          if(message.author.id === userID) {
              message.react('emoji name or id');
          }
      });
      

      注意:要查找用户的ID,只需右键单击他并按“复制ID”即可。

      【讨论】:

        猜你喜欢
        • 2020-11-08
        • 2021-10-05
        • 2019-08-26
        • 2021-11-08
        • 2020-01-25
        • 2020-10-11
        • 2023-02-26
        • 2021-04-30
        • 1970-01-01
        相关资源
        最近更新 更多