[英]How can I get my discord.js bot to respond to DM's, with different messages?
I am coding a bot in discord.js, and I have managed to make the bot respond to a DM, but it only responds with 1 message.我在 discord.js 中编写一个机器人,我已经设法让机器人响应 DM,但它只响应 1 条消息。
I want it to be able to respond with one thing first, like "Hello", and then the second time when the same person DM's it, I want it to reply "how are you?", and then loop / stop once all responses have been used.我希望它能够首先回复一件事,比如“你好”,然后第二次当同一个人 DM 回复它时,我希望它回复“你好吗?”,然后在所有回复后循环/停止被用过。
Here's the code for it to respond to DM's:这是它响应 DM 的代码:
if (msg.channel.type == "dm") {
msg.author.send("Hey!");
return;
}
})
Thanks:D感谢:D
You can just save how often you messaged the author, however you'd need a database for that if you plan on having this going for a long time (since all saves at runtime are deletd when restarting the bot), one option would be a dictionary-wise apporach:您可以只保存向作者发送消息的频率,但是如果您计划长时间使用此功能,则需要一个数据库(因为在重新启动机器人时会删除运行时的所有保存),一个选项是字典式方法:
//at the top of your file
let msgs = { }; //this is where you will dynamically store the amount
const dms = ["hey!", "How are you?", "Nice to meet you!", "..."]; //array of answers
...
if (msg.channel.type == "dm") {
let count = msgs[message.author.id]; //get the amount of messages sent
if(!count) count = msgs[message.author.id] = 0; //set to 0 if non sent before
msgs.author.send(dms[count]); //send the corresponding message => fetched from array
msgs[message.author.id]++; //increase message sent count by one
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.