首页 > 解决方案 > 为什么我的命令只对 1 人有效

问题描述

我不确定发生了什么,我似乎在代码中找不到任何会导致这种情况的东西,但突然间只有一个人可以使用命令。除了那个人之外,任何人都不能使用为某些权限设置的命令。它被困住的人对服务器来说也是相当新的,这很奇怪。它似乎在我们的测试服务器上运行良好,但在主要服务器上它只适用于 1 人。

下面是我有命令处理程序的主文件

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

var used = false;

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

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

// This is the start of the main function when the bot is turned on
client.on('message', message => {

    if (message.author.bot || !message.guild) return;

    const words = message.content.toLowerCase();
    if (words.includes('shalomi')) {
        setTimeout(function() {
          message.channel.send(`Shut up ${message.author}`);
        }, 1500);
    }

    if (words.includes(' bum ')) {
        setTimeout(function() {
          message.channel.send('Are we talking about <@458068171241553921>?!');
        }, 1500);
    }

    if (words == 'prefix') {
        message.channel.send(`The current prefix is "${prefix}".`);
    }

    if (used) return;
    else {
        if (words == 'f') {
            message.channel.send('F');
            used = true;
            setTimeout(() => {
                used = false;
            }, 1000 * 20);
        }
    }

    for (let x = 0; x < profanities.length; x++) {
        if (message.member.roles.some(role => role.id === '483641589193900043')) return; 

        else {
            if (message.content.toUpperCase().includes(profanities[x].toUpperCase())) {
                message.channel.send('Oooooooh you said a bad word!');
                client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
                message.delete();
                return;
            }
        }
    }
    // The bot will not respond if there is no prefix,
    // the user that typed it was a bot,
    // or if it was not sent from in the server
    if (!message.content.startsWith(prefix) || message.author.bot || !message.guild) return;

    // Creates the arguments variable and separates it with a space
    // and creates the command variable
    const args = message.content.slice(prefix.length).split(' ');
    const commandName = args.shift().toLowerCase();

    if (!client.commands.has(commandName)) return;

    const command = client.commands.get(commandName);

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

    try {
        command.execute(message, args);
    }
    catch (error) {
        console.error(error);
        message.channel.send('There was an error trying to execute that command!\nCheck the console for details.');
    }
});
// This logs in the bot with the specified token found in config
client.login(token);

标签: javascriptdiscorddiscord.js

解决方案


所以这是我几天前完成的最新命令。当我第一次执行此命令时,我忘记将逗号和分号放在底部(现在在那里),就像我应该做的那样。当我在普通服务器上测试它时,第一个使用命令的人现在是唯一可以使用每个命令的人。

忘记这些标点符号会是现在每个命令只对首先使用“拥抱”命令的人有效的原因吗?我尝试删除该命令并重做它,认为它会删除可能保存到变量中的信息。就像它可能只是在寻找一位作者,因为它从未关闭过循环?我不确定

module.exports = {
    name: 'hug',
    description: 'Used to hug everyone or mention a user to hug them specifically.',
    execute(message, args) {

        args = message.mentions.users.size;

        if (!args) {
            message.delete();
            return message.channel.send(`${message.author} gives a big ol' hug to everyone!`);
        }
        else {
            message.delete();
            return message.channel.send(`${message.author} sends love to ${message.mentions.users.first()} with a hug!`);
        }
    },
};

推荐阅读