为什么我的前缀在我的不和谐机器人上不起作用?

时间:2021-03-07 17:31:20

标签: javascript discord discord.js bots

为什么我的机器人没有检测到前缀?我放d没关系!或克!或其他信件。 我希望我的机器人只能使用前缀 购买我不知道为什么我写的内容无关紧要。 然后我使用: 名称:help, 别名:h, 调用其他命令。如果我把 g!help 或 f!help 放在它不能工作的时候它会工作。

const {
    Client,
    Collection,
    Message
} = require("discord.js");
class BotD extends Client {
    constructor() {
        super();
        this.discord = require("discord.js");
        this.fs = require("fs");
        this.path = require("path");
        this.ms = require("ms");
        this.commands = new Collection();
        this.timeouts = new Collection();
        this.config = {
            prefix: "d!",
        };
        const self = this;
    }
    commandHandler(path) {
        this.fs.readdirSync(this.path.normalize(path)).map((f) => {
            const file = require(this.path.join(__dirname, `../../`, path, f));
            this.commands.set(file.name, file);
        });
    }
    getCommand(cmd) {
        return this.commands.has(cmd) ? this.commands.get(cmd) : false;
    }
    start(token, path) {
        this.commandHandler(path);
        this.login(token);
        this.on("ready", () => {
            console.log(`${this.user.tag} is now online!`);
            this.user.setActivity('PREFIJO: d! | AYUDA: d!help', {
                type: 'WATCHING'
            });
        });
        this.on('guildMemberAdd', member => {
            member.send("BIENVENID@  <@" + member + "> ESPERAMOS QUE DISFRUTE DE SU ALOJAMIENTO. ANTES DE EMPEZAR LEA LAS REGLAS. MUCHAS GRACIAS!!!", {
                files: ["https://media.discordapp.net/attachments/767487587807395851/785507480313659433/istockphoto-1023674734-170667a-1.png"]
            });
        });
        this.on("message", async (message) => {
            const args = message.content.slice(this.config.prefix.length).trim().split(" ")[0];
            //arg.slice(0,3)
            const subarg = message.content.split(" ").slice(1);
            const cmd = args.toLowerCase();
            const command = this.getCommand(cmd);
            if (!command) return;
            if (command.timeout) {
                if (this.timeouts.has(`${command.name}${message.author.id}`)) return message.channel.send(this.embed({
                    description: `please wait ${this.ms(
                Date.now() -
                  this.timeouts.get(`${command.name}${message.author.id}`),
                { long: true }
              )}`,
                }));
                command.run(this, message, args, subarg).catch(console.error);
                this.timeouts.set(`${command.name}${message.author.id}`, Date.now() + command.timeout);
                setTimeout(() => {
                    this.timeouts.delete(`${command.name}${message.author.id}`);
                }, command.timeout);
            } else return command.run(this, message, args, subarg).catch(console.error);
        });
    }
    embed(data, message) {
        return new this.discord.MessageEmbed({
            ...data,
        }).setFooter(message.author.tag, message.author.displayAvatarURL());
    }
}
module.exports = BotD;

1 个答案:

答案 0 :(得分:0)

您的代码仅检查以确保命令存在,使用前缀的长度对字符串进行切片:(message.content.slice(this.config.prefix.length).split(' ')[0])
但它不会检查消息是否以前缀开头:(message.content.startsWith(this.config.prefix)).

只需在消息事件处理程序的开头添加条件

this.on("message", async (message) => {
    if (!message.content.startsWith(this.config.prefix)) return;
    const args = message.content.slice(this.config.prefix.length).trim().split(" ")[0];
    ...
});
相关问题