简体   繁体   English

如何让我的 discord.js 机器人用不同的消息回复 DM?

[英]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.

相关问题 discord.js 你能记录 Bot 每个 DM 获得的消息吗? - discord.js Can you log the messages that the Bot get per DM? 如果斜线命令有效,我怎样才能让我的 discord.js v14 机器人停止说“应用程序没有响应”? - How can i get my discord.js v14 bot to stop saying "The application did not respond" if the slash command works? 如何让discord.js bot在用户响应后才回复一系列DM消息? - How to get discord.js bot to reply to a series of DM messages only after user response? 我做了一个Discord.js机器人,但是当我告诉DM某人时,它会在DM中键入该人的ID。 - I made a Discord.js bot, but when i tell it to DM someone, it types that person's ID in the DM 如何让我的 Discord.JS 机器人在 DM 中回复我 - How do I have my Discord.JS bot respond to me in DMs Discord JS:如何删除机器人在 DM 中发送的所有消息? - Discord JS: How do I delete all messages the bot has sent in DM's? Discord 机器人不响应我的消息(js) - Discord bot does not respond to my messages (js) 如何使 discord.js bot dm 向特定人员发送消息? - How to make a discord.js bot dm a message to a specific person? 为什么我的机器人无法获取频道? 不和谐.js - Why can't my bot get channels? discord.js 我怎样才能实现!leaveguild<guildid> 命令(离开指定的公会)进入我的 Discord (discord.js) 机器人?</guildid> - How can I implement a !leaveguild <guildID> command (that leaves the specified guild) into my Discord (discord.js) bot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM