尝试执行随机命令而不必重新启动机器人

Trying to execute a random command without having to restart bot

我试图遵循这个 link:

但是在尝试实现它时卡住了。基本上,使用 discord.js 库我有一个频道,我想允许一个词和一个词。当有人在所述频道中发布不是那个词的任何内容时,我希望机器人将其删除并发送从数组中随机选择的回复。我已经让它随机 select 一个索引项目,但它并没有真正保持随机。唯一一次回复更改是机器人是否重新启动。我希望它在每次事件发生后执行,但每次尝试时,我都会收到 ReferenceError: execute is not defined.

今天是我使用 Javascript 工作的第一天,因此我们将不胜感激任何简化此操作的方法。我反复测试过它,在我尝试使用 execute 函数之前它工作得很好。我正在使用 Visual Code Studio 进行编码,node.js 用于 运行 文件。

除了discord.js、dotenv 和nodemon 之外,我没有导入任何其他文件。我不知道 execute 应该做什么。我只是盲目地追随 link.

这是我的代码:

const client = new Client();

const bredfreply = [
    'Excuse me person. I see you are participating in the "bredf" channel in the INCREDIBLE Realm of Eden. Unfortunately, this channel is only limited to bredf. I cannot believe you would speak any other word. You should be ashamed!',
    'Did you do what I think you just did? EXCUSE ME, but bredf is only for bredf. You MUST say bredf because it is bredf and everyone knows bredf is only bredf and not whatever you thought it was. bredf. Only bredf.',
    'You person, are in trouble. For the fact that you are rebelling against the bredf, you have lost one cookie. I do not care if you already did not have cookie because now you have less than no cookies. You actually OWE cookies so pay up.',
    'Whatever you just did? That was not bredf.',
    'bredf bredf bredf bredf bredf bredf bredf bredf bredf bredf bredf bredf',
    'PLEASE only post bredf in the channel. You are scaring the other bredfs.',
    'Did you hear about bredf? It is this really cool thing that all the cool kids post in bredf. Oh, you must not be bredf because I do not see you in there. You should be bredf.',
    '*sniffles* I want bredf. Can you help me? Put it in the channel then because I do not think you can.',
    'I only like people that say bredf. Since you apparently are not, that must mean I do not like you. Hmph'
];

    client.on('ready', () => {
console.log(`${client.user.username} is ready to start the day!`);
});

client.on('message', (message) => {
if(message.channel.id == '840422367829164052' && message.content != 'bredf'
    && message.author.id != '854877244157853716')
        execute(message,)
            const randombredfreply = bredfreply[Math.floor(Math.random(1)) * bredfreply.length];
            message.author.send(randombredfreply);
            message.delete()
        }
)```

您是否从任何其他文件导入函数 execute?或者 execute 函数有什么作用?

我可以通过玩弄它来解决这个问题。我通过移动

实现了我的目标

通过移动我的

var randombredfreply = bredfreply[Math.floor(Math.random() * bredfreply.length]);

在我的 if(... 命令) 中,只要满足 if 参数,它就会重新计算。

这是最终代码:

if(message.channel.id == '840422367829164052' && message.content != 'bredf'
    && message.author.id != '854877244157853716') 
    {
        var randombredfreply = bredfreply[Math.floor(Math.random() * bredfreply.length)];
        message.author.send(randombredfreply);
        message.delete()
    }
})