【问题标题】:Guild not defined discord.js公会未定义 discord.js
【发布时间】:2021-04-06 08:36:25
【问题描述】:

我最近在尝试为我的不和谐服务器编写机器人时遇到了一个问题。我收到此错误:

ReferenceError: guild is not defined

即使我已经在机器人的代码中定义了它(或者至少在我看来是这样的)。我需要做什么来定义它?我在这里错过了一些重要的步骤吗?我查看了有关此主题的其他一些问题,但似乎没有一个解决方案有效。

这是我的机器人代码:

const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

const cooldowns = new Discord.Collection();

client.once('ready', () => {
    console.log('Ready!');
});

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

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const commandName = args.shift().toLowerCase();
    const guild = client.guilds.cache.get('*guild token*');

    const command = client.commands.get(commandName)
        || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

    if (!command) return message.reply('this is not a valid command!');

    if (command.guildOnly && message.channel.type === 'dm') {
        return message.reply('I can\'t execute that command inside DMs!');
    }

    if (command.permissions) {
        const authorPerms = message.channel.permissionsFor(message.author);
        if (!authorPerms || !authorPerms.has(command.permissions)) {
            return message.channel.reply('You can not do this!');
        }
    }

    if (command.args && !args.length) {
        let reply = `You didn't provide any arguments, ${message.author}!`;

        if (command.usage) {
            reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
        }

        return message.channel.send(reply);
    }

    if (!cooldowns.has(command.name)) {
        cooldowns.set(command.name, new Discord.Collection());
    }

    const now = Date.now();
    const timestamps = cooldowns.get(command.name);
    const cooldownAmount = (command.cooldown || 3) * 1000;

    if (timestamps.has(message.author.id)) {
        const expirationTime = timestamps.get(message.author.id) + cooldownAmount;

        if (now < expirationTime) {
            const timeLeft = (expirationTime - now) / 1000;
            return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
        }
    }

    timestamps.set(message.author.id, now);
    setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

    try {
        command.execute(message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

client.login(token);

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    我看到你定义了公会,但我找不到你在哪里使用它。如果您能显示错误发生的确切位置,那就太好了。

    我注意到你“审查”了公会 ID:

    const guild = client.guilds.cache.get('*guild token*');
    
    

    这不是必需的,因为每个人都可以获得公会ID。正如我所提到的,它是一个 ID,而不是一个令牌。

    【讨论】:

    • 对不起,我可能应该把它包括在内。这是我从 discord.js 网站下的禁令命令:module.exports = { name: 'ban', description: 'Bans a user.', guildOnly: true, args: true, usage: '&lt;user&gt;', cooldown: 5, permissions: 'BAN_MEMBERS', execute(message, args) { const user = message.mentions.users.first(); guild.members.ban(user); }, }; 不过,该错误也发生在其他带有公会常量的命令中。
    • 是的,您在禁止命令的命令代码中使用公会。但是您的索引中的guild 是不可见的。或者换句话说,错误是如何表示的:guild 未定义(不在您的禁令命令代码中)。
    猜你喜欢
    • 2020-12-24
    • 2018-06-24
    • 2021-12-04
    • 2021-05-30
    • 2021-10-06
    • 2021-06-25
    • 2017-07-03
    • 2020-07-05
    • 2021-02-05
    相关资源
    最近更新 更多