Discord Bot 在 dm 中提及用户

Discord Bot Mention a user in dm

所以我希望机器人@他正在与之交谈的用户因为 ${member}(我在 youtube 上看到了)不起作用所以我想问我必须写什么才能让他写 "Hello @(the users name)..." 请记住他是作为 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!');
});

问题不在于成员,而是在于 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)

刚开始我遇到了同样的问题,这应该可以帮助你解决问题:

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