【问题标题】:Problem: "user" is undefined (Unban Command)问题:“用户”未定义(Unban 命令)
【发布时间】:2022-07-21 15:39:31
【问题描述】:

我目前正在尝试创建一个 unban 命令,但是,“用户”未定义,我不确定解决方案是什么或如何定义它。我检查了我可能犯的错别字和错误,但我所看到的似乎没有。

(另外,我正在关注 reconlx 的 Kick/Ban/Unban 命令教程,其中包含我自己的一些嵌入)。

unban.js 的代码,错误如下。

const { Client, CommandInteraction, MessageEmbed } = require("discord.js");

module.exports = {
    name: "unban",
    description: "Unbans a user",
    userPermissions: ["BAN_MEMBERS"],
    options: [
        {
            name: "userid",
            description: "User ID of the offender",
            type: "STRING",
            required: true,
        },
    ],
    /**
     * @param {Client} client 
     * @param {CommandInteraction} interaction 
     */
    run: async (client, interaction) => {
        const userID = interaction.options.getString("userid");

        const unbanEmbed = new MessageEmbed()
            .setTitle(`${user.tag} has been unbanned!`)
            .setFooter({ text: "Unban Successful!"})

        const unbanNull = new MessageEmbed()
            .setTitle("Invalid ID!")
            .setFooter({ text: "Error: Invalid User ID" })
        
        interaction.guild.members
            .unban(userID)
            .then((user) => {
                interaction.followUp({ 
                    embeds: 
                        [unbanEmbed], 
                });
            })
            .catch(() => {
                interaction.followUp({
                    embeds:
                        [unbanNull],
                });
            });
    },
};

错误

ReferenceError: user is not defined
    at Object.run (C:\Users\admin\Desktop\Tonkotsu\SlashCommands\moderation\unban.js:23:26)
    at Client.<anonymous> (C:\Users\admin\Desktop\Tonkotsu\events\interactionCreate.js:27:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    在此处使用用户获取。它将允许您从 ID 中检索用户数据。

    const userID = interaction.options.getString("userid");
    const user = await interaction.client.users.fetch(userID);
    
    const unbanEmbed = new MessageEmbed()
    

    【讨论】:

      【解决方案2】:

      正如您在错误中看到的那样,导致问题的行是第 23 行。如果您查看这一行,您似乎在声明之前使用了该变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-25
        • 1970-01-01
        • 2021-03-20
        • 2021-09-15
        • 2020-08-12
        • 2020-10-01
        • 1970-01-01
        • 2022-01-20
        相关资源
        最近更新 更多