【问题标题】:discord.js prefix throwing error without command没有命令的 discord.js 前缀抛出错误
【发布时间】:2021-09-10 04:48:54
【问题描述】:

所以我的机器人前缀是.,只要用户发送.,机器人就会回复there was an error trying to execute that command!,这很烦人。我不知道如何让它不响应 . 的错误

这一行应该可以防止这种情况发生,但不是 const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName)); if (!command) return;

client.on("message", message => {
    const prefixes = ['.'];
    const prefix = prefixes.find(p => message.content.startsWith(p));
    if (!prefix) return;
 
    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const commandName = args.shift().toLowerCase();
    const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
    
    if (!command) return;

    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.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);
    }

    const { cooldowns } = client;

    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 || 0) * 1000;
    
    if (timestamps.has(message.guild.id)) {
        const expirationTime = timestamps.get(message.guild.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.guild.id, now);
    setTimeout(() => timestamps.delete(message.guild.id), cooldownAmount);

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

【问题讨论】:

  • 控制台记录什么?您的代码显示您在控制台记录错误
  • 这也不能阻止它,因为“。”被视为以“.”开头的字符串(即使整个字符串都是这样)

标签: discord.js


【解决方案1】:

您应该尝试检查,如果消息比前缀本身长,请尝试将const prefix 更改为:

const prefix = prefixes.find(p => message.content.startsWith(p) && message.content.length>p.length);

【讨论】:

    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多