gpt4 book ai didi

javascript - 如何让 Discord 机器人检查它是否正在被私信,然后使用 Discord.js 回复所述私信?

转载 作者:行者123 更新时间:2023-12-01 02:32:56 24 4
gpt4 key购买 nike

如何让我的机器人检查用户是否正在向机器人发送私信,然后让机器人响应所述消息?

到目前为止我使用过的代码,部分工作正常,例如“sendMessage、console.log 和 bot.channels.get 等,但问题是它得到了运行该特定部分的正确语句,代码:

// "Help" command for admin assistance
bot.on('message', (message) => {
if(message.channel.DMChannel) {
// Check if the word sent is "help"
if(message.content.toLowerCase() == 'help') {
console.log('User ' + member.user.username + ' is requesting assitance. Now alerting staff members!');
bot.channels.get("397707437781680130").send('**' + member.user.username + '**, is requesting staff assitance. Now alerting staff members!')
bot.sendMessage('I helped you! A staff member will respond soon!');
} else {
bot.sendMessage('You can only ask for help by DM, please type "help" if you need assistance!');
}
}
});

非常感谢您的指点。

最佳答案

Discord.js 有一个内置的 channel.type,您可以使用它来检查 DM channel (也称为 PM channel )。

考虑到这一点,您的代码应该类似于:

bot.on('message', (message) => {
if(message.channel.type == "dm") {
//what should happen on a dm
} else {
//what should happen if the channel is not a dm
}
});

关于javascript - 如何让 Discord 机器人检查它是否正在被私信,然后使用 Discord.js 回复所述私信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48176232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com