简体   繁体   English

Discord Bot 从数组随机提示,不重复

[英]Discord Bot random prompt from array, no repeats

This is the problem I'm having.这是我遇到的问题。 This is meant to choose a word at random from the array when a user calls,prompt.这意味着当用户调用 prompt 时,从数组中随机选择一个词。 but then it'll only spit out that prompt and nothing else.但它只会吐出那个提示,没有别的。 It's supposed to pick randomly every time?prompt is called.它应该每次都随机选择?提示被调用。 How do I fix it?我如何解决它?

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

An example of what's going on, say the user calls.prompt and it spits out "Sephiroth".正在发生的事情的一个例子,假设用户调用.prompt 并且它吐出“Sephiroth”。 It will then ONLY spit out "Sephiroth" and nothing else until the bot is reset.然后它只会吐出“Sephiroth”,直到机器人被重置。

You're setting randomPrompt as a const one time when the script is initialized, and referring back to the same const each time.您在脚本初始化时将randomPrompt设置为const一次,并且每次都引用相同的const Instead, decide what the randomPrompt value will be when your message handler is executed:相反,决定在执行消息处理程序时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);

I've indented your code in accordance with generally accepted indentation standards for JavaScript. Your original code might be a bit more human-readable if it were indented correctly;我已经按照 JavaScript 的普遍接受的缩进标准缩进了您的代码。如果缩进正确,您的原始代码可能更易于阅读; you should consider using a proper IDE that will help you with this.您应该考虑使用合适的 IDE 来帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM