使用discord.js在Discord机器人上工作时,tempmute无法正常工作

时间:2019-11-09 17:37:23

标签: javascript discord discord.js

使用带有Visual Studio代码的JavaScript制作不和谐的bot,但由于某种原因出错。我将向您展示最相关的代码。

总体上试图使临时静音功能正常工作,我想将其添加到您单击!help时弹出的可用命令中。表格看起来像这样:

!help

classes I'm working with

这是index.js:


const { Client, Collection } = require("discord.js");
const { config } = require("dotenv");
const fs = require("fs");

const client = new Client({
    disableEveryone: true
});

client.commands = new Collection();
client.aliases = new Collection();

client.categories = fs.readdirSync("./commands/");

config({
    path: __dirname + "/.env"
});

["command"].forEach(handler => {
    require(`./handlers/${handler}`)(client);
});

client.on("ready", () => {
    console.log(`Hi, ${client.user.username} is now online!`);

    client.user.setPresence({
        status: "online",
        game: {
            name: "you get boosted❤️",
            type: "Watching"
        }
    }); 
});

client.on("message", async message => {
    const prefix = "!";

    if (message.author.bot) return;
    if (!message.guild) return;
    if (!message.content.startsWith(prefix)) return;
    if (!message.member) message.member = await message.guild.fetchMember(message);

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const cmd = args.shift().toLowerCase();

    if (cmd.length === 0) return;

    let command = client.commands.get(cmd);
    if (!command) command = client.commands.get(client.aliases.get(cmd));

    if (command) 
        command.run(client, message, args);
});

client.login(process.env.TOKEN);

这里有节制:


bot.on('message', message => {
    let args = message.content.substring(PREFIX.length).split(" ");

switch (args[0]) {
    case 'mute':
        var person  = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]));
        if(!person) return  message.reply("I CANT FIND THE USER " + person)

        let mainrole = message.guild.roles.find(role => role.name === "Newbie");
        let role = message.guild.roles.find(role => role.name === "mute");


        if(!role) return message.reply("Couldn't find the mute role.")


        let time = args[2];
        if(!time){
            return message.reply("You didnt specify a time!");
        }

        person.removeRole(mainrole.id)
        person.addRole(role.id);


        message.channel.send(`@${person.user.tag} has now been muted for ${ms(ms(time))}`)

        setTimeout(function(){

            person.addRole(mainrole.id)
            person.removeRole(role.id);
            console.log(role.id)
            message.channel.send(`@${person.user.tag} has been unmuted.`)
        }, ms(time));



    break;
}


});

这是列出所有命令的help.js


const { RichEmbed } = require("discord.js");
const { stripIndents } = require("common-tags");

module.exports = {
    name: "help",
    aliases: ["h"],
    category: "info",
    description: "Returns all commands, or one specific command info",
    usage: "[command | alias]",
    run: async (client, message, args) => {
        if (args[0]) {
            return getCMD(client, message, args[0]);
        } else {
            return getAll(client, message);
        }
    }
}

function getAll(client, message) {
    const embed = new RichEmbed()
        .setColor("RANDOM")

    const commands = (category) => {
        return client.commands
            .filter(cmd => cmd.category === category)
            .map(cmd => `- \`${cmd.name}\``)
            .join("\n");
    }

    const info = client.categories
        .map(cat => stripIndents`**${cat[0].toUpperCase() + cat.slice(1)}** \n${commands(cat)}`)
        .reduce((string, category) => string + "\n" + category);

    return message.channel.send(embed.setDescription(info));
}

function getCMD(client, message, input) {
    const embed = new RichEmbed()

    const cmd = client.commands.get(input.toLowerCase()) || client.commands.get(client.aliases.get(input.toLowerCase()));

    let info = `No information found for command **${input.toLowerCase()}**`;

    if (!cmd) {
        return message.channel.send(embed.setColor("RED").setDescription(info));
    }

    if (cmd.name) info = `**Command name**: ${cmd.name}`;
    if (cmd.aliases) info += `\n**Aliases**: ${cmd.aliases.map(a => `\`${a}\``).join(", ")}`;
    if (cmd.description) info += `\n**Description**: ${cmd.description}`;
    if (cmd.usage) {
        info += `\n**Usage**: ${cmd.usage}`;
        embed.setFooter(`Syntax: <> = required, [] = optional`);
    }

    return message.channel.send(embed.setColor("GREEN").setDescription(info));
}

错误:

Error message, bot not defined.

总体上试图使临时静音功能正常工作,我想将其添加到您单击!help时弹出的可用命令中。表格看起来像这样:

!help

1 个答案:

答案 0 :(得分:0)

我认为,临时制根本不起作用,因为您使用的是bot.on()而不是client.on(),后者是在index.js中定义的。剩下的时间我无能为力,但一切都与此有关。