如何使不和谐的机器人重复用户消息的特定部分

时间:2020-09-13 13:18:33

标签: javascript discord discord.js

我正在尝试制作一个不和谐的bot,该bot发送消息,然后重复用户提供的代码。 例如:

用户:!play <code>

Bot:@everyone a new game has been started with the code <code>


我还希望它记住该变量,直到将其重置为止,这样才有可能:

用户:!code

Bot: the current game code is <code>

是否可以这样做,如果可以,怎么做?我无法在简单的.js脚本中找到任何能显示出我正在寻找的内容

这是我现在拥有的:

client.on('message', (msg) => {
 if (msg.content === '!play') {
  msg.reply('@everyone A new game has been started with the code');
 }
});

client.on('message', (msg) => {
 if (msg.content === '!code') {
  msg.reply('The current game code is');
 }
});

2 个答案:

答案 0 :(得分:0)

您可以这样做:

var code = '';

client.on('message', (msg) => {
 if (msg.content.startsWith('!play ')) {
  code = msg.content.substr(6); // Remove first 6 characters of message
  msg.reply(`@everyone A new game has been started with the code ${code}`);
 }
 if (msg.content.startsWith('!code ') && code.length > 0) {
  msg.reply(`The current game code is ${code}`);
 }
});

答案 1 :(得分:0)

哦,它也给我TypeError:msg.startsWith不是函数

使用message.content.startsWith()

这可能对!play有效,但是我希望!code回复!play中已经定义的代码

是的,不用命令就可以获取消息的参数

let args = message
.content // We need the content of the message
.split(' ') // Then we creates an array with the message separated with a space.
.slice(1) // We remove the command from the array

然后您可以根据需要使用args。

message.channel.send(args.join(' ')) // We join with a space to transform the array with a string