You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Discord服务器中对比用户ID与机器人ID列表?

问题解决:判断目标用户是否为机器人

你当前代码中member.user.id == bots的判断逻辑完全错误——bots是存储所有机器人ID的数组,单个用户ID和数组做相等判断永远不会返回true,这就是你无法正确对比的原因。下面给出两种可行的解决方法:


方法1:直接使用Discord.js自带的bot属性(最简便高效)

Discord.js的User对象本身就内置了bot布尔属性,无需自己生成机器人ID数组,直接判断即可:

async execute(interaction) {
    const target = interaction.options.getUser("target")
    const member = interaction.options.getMember("target") // 优化:直接获取成员,无需查缓存

    if (!member) {
        // ... 原逻辑不变
    } else if (member.user.id === interaction.user.id) {
        // ... 原逻辑不变
    } else if (member.user.bot) { // 直接判断是否为机器人
        await interaction.reply({ content: "**🧩│你不能给机器人转账**" }).then(() => {
            setTimeout(async () => {
                await interaction.deleteReply().catch((error) => {
                    if (error.code !== RESTJSONErrorCodes.UnknownMessage) return;
                });
            }, ms("3s"));
        });
    }
}

方法2:用数组includes()方法判断ID是否在列表中

如果一定要保留自己生成的机器人ID数组,使用数组的includes()方法检查目标ID是否存在于数组内:

async execute(interaction) {
    const target = interaction.options.getUser("target")
    const member = await interaction.guild.members.cache.get(target.id)
    const bots = interaction.guild.members.cache.filter(m => m.user.bot).map(i => i.user.id) // 无需await,缓存操作是同步的

    if (!member) {
        // ... 原逻辑不变
    } else if (member.user.id === interaction.user.id) {
        // ... 原逻辑不变
    } else if (bots.includes(member.user.id) || member.user.id === interaction.guild.members.me.id) { // 使用includes判断
        await interaction.reply({ content: "**🧩│你不能给机器人转账**" }).then(() => {
            setTimeout(async () => {
                await interaction.deleteReply().catch((error) => {
                    if (error.code !== RESTJSONErrorCodes.UnknownMessage) return;
                });
            }, ms("3s"));
        });
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 03:25:28