Discord Bot在dm中提及用户

时间:2020-04-14 10:51:58

标签: javascript bots discord mention dm

所以我希望该机器人@与他交谈的用户,因为$ {member}(我在youtube上看到的)不起作用,所以我想问一下我必须写些什么,以便他写“ Hello @(the用户名)...”,请记住,他将其写为dm。

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

client.on('ready', () => {
    console.log('This Bot is online!');
})

client.login(token);

client.on('guildMemberAdd', member => {
    member.send('Hello ${member}, welcome to the PotatoHost Server!');
});

2 个答案:

答案 0 :(得分:1)

问题不在于成员,而是client.login(),如果有代码,问题应该总是在结尾!

我希望这会对您有所帮助。祝你有美好的一天!

编辑:此外,一些成员已锁定dm,因此您应该使用try-catch函数,如果遇到错误,则在聊天室中发送欢迎消息。

try-catch函数的工作原理如下:

try{
member.send("message here")
}catch(error){
member.guild.channels.get("here the Id of the channel you want to send the welcome message in").send("message here")
}

如果您不喜欢在服务器的通道中发送消息的想法,请改成:

console.log(error)

答案 1 :(得分:1)

然后我遇到了同样的问题,这应该可以帮助您解决问题:

client.on("guildMemberAdd", async member => {
const dmErr = false;
try {
await member.send()
} catch (error) {
dmErr = true;
} if (dmErr === true) {
member.guild.channels.get("Id of the channel here").send()
}
}); 
相关问题