简体   繁体   English

尝试…catch 在 JavaScript (Discord.js) 中不起作用

[英]try…catch not work in JavaScript (Discord.js)

I'm working on a bot to fetch a user's avatar, I use try...catch for user mention detection, but it still throws an error.我正在开发一个机器人来获取用户的头像,我使用try...catch进行用户提及检测,但它仍然会引发错误。

And I tried a simple try...catch , and it throws an error SyntaxError: Identifier 'x' has already been declared :我尝试了一个简单的try...catch ,它抛出了一个错误SyntaxError: Identifier 'x' has already been declared

try {
    let x = 1;
    let x = 2; // Variable already been declared
} catch (e) {
    console.log(e)
}

Here's my code:这是我的代码:

// Get avatar by mention
try {
    client.users.fetch(msg.content.substr(prefix.length + 6, msg.content.length - prefix.length - 7)).then(result => {
        embeds.avatar
            .setTitle(`The avatar of ${msg.author.tag}`)
            .setImage(result.avatarURL({ dynamic: true }));
        msg.channel.send(embeds.avatar);
    });
} catch (e) {
    // Invalid user id
    logConsole('commandInvalidParam', msg);
    if (msg.content.length <= prefix.length + 14)
        embeds.commandInvalidParam.setDescription(`\`\`\`Invalid parameter at\n${msg.content}\n`);
    else
        embeds.commandInvalidParam.setDescription(`\`\`\`Invalid parameter at\n${msg.content.substr(0, prefix.length + 14)} ...\n`);
    for (let index = -4; index < prefix.length; index++)
        embeds.commandInvalidParam.setDescription(embeds.commandInvalidParam.description + ' ');
    embeds.commandInvalidParam.setDescription(embeds.commandInvalidParam.description + `^\`\`\`Type '${prefix}' for help`);
    msg.channel.send(embeds.commandInvalidParam);
}

IDE Using: IDE 使用:

Virtual Studio Code虚拟工作室代码

Version: 1.54.3 (user setup)版本:1.54.3(用户设置)

Commit: 2b9aebd5354a3629c3aba0a5f5df49f43d6689f8提交:2b9aebd5354a3629c3aba0a5f5df49f43d6689f8

Date: 2021-03-15T10:55:45.459Z日期:2021-03-15T10:55:45.459Z

Electron: 11.3.0 Electron:11.3.0

Chrome: 87.0.4280.141铬:87.0.4280.141

Node.js: 12.18.3 Node.js:12.18.3

V8: 8.7.220.31-electron.0 V8:8.7.220.31-电子.0

OS: Windows_NT x64 10.0.18363操作系统:Windows_NT x64 10.0.18363

For the simple try...catch, you have already declared the x variable in your code.对于简单的 try...catch,您已经在代码中声明了x变量。 No 2 variables can be named the same thing, so it's going to give you a syntax error.没有 2 个变量可以被命名为相同的东西,所以它会给你一个语法错误。 If you want to change the value of x , just do x = 2 .如果要更改x的值,只需执行x = 2

For your code, it can't find the user ID inside the fetch method.对于您的代码,它无法在 fetch 方法中找到用户 ID。 This could be because the prefix isn't the right length, or your substr method isn't getting the right parts of the method content.这可能是因为前缀长度不正确,或者您的 substr 方法没有获取方法内容的正确部分。 Either way, you should try declaring a variable with that substring, and then console.log ing the variable.无论哪种方式,您都应该尝试使用该 substring 声明一个变量,然后console.log对该变量进行记录。

So, Here is an example of what a userinfo command should look like.所以,这里是一个 userinfo 命令应该是什么样子的例子。 I quickly made this with an index.js file with discord.js installed.我用一个安装了 discord.js 的 index.js 文件快速完成了这个。

CODE:代码:


module.exports = {
    name: 'userinfo',
    description: 'Get info on a user',
    execute(message, client) {
        const split = message.content.split(/ +/);
        const args = split.slice(1);

        const user = getUserFromMention(args[0], client);
        message.channel.send(`Name: ${user.username}, ID: ${user.id}, Avatar: ${user.displayAvatarURL({ dynamic: true })}`);
        // Other possible information
        // USER OBJECTS @ discord.js
        // ${user.id} ${user.username} ${user.discriminator}(AKA THE LAST 4 NUMBERS OF A DISCORD USERS ACCOUNT) ${user.bot?} ${user.system?} ${user.mfa_enabled}
        // ${user.locale?} ${user.verified?} ${user.email?} ${user.flags?} ${user.public_flags} ${user.premium}
        // src https://discord.com/developers/docs/resources/user#get-user
    }
};

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

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