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

如何检查Discord用户是否属于指定Guild(服务器)

如何检查Discord用户是否属于指定Guild(服务器)

Hey there! I get that you're trying to build a Discord command to invite folks to your football club, but only want members from your main server to be eligible. Let's fix that user check issue you're running into.

Looking at your code, the main problem is probably that when you try to fetch a member who isn't in the main server, Discord's API throws an error—and if you don't handle that error, your command just crashes without giving you feedback. Let's go through two solid solutions to fix this.

方案一:缓存优先+安全Fetch(高效且容错)

This approach checks the bot's local cache first to avoid unnecessary API calls, and only fetches from Discord if the member isn't in cache—plus it won't throw an error if the user doesn't exist in the server.

const user = interaction.options.getMember('user');
const club = interaction.options.getRole('club');
const position = interaction.options.getString('position');
// 主服务器ID
const mainGuildId = '1232018031736000613';
const mainGuild = await interaction.client.guilds.fetch(mainGuildId);

// 先检查缓存里有没有该成员
let mainServerMember = mainGuild.members.cache.get(user.id);

// 缓存里没有的话,安全Fetch(force: false 表示如果用户不存在,返回undefined而非报错)
if (!mainServerMember) {
  mainServerMember = await mainGuild.members.fetch({ user: user.id, force: false }).catch(() => null);
}

if (mainServerMember) {
  // 用户在主服务器里,执行邀请逻辑
  console.log(`开始邀请 ${user.user.tag} 加入${club.name},职位:${position}`);
  // 这里写你的邀请代码,比如添加俱乐部角色、发送通知等
  await interaction.reply({ content: `已邀请 ${user.user.tag} 加入${club.name}担任${position}!`, ephemeral: false });
} else {
  // 用户不在主服务器,提示错误
  await interaction.reply({ content: '抱歉,只有主服务器的成员才能被邀请加入足球俱乐部哦!', ephemeral: true });
}

方案二:Try/Catch捕获错误

If you prefer a more straightforward fetch approach, wrapping the fetch in a try/catch block lets you handle the "user not found" error directly.

const user = interaction.options.getMember('user');
const club = interaction.options.getRole('club');
const position = interaction.options.getString('position');
const mainGuildId = '1232018031736000613';
const mainGuild = await interaction.client.guilds.fetch(mainGuildId);

let isInMainServer = false;

try {
  // 尝试获取成员,用户不存在时会抛出错误
  await mainGuild.members.fetch(user.id);
  isInMainServer = true;
} catch (error) {
  // 错误码10007表示用户不在该服务器
  if (error.code === 10007) {
    isInMainServer = false;
  } else {
    // 处理其他错误,比如机器人权限不足
    console.error('检查成员时出错:', error);
    await interaction.reply({ content: '检查成员信息时发生错误,请稍后再试', ephemeral: true });
    return;
  }
}

if (isInMainServer) {
  // 执行邀请逻辑
} else {
  await interaction.reply({ content: '只有主服务器成员才能被邀请加入足球俱乐部哦!', ephemeral: true });
}

几个关键细节要注意:

  • 权限与意图:确保你的机器人在Discord开发者面板里开启了Guild Members Intent,并且在主服务器里有查看成员的权限,否则fetch操作会失败。
  • 变量命名:尽量用清晰的命名(比如把mainguild改成mainGuild),方便自己后续维护代码。
  • API调用效率:优先用缓存检查可以减少API请求次数,避免触发Discord的速率限制。

备注:内容来源于stack exchange,提问作者International Football Federat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.17 10:38:05