javascript 当用户在句子中说出一个词时,Discord Bot 发送消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48845195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:19:14  来源:igfitidea点击:

Discord Bot send a message when the user says a word in a sentence

javascriptbotsdiscord

提问by Ressii

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.on('message', (message) => {
    if(message.content == 'ping') {
        message.reply('Pong');
    }
});

bot.login('my token is here');

I want my bot to say something when the user says something that contains 'server IP' I currently have this code, but it only replies when I send 'ping', how can I make it where it detects when the word 'ping' is in the sentence?

我希望我的机器人在用户说一些包含“服务器 IP”的内容时说些什么我目前有这个代码,但它只在我发送“ping”时回复,我怎样才能让它检测到“ping”这个词在句子中?

回答by newbie

You can use .includes(...)to check if the content(returns a string) of a messagecontains 'ping'.

您可以使用.includes(...)检查contenta的(返回字符串)是否message包含“ping”。

Like so:

像这样:

if(message.content.includes('ping')) {
    message.reply('Pong');
}