简体   繁体   English

使用 discord.js 使用 Discord 机器人,tempmute 不起作用

[英]Working on a Discord bot with discord.js, tempmute won't work

making a discord bot in javascript with visual studio code but for some reason getting an error.使用 Visual Studio 代码在 javascript 中制作 discord 机器人,但由于某种原因出现错误。 I'll show you the code that is relevant first.我将首先向您展示相关的代码。

Overall trying to get a temporary mute function to work and I want to add it to the available commands which pops up when you hit.help: Table looks like this:总体而言,试图让临时静音 function 工作,我想将其添加到当您点击时弹出的可用命令中: 表如下所示:

!help !帮助

classes I'm working with我正在使用的课程

Here is the index.js:这是 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);

Here is tempmute:这是临时工:


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


});

Here is the help.js which lists all commands这是列出所有命令的 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:错误:

Error message, bot not defined.错误消息,未定义机器人。

Overall trying to get a temporary mute function to work and I want to add it to the available commands which pops up when you hit.help: Table looks like this:总体而言,试图让临时静音 function 工作,我想将其添加到当您点击时弹出的可用命令中: 表如下所示:

!help !帮助

I think the tempmute simply doesn't work because you use bot.on() instead of client.on() , which was defined in the index.js.我认为 tempmute 根本不起作用,因为您使用bot.on()而不是在 index.js 中定义的client.on() I can't help you for the rest but everything is maybe related to this.我无法为您提供 rest 的帮助,但一切都可能与此有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM