前缀正在更改

时间:2020-06-13 09:55:50

标签: discord discord.js

因此,在我的config.json文件中,我将前缀设置为“!”。我正在朋友服务器中测试该机器人,并且该前缀与其他机器人发生冲突。我将其更改为“;”。但是现在两个前缀都在起作用!和”;”。我也保存了所有文件并重新启动了节点,但是命令仍然响应“!”。我想念什么吗? 我的文件系统的结构是- 1. index.js 2. config.json 3.命令文件夹(命令文件)

index.js

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

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

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); //returns an array of all file anmes in a dir ['ping.js', beep.js, ....]; the filter is used to get only .js files.
const commandFiles_mod = fs.readdirSync('./commands/mod').filter(file => file.endsWith('.js')); //mod folder
const commandFiles_info = fs.readdirSync('./commands/info').filter(file => file.endsWith('.js')); //info folder
const commandFiles_extras = fs.readdirSync('./commands/extras').filter(file => file.endsWith('.js')); //

//file system
for (const file of commandFiles) {
    const command = require(`./commands/${file}`); 

    // set a new item in the Collection
    // with the key as the command name and the value as the exported module
    client.commands.set(command.name, command); //set takes the file name and the path to the file. //command is the path to file and command.name is the name of the file
}

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

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

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

client.once('ready', () => {
    client.user.setActivity(`${prefix}help`)
    console.log(`Hello! ${client.user.username}-bot is now online!`)
});

client.on('message', message => {
    //command handling

    if (message.content === 'galaxy-prefix') {
        const prefix_embed = new Discord.MessageEmbed()
        .setTitle('Prefix')
        .setDescription(`Use this ahead of comamnds for usage: **${prefix}**`)
        .setColor('RANDOM')
        .setTimestamp()
        message.channel.send(prefix_embed);
    }

    if (message.content.startsWith(prefix) || !message.author.bot || !message.channel.type === 'dm') { //edit here
        const args = message.content.slice(prefix.length).split(/ +/); //caveats
        //console.log(args);
        const messageArray = message.content.split(/ +/);
        //console.log(messageArray);
        const cmd = args[1];
        //console.log(cmd);
        const commandName = args.shift().toLowerCase();
        //console.log(commandName);

        /*if (!message.content.startsWith(prefix) && !message.author.bot) {
            const messageArray = message.content.split(" ");
            console.log(messageArray);
        }*/

        /*for (i=0; i < profanity.length; i++) {
            if (messageArray.includes(profanity[i])) {

                message.delete();
                break;
            }
        }*/

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

        if (command.guildOnly && message.channel.type !== 'text') {
            return message.reply('I can\'t execute that command inside DMs!'); //cannot dm this command
        }
        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}\``; //usage check, in file as usage: '<...>'
            }
            return message.channel.send(reply);
        }
        if (!cooldowns.has(command.name)) {  //cooldown
            cooldowns.set(command.name, new Discord.Collection());
        }

        const now = Date.now();  
        //console.log(now);
        const timestamps = cooldowns.get(command.name);  // gets the collection for the triggered command
        const cooldownAmount = (command.cooldown || 3) * 1000;  //3 is default cooldown time, if cooldown isnt set in command files.

        if (timestamps.has(message.author.id)) {
            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(client, message, args);
        } catch (error) {
            console.error(error);
            message.reply('there was an error trying to execute that command!');

        }
    }
});


client.on('shardError', error => {
    console.error('A websocket connection encountered an error:', error);
});

process.on('unhandledRejection', error => {
    console.error('Unhandled promise rejection:', error);
});

client.login(token);

config.json

{
    "token": " ................. ",
    "prefix": ";"
}

1 个答案:

答案 0 :(得分:1)

您的机器人会真正响应任何事情...

if (message.content.startsWith(prefix) || !message.author.bot || !message.channel.type === 'dm')

这意味着如果邮件以前缀 OR 开头,则发件人将运行,而发件人不是机器人 OR ,则通道类型为DM。您应该尝试使用? -...。它们也可能起作用。

要使用and,请使用&&而不是||