我如何为我的机器人禁止、踢出和清除命令?

时间:2021-02-10 19:12:44

标签: javascript node.js discord discord.js bots

我想知道如何为我的机器人禁止、踢出和清除命令。我将向您展示代码,但在此之前我一直在进行研究,但没有任何效果。我使用 node.js,我是一个初学者,所以这就是我需要你告诉我的。

-命令的代码(无外部链接) - 如何让代码工作。 - 代码放在哪里。

好的,这是代码。

const Discord = require('discord.js');
const bot = new Discord.Client();
const { MessageEmbed } = require("discord.js");

bot.once('ready', () => {
    console.log('Ready!');
});

bot.on('message', message => {
if(message.content === '!help') {
let embed = new MessageEmbed()
.setTitle("Ratchet Commands")
.setDescription("!getpizza, !shutup, !playdead, !server-info, !myname, !banhammer, !yourcreator, !annoy, !youare")
.setColor("RANDOM")
message.channel.send(embed)
}
});

bot.on('message', message => {
    if (message.content === '!getpizza') {
        message.channel.send('Welcome to Ratchets Pizza!!! Heres your pizza and have a nice day!!! :pizza:');
    }
});


bot.on('message', message => {
    if (message.content === '!shutup') {
        message.channel.send('Okay, I am sorry.');
    }
});

bot.on('message', message => {
    if (message.content === '!server-info') {
        message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
    }
});

bot.on('message', message => {
    if (message.content === '!myname') {
        message.channel.send(`Your username: ${message.author.username}`);
    }
});

bot.on('message', message => {
    if (message.content === '!rocket') {
        message.channel.send('3..2..1..Blast Off!!! :rocket:');
    }
});

bot.on('message', message => {
    if (message.content === '!youare') {
        message.channel.send(`I am <@!808773656700256318>`);
    }
});
bot.on('message', message => {
if(message.content === '!yourcreator') {
let embed = new MessageEmbed()
.setTitle("Ratchets Creator")
.setDescription("My creator is <@!765607070539579402>")
.setColor("RANDOM")
message.channel.send(embed)
}
});


bot.on('message', message => {
if(message.content.startsWith(`!annoy`)) {
    const mentionedUser = message.mentions.users.first();
    if(!mentionedUser) return message.channel.send("You have to mention someone to continue annoying someone :rofl:");
    mentionedUser.send('You have a problem with me?');
    message.channel.send("Annoyed " + mentionedUser + "! (Oh wait, I annoyed them 2 times!)");
}
});


module.exports = {
    name: '!kick',
    description: "this command kicks people",
    execute(message, args){
        const member = message.mentions.users.first();
        if(member){
            const memberTarget = message.guild.member(member);
            memberTarget.kick();
            message.channel.send("bing bong he is gone!");
        }else{
            message.channel.send('you couldnt kick that person');
        }
    }
}

bot.login('TOKEN');

我知道我需要一个命令处理程序,但我也不知道该怎么做。

谢谢!!!

1 个答案:

答案 0 :(得分:0)

所以首先你犯了一个很大的错误,你总是想起 message 事件,像这样优化你的代码:

const Discord = require('discord.js');
const bot = new Discord.Client();
const { MessageEmbed } = require("discord.js");

bot.once('ready', () => {
    console.log('Ready!');
});

bot.on('message', message => {
if(message.content === '!help') {
let embed = new MessageEmbed()
.setTitle("Ratchet Commands")
.setDescription("!getpizza, !shutup, !playdead, !server-info, !myname, !banhammer, !yourcreator, !annoy, !youare")
.setColor("RANDOM")
message.channel.send(embed)

if (message.content === '!getpizza') {
        message.channel.send('Welcome to Ratchets Pizza!!! Heres your pizza and have a nice day!!! :pizza:');
}

if (message.content === '!server-info') {
        message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
    }

if (message.content === '!myname') {
        message.channel.send(`Your username: ${message.author.username}`);
    }

if (message.content === '!rocket') {
        message.channel.send('3..2..1..Blast Off!!! :rocket:');
    }

if (message.content === '!youare') {
        message.channel.send(`I am <@!808773656700256318>`);
    }

if(message.content === '!yourcreator') {
let embed = new MessageEmbed()
.setTitle("Ratchets Creator")
.setDescription("My creator is <@!765607070539579402>")
.setColor("RANDOM")
message.channel.send(embed)
}

if(message.content.startsWith(`!annoy`)) {
    const mentionedUser = message.mentions.users.first();
    if(!mentionedUser) return message.channel.send("You have to mention someone to continue annoying someone :rofl:");
    mentionedUser.send('You have a problem with me?');
    message.channel.send("Annoyed " + mentionedUser + "! (Oh wait, I annoyed them 2 times!)");
}

});

module.exports = {
    name: '!kick',
    description: "this command kicks people",
    execute(message, args){
        const member = message.mentions.users.first();
        if(member){
            const memberTarget = message.guild.member(member);
            memberTarget.kick();
            message.channel.send("bing bong he is gone!");
        }else{
            message.channel.send('you couldnt kick that person');
        }
    }
}

bot.login('TOKEN');

然后做一个 !ban @mention 你可以这样做:

if (message.content.startsWith('!ban ')) {
                if (message.mentions.members.first()) {
                    if (!message.mentions.members.first().bannable) {
                        message.channel.send('I can\'t ban the specified user')
                        return
                    }
                    try {
                        message.mentions.members.first().ban()
                    }finally {
                        message.channel.send('The member was correctly banned')
                    }
                }
            }

为了一脚!踢

if (message.content.startsWith('!kick ')) {
                if (message.mentions.members.first()) {
                    if (!message.mentions.members.first().kickable) {
                        message.channel.send('I can\'t kick the specified user')
                        return
                    }
                    try {
                        message.mentions.members.first().kick
                    }finally {
                        message.channel.send('The member was correctly kicked')
                    }
                }
            }

最后是清除命令

if (message.content.startsWith('!clear ')) {
                let toClear = args[1]
                for (let i = 0; i < toClear; i++) {
                    m.channel.lastMessage.delete()
                }
            }

希望对你有帮助^^