尝试执行随机命令而无需重新启动机器人

Posted

技术标签:

【中文标题】尝试执行随机命令而无需重新启动机器人【英文标题】:Trying to execute a random command without having to restart bot 【发布时间】:2021-09-01 21:51:50 【问题描述】:

我试图点击这个链接:Random Message Reply Discord.js 2020

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

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

除了 discord.js、dotenv 和 nodemon,我没有导入任何其他文件。我不知道执行应该做什么。我只是盲目地点击链接。

这是我的代码:

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()
        
)```

【问题讨论】:

【参考方案1】:

我能够通过玩弄它来解决这个问题。我通过移动实现了我的目标

通过移动我的

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

在我的 if(... command) 中,只要满足 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()
    
)

【讨论】:

【参考方案2】:

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

【讨论】:

除了 discord.js、dotenv 和 nodemon 之外,我没有导入任何其他文件。我不知道执行应该做什么。我只是盲目地点击链接。 我认为你只需要评论那行并尝试。 我不知道你评论那一行是什么意思。 执行已经在我的代码中了,所以我可能真的没有注意到这里。它就在底部。在我的 if(message.channel.id 命令中 && message.author.id

以上是关于尝试执行随机命令而无需重新启动机器人的主要内容,如果未能解决你的问题,请参考以下文章

(Rails)重新加载“lib”文件而无需重新启动服务器......? [复制]

当机器人重新启动或打开时,执行特定功能

在 Spring Boot 应用程序中重新加载 bean 配置 xml 文件而无需重新启动

js代码在导航路线后停止工作而无需重新启动页面

重新加载Spring Boot 上的更改,而无需重新启动服务

在 DEB 安装后将用户添加到组而无需重新启动