简体   繁体   English

如何让不和谐机器人重复用户消息的某个部分

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

相关问题 如何让discord.js bot在另一个频道中重复给它的消息? - How to make discord.js bot repeat message given to it in another channel? 在为 Discord 服务器创建 BOT 时,如何“scanf()”和“printf()”discord.js 中的用户消息? - How do I "scanf()" and "printf()" a user's message in discord.js when creating a BOT for Discord server? 如何让 discord 机器人发送抓取的消息? - How to make discord bot send scraped message? 如何使 discord 机器人消息的粗斜体? - How to make bold italics of a discord bot message? 如何让 discord 机器人编辑 dm 消息? - how to make a discord bot edit a dm message? 如何让我的 discord 机器人在特定时间或日期发送消息 - How do I make my discord bot to send a message on a certain time or date 我将如何 go 关于编辑 aa discord 机器人发送的消息? 但只有当具有特定角色的用户做出反应时? - How would I go about editing a message sent by a a discord bot? But only when reacted to by a user with a certain role? 当说某些话时,你如何让 Discord Bot 不发送消息(Node.js) - How do you make a Discord Bot not send a message when certain words are said (Node.js) 如何让我的不和谐机器人复制并发送每个用户写的消息? - How do I make my discord bot copy and message every user written message? Discord.js | 如何获取用户的输入并为其发送消息? - Discord.js | How to get user's input and make a message for it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM