首页 > 解决方案 > Discord Bot 错误未处理PromiseRejectionWarning

问题描述

我正在制作一个Discord Bot在语音频道中自动播放音乐。但是当我使用机器人的命令“cm!play”时出现错误 UnhandledPromiseRejectionWarning。如果有人能够帮助我,那就太好了。我在 Google 上发现使用 .catch() 是必要的,但我这样做了,但效果并不好。这是我的代码:

const { Client, MessageEmbed }= require("discord.js"); // Module Discord Js
const client = new Client(); // On ouvre le client du bot
var prefix = "cm!"; // Préfix des commandes du bot

client.on('ready', () => {
    console.log('Chill Musik Bot is running !');
    console.log('Le bot se trouve dans ' + client.guilds.cache.size + ' serveurs');
    let clientguilds = client.guilds.cache;
    console.log(clientguilds.map(g => g.id) || "None");
    console.log(clientguilds.map(g => g.name) || "None");

    client.user.setPresence({
        status: 'online',
        activity: {
            name: 'Use cm!play ~ Listen on www.chillmusik.ml',
            type: 'PLAYING',
            url: 'https://www.chillmusik.ml/'
        }
    });
});

var bot = client.users.cache.find(user => user.id === '797101244182298635');

// Lorsqu'un message est détecté
client.on('message', message => {
        
    if (!message.guild) return;
    if (!message.content.startsWith(prefix) || message.author.bot) return;
        
    // Gestion des arguments de commandes
    const args = message.content.slice(prefix.length).split(' ');
    const command = args.shift().toLowerCase();
        
    // Si la commande est "on"
    if (command === 'play') {
        if (message.member.hasPermission("MANAGE_CHANNELS")) {
            const channel = message.member.voice.channel;
            channel.join().then(connection => {      
                connection.play('https://eu1.fastcast4u.com/proxy/chillmusik?mp=/1.mp3');
                console.log('There is a connection to a channel.');
            }).catch(console.error);
          }
        }
        
        // Si la commande provient d'un bot administrator 
        if (message.author.id == '602052289263435787') {
            // p:stats
            if (command === 'stats') {
                message.channel.send('Chill Musik Bot is running !');
                message.channel.send('Le bot se trouve dans ' + client.guilds.cache.size + ' serveurs');
                message.delete();
            }
            
            //p:send
            else if (command === 'send') {
                message.delete();
                var messageContent = '';
                for (var i in args) {
                    messageContent += args[i] + ' ';
                }
                message.channel.send(messageContent);
            }
            
            else if (command === 'servers') {
                let clientguilds = client.guilds.cache;
                message.channel.send(clientguilds.map(g => g.id) || "None");
                message.channel.send(clientguilds.map(g => g.name) || "None");
            }
            
            else if (command === "allinvites") {
                message.delete();
                var guildList = client.guilds.cache;
                try {
                    console.log("Liste des invitations de serveurs : ");
                    guildList.map(guild => {
                            guild.fetchInvites().then(invites => {
                                invites.map(invite => {
                                    console.log("\n Server named : " + guild.name);
                                    message.channel.send("\n Server named : " + guild.name);
                                    message.channel.send(invite.code);
                                    console.log(invite.code);
                            });
                        });
                    });
                    console.log("Tous les codes d'invitations ont été affichés")
                } catch (err) {
                    console.log("Impossible d'afficher les codes invitations")
                    message.channel.send("Impossible d'afficher les codes");
                }
            }
        }
});

client.login('HIDDEN');

这里的错误信息:

(node:35714) UnhandledPromiseRejectionWarning: Error: send EPERM 5.200.14.147:50004
    at doSend (dgram.js:692:16)
    at defaultTriggerAsyncIdScope (internal/async_hooks.js:429:12)
    at afterDns (dgram.js:638:5)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:35714) 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:35714) [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.
Error [VOICE_CONNECTION_TIMEOUT]: Connection not established within 15 seconds.
    at VoiceConnection.authenticateFailed (/home/boma0924/nodevenv/nodejs_app/chillmusik/14/lib/node_modules/discord.js/src/client/voice/VoiceConnection.js:303:26)
    at /home/boma0924/nodevenv/nodejs_app/chillmusik/14/lib/node_modules/discord.js/src/client/voice/VoiceConnection.js:324:61
    at Timeout.<anonymous> (/home/boma0924/nodevenv/nodejs_app/chillmusik/14/lib/node_modules/discord.js/src/client/BaseClient.js:83:7)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7) {
  [Symbol(code)]: 'VOICE_CONNECTION_TIMEOUT'
}

标签: node.jsdiscorddiscord.js

解决方案


推荐阅读