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

Discord.js带服务器加入要求的抽奖命令开发求助

How to Restrict Giveaway Entries to Users in a Specific Server

Hey there! Let's work through how to make sure only users in your specified server can enter your Discord bot's giveaway. Here's a step-by-step implementation using discord.js (v14+, since it's the latest stable version):

1. Cache the Target Server First

First, we need to get a reference to the server users must join. We'll fetch and cache it when the bot starts up so we don't have to repeatedly call the API later:

// Replace with your target server's ID
const REQUIRED_GUILD_ID = "123456789012345678";
let requiredGuild;

client.on('ready', async () => {
  try {
    requiredGuild = await client.guilds.fetch(REQUIRED_GUILD_ID);
    console.log(`Cached required server: ${requiredGuild.name}`);
  } catch (err) {
    console.error(`Failed to fetch required server: ${err.message}`);
  }
});

2. Validate Users on Reaction Add

When a user reacts to your giveaway message, we'll check if they're in the required server. If not, we'll remove their reaction and optionally notify them. We'll also skip bot reactions to avoid clutter:

// Replace with your giveaway message's ID (or add logic to detect giveaway messages dynamically)
const GIVEAWAY_MESSAGE_ID = "987654321098765432";

client.on('messageReactionAdd', async (reaction, user) => {
  // Ignore bot reactions
  if (user.bot) return;
  
  // Make sure this is the giveaway message we care about
  if (reaction.message.id !== GIVEAWAY_MESSAGE_ID) return;

  try {
    // Try to fetch the user as a member of the required server
    await requiredGuild.members.fetch(user.id);
    
    // If we get here, the user is in the server! Record their entry
    console.log(`Recorded entry from ${user.tag} (valid member)`);
    // Add your logic here to save their entry (e.g., push to an array, save to a database)
  } catch (err) {
    // If fetch fails, the user isn't in the server
    await reaction.users.remove(user);
    console.log(`Removed reaction from ${user.tag} (not in required server)`);
    
    // Optional: Send a DM to the user explaining why they can't enter
    try {
      await user.send(`You need to join ${requiredGuild.name} to enter this giveaway!`);
    } catch (dmErr) {
      console.error(`Couldn't DM ${user.tag}: ${dmErr.message}`);
    }
  }
});

3. Handle Reaction Removals (Optional)

If you want to remove users from the giveaway when they un-react, add this listener to clean up your entry list—again, only for users who are actually in the required server:

client.on('messageReactionRemove', async (reaction, user) => {
  if (user.bot) return;
  if (reaction.message.id !== GIVEAWAY_MESSAGE_ID) return;

  try {
    // Confirm the user was in the server (so we know they had a valid entry)
    await requiredGuild.members.fetch(user.id);
    
    // Remove their entry from your list/database
    console.log(`Removed ${user.tag} from giveaway entries`);
    // Add your cleanup logic here
  } catch (err) {
    // User wasn't in the server anyway—nothing to do
    return;
  }
});

Key Notes to Keep in Mind

  • Permissions: Your bot needs the View Members permission in the required server, otherwise members.fetch() will fail.
  • Dynamic Giveaways: If you create giveaways via a command (instead of hardcoding the message ID), save the message ID to a variable or database when you send the giveaway message.
  • Rate Limits: For large servers, caching user membership status (e.g., with a Set of user IDs) can reduce API calls and avoid hitting rate limits.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 16:07:58