【问题标题】:How do I make my discord bot reply to a specific message with reply from a pool of replies?如何通过回复池中的回复让我的不和谐机器人回复特定消息?
【发布时间】:2021-05-07 04:35:49
【问题描述】:

几乎都在标题中。我希望我的机器人从回复池中随机回复。这是我目前所拥有的:

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

bot.on('ready', () =>{
    console.log('Just Chillin');
    bot.user.setActivity('Working' , { type: 'BotOn'}).catch(console.error);

}); 

function randomMessage(){
    var randomNumber = Math.round(Math.random()*2); // 0, 1 or 2
    switch(randomNumber){
        case 0: return 'Hello!';
        case 1: return 'Hi!';
        case 2: return 'Hola';
    }
}

bot.on('message' , message => {
    if(message.content.toLowerCase().includes('ping'))
        message.reply('pong')
    else if(message.content.toLowerCase().includes('Hello'))
            message.reply(randomMessage());
});

另外,我在 Java 方面没有太多经验,只有 R,所以我也想知道你会把第一段代码放在哪里。你会把它放在bot.on('message', message=> 的下方还是上方?此外,当我输入该代码时,什么也没有发生,其他一切都运行良好。

提前谢谢你。对于我缺乏 Java 知识,我深表歉意。

【问题讨论】:

  • 请您提供一个最低限度的工作示例吗?
  • 感谢您与我们联系。您能否详细说明“工作示例”的含义?
  • 一个最小的工作示例基本上只是为社区提供足够的代码来在他们自己的环境中重现您的项目,而不会泄露太多代码,也不需要我们(社区)猜测细节。
  • 好的,我马上更新。
  • 也许只是尝试将else if 更改为:else

标签: javascript discord discord.js bots


【解决方案1】:

当我想选择一条随机消息(如 8ball)时,我总是这样做:

定义不同的选项:

let options = ["Hey", "Hello there!", "Hey, it's me!", "NOT NOW IM BUSY"];

随机选择一条消息:

let result = options[Math.floor(Math.random() * options.length)];

记录结果:

console.log(result);

【讨论】:

    猜你喜欢
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2022-01-09
    • 2020-06-20
    • 2021-11-19
    • 2022-11-20
    • 2021-07-01
    相关资源
    最近更新 更多