首页 > 解决方案 > 带有 Discord.js 的 AlertBot 用于在线游戏

问题描述

我正在尝试制作一个 Discord.js 机器人,其他人可以输入他们希望如何收到警报,然后在该特定警报的警报消失后清除数组,同时仍保留其他警报。

例如,如果 user1 键入“!s 1,1” user2 types “!s 1,1” user3 types “!s 1,2” 那么如果有人键入“!t 1,1”,则只有 user1 和 user2 会从bot 同时从数组中删除该单元格,同时保留“1,2”单元格以防警报稍后出现。

到目前为止,这是我的代码(如您所见,我到处都是。自从我使用 javascript 以来已经有好几年了......):

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const user = message.author;
    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();
    const ambushStack = user.tag.keyArray();

    if (command === 's') {
        if (!args.length) {
            return message.channel.send(`You didn't provide any arguments, ${message.author}!`);
        }
        message.react('');
        ambushStack.split(`${user.tag}`);
        message.channel.send(`Set At: ${args} with ${ambushStack}`);
        
        if (command === 't') {
            if (!args.length) {
                return message.channel.send(`You didn't provide any arguments, ${message.author}!`);
            }
            message.channel.send(`TRIGGERED: <@${ambushStack.user}>`);
        }
    }

});

所以在 Discord 中,它显示了这一点:

[7:32 AM] user1: !s location1
[7:32 AM] BOT TriggerBot: Set At: location1 with user#1234
[7:32 AM] user2: !s location1
[7:32 AM] BOT TriggerBot: Set At: location1 with user#2345
[7:35 AM] user1: !t location1
[7:35 AM] BOT TriggerBot: TRIGGERED: undefined

此外,如果其他用户设置在 location1,它不会说该位置的所有用户,只说命令的作者。

来自 cmd 的错误代码:

(node:18392) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Access
    at RequestHandler.execute (C:\Users\*\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async RequestHandler.push (C:\Users\*\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:18392) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:18392) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

标签: javascriptarraysdiscorddiscord.js

解决方案


推荐阅读