在本指南中,我们将使用 node.js 和 discord.js 库为 discord 创建一个简单的审核机器人。该机器人将包括禁止、静音和监控聊天活动等功能。
在本指南中,我们将使用 node.js 和 discord.js 库为 discord 创建一个简单的审核机器人。该机器人将包括禁止、静音和监控聊天活动等功能。
mkdir discord-moderation-bot cd discord-moderation-bot
npm init -y
npm install discord.js
touch index.js
在代码编辑器中打开index.js并添加以下代码:
const { client, gatewayintentbits } = require('discord.js'); const client = new client({ intents: [ gatewayintentbits.guilds, gatewayintentbits.guildmessages, gatewayintentbits.messagecontent, gatewayintentbits.guildmembers ] }); const token = 'your_bot_token'; // replace with your bot token
client.once('ready', () => { console.log(`logged in as ${client.user.tag}!`); }); client.login(token);
client.on('messagecreate', (message) => { if (message.content.startswith('!ban')) { if (!message.member.permissions.has('banmembers')) { return message.reply('you do not have permission to ban members.'); } const member = message.mentions.members.first(); if (!member) { return message.reply('please mention a user to ban.'); } member.ban() .then(() => message.reply(`${member.user.tag} has been banned.`)) .catch((err) => message.reply('i was unable to ban the member.')); } });
client.on('messagecreate', (message) => { if (message.content.startswith('!mute')) { if (!message.member.permissions.has('manageroles')) { return message.reply('you do not have permission to mute members.'); } const member = message.mentions.members.first(); if (!member) { return message.reply('please mention a user to mute.'); } let muterole = message.guild.roles.cache.find(role => role.name === 'muted'); if (!muterole) { return message.reply('no "muted" role found. please create one first.'); } member.roles.add(muterole) .then(() => message.reply(`${member.user.tag} has been muted.`)) .catch((err) => message.reply('i was unable to mute the member.')); } });
client.on('messagecreate', (message) => { if (message.content.startswith('!clear')) { if (!message.member.permissions.has('managemessages')) { return message.reply('you do not have permission to manage messages.'); } const args = message.content.split(' '); const amount = parseint(args[1]); if (isnan(amount) || amount <= 0) { return message.reply('please specify a valid number of messages to delete.'); } message.channel.bulkdelete(amount, true) .then((deletedmessages) => message.reply(`deleted ${deletedmessages.size} messages.`)) .catch((err) => message.reply('i was unable to delete messages.')); } });
node index.js
通过本指南,您将拥有一个功能齐全的审核机器人,您可以对其进行自定义以满足服务器的需求!