You need to enable JavaScript to run this app. 如何让Discord.js开发的机器人不对SugarPancake和vordemolt回复指令
优惠活动
大模型
产品
解决方案
定价
更多

如何让Discord.js开发的机器人不对SugarPancake和vordemolt回复指令

Discord机器人指定用户免回复修改方案

修改逻辑如下:

  1. 删除原有代码末尾逻辑冲突的message事件监听器,该段逻辑不符合免回复需求
  2. 合并嵌套注册的事件监听器,避免重复触发和内存泄漏问题
  3. 在指令处理的最前置校验位新增免回复用户判断,匹配到目标用户名时直接终止流程
  4. 原有其他用户的指令执行逻辑、管理员.test指令校验逻辑全部保留不变

修改后完整代码:

const Discord = require('discord.js');

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"] });
const prefix = '-';
const fs = require('fs');

client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('YES is online');
    client.user.setActivity(' You Shower', { type: "WATCHING" })
});

client.on('message', message => {
    // 免回复用户列表,后续调整直接修改该数组即可
    const ignoredUsers = ["SugarPancake", "vordemolt"];
    if (ignoredUsers.includes(message.author.username)) return;

    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
    if (message.author.username == "malte" || message.author.username == "Dr.Garbets") {
        if (command === 'comebw') {
            client.commands.get('comebw').execute(message, args);
        } else if (command == 'come') {
            client.commands.get('come').execute(message, args);
        } else if (command == 'mc') {
            client.commands.get('mc').execute(message, args);
        } else if (command == 'mute') {
            console.log(message.author.username);
            client.commands.get('mute').execute(message, args);
        } else if (command == 'unmute') {
            client.commands.get('unmute').execute(message, args);
        } else if (command == 'kick') {
            client.commands.get('kick').execute(message, args);
        }
    }

    // 原有管理员.test指令校验逻辑
    if (message.author.bot || !message.guild) return;
    if (message.content.toLowerCase() === ".test") {
        const roles = ["Admin"];
        if (!message.member.roles.cache.some(r => roles.includes(r.name)))
            return message.channel.send("You don't have the required role to use this command");
    }
});

内容的提问来源于stack exchange,提问作者Uriyah Liberman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 03:57:03