[英]How to make a discord bot repeat a certain part of a user's message
I am trying to make a discord bot that sends a message, then repeats a code the user gives.我正在尝试制作一个发送消息的不和谐机器人,然后重复用户提供的代码。 For example:
例如:
User: !play <code>
用户:
!play <code>
Bot: @everyone a new game has been started with the code <code>
机器人:
@everyone a new game has been started with the code <code>
I also want it to remember this variable until it's reset so that this could be possible:我还希望它记住这个变量,直到它被重置,这样才有可能:
User: !code
用户:
!code
Bot: the current game code is <code>
Is it possible to do this, and if so, how?是否有可能做到这一点,如果有,如何做到? I cannot find anything showing what I'm looking for in a simple .js script
我在一个简单的 .js 脚本中找不到任何显示我正在寻找的内容
Here's what I have at the moment:这是我目前所拥有的:
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');
}
});
You could do it like this:你可以这样做:
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}`);
}
});
oh and also it gives me TypeError: msg.startsWith is not a function
哦,它也给了我 TypeError: msg.startsWith is not a function
Use message.content.startsWith()
使用
message.content.startsWith()
That might work for !play, however I want !code to reply the code that was already defined in !play
这可能适用于 !play,但是我希望 !code 回复已经在 !play 中定义的代码
True, to get the arguments of a message without a command it's是的,要在没有命令的情况下获取消息的参数,它是
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
then you can use args as you want.然后你可以根据需要使用 args 。
message.channel.send(args.join(' ')) // We join with a space to transform the array with a string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.