簡體   English   中英

如何讓不和諧機器人重復用戶消息的某個部分

[英]How to make a discord bot repeat a certain part of a user's message

我正在嘗試制作一個發送消息的不和諧機器人,然后重復用戶提供的代碼。 例如:

用戶: !play <code>

機器人: @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');
 }
});

你可以這樣做:

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}`);
 }
});

哦,它也給了我 TypeError: msg.startsWith is not a function

使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM