来自数组的Discord Bot随机提示,无重复

时间:2020-08-18 18:42:00

标签: javascript node.js discord

这是我遇到的问题。这是为了在用户调用!prompt时从数组中随机选择一个单词,但是这样只会吐出该提示而已。应该在每次调用提示时随机选择。我该如何解决?

const Discord = require('discord.js');

const bot = new Discord.Client();

const PREFIX = '!';


const token = 'notforstackoverflow';
bot.on('ready', () =>{
    console.log('This bot is online!');
})



var prompts = ["Cloud", "Tifa", "Sarge", "Chocobo",
"Sephiroth",
"Materia",
"Lifestream",
"Mako",
"Ancient",
"Cetra", 
"Lost number",
"Ultimate weapon",
"Point of no return",
"Eorzea cafe",
"Artnia",
"Healing Spring",
"Unseen Realm",
"Sarnoia civil war",
"Dimensional shift",
"Day of conjuction",
"Disguise",
"Pirate",
"Monster",
"Astos",
"Four Fiends",
"Matoya",
"Sarah",
"Dancing Girl",
"Honey Bee",
"Rydia",
"Tidus",
"Tuna",
"Rikku",
"Aerith",
"Jenova",
"Jessie",
"Vincent Valentine",
"Zack Fair",
"Butch",
"Kotch",
"Weapons",
"Nibelheim",
"SOLDIER"];
const randomPrompt = prompts[Math.floor(Math.random() * prompts.length)];

bot.on('message', message=>{
    let args = message.content.substring(PREFIX.length).split(" ");
    switch(args[0]){
case 'prompt':
message.channel.send(randomPrompt); 
break;  }
})



bot.login(token);

发生了什么事的示例,例如用户调用!prompt,它弹出“ Sephiroth”。然后,它只会吐出“ Sephiroth”,直到重置机器人为止。

1 个答案:

答案 0 :(得分:0)

初始化脚本时,您一次将randomPrompt设置为const,并且每次都返回相同的const。相反,请确定执行消息处理程序时randomPrompt的值是什么:

const Discord = require('discord.js');

const bot = new Discord.Client();

const PREFIX = '!';


const token = 'notforstackoverflow';
bot.on('ready', () => {
    console.log('This bot is online!');
})



var prompts = ["Cloud", "Tifa", "Sarge", "Chocobo",
    "Sephiroth",
    "Materia",
    "Lifestream",
    "Mako",
    "Ancient",
    "Cetra",
    "Lost number",
    "Ultimate weapon",
    "Point of no return",
    "Eorzea cafe",
    "Artnia",
    "Healing Spring",
    "Unseen Realm",
    "Sarnoia civil war",
    "Dimensional shift",
    "Day of conjuction",
    "Disguise",
    "Pirate",
    "Monster",
    "Astos",
    "Four Fiends",
    "Matoya",
    "Sarah",
    "Dancing Girl",
    "Honey Bee",
    "Rydia",
    "Tidus",
    "Tuna",
    "Rikku",
    "Aerith",
    "Jenova",
    "Jessie",
    "Vincent Valentine",
    "Zack Fair",
    "Butch",
    "Kotch",
    "Weapons",
    "Nibelheim",
    "SOLDIER"
];

bot.on('message', message => {
    let args = message.content.substring(PREFIX.length).split(" ");
    switch (args[0]) {
        case 'prompt':
            const randomPrompt = prompts[Math.floor(Math.random() * prompts.length)];
            message.channel.send(randomPrompt);
            break;
    }
})

bot.login(token);

我已经按照公认的JavaScript缩进标准对您的代码进行了缩进。如果正确缩进,您的原始代码可能更易于阅读。您应该考虑使用适当的IDE来帮助您。