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

Discord.js如何按参数指定数量随机给语音频道成员发私信?

Solution: Randomly DM Specified Number of Voice Channel Members

Got it, let's build out the functionality you need—randomly sending DMs to a specific number of voice channel members based on your command parameter. Since you already have the basics down (filtering voice members and sending DMs), we'll focus on the random selection and parameter handling part.

Step 1: Parse & Validate the Target Count

First, you need to extract the number of members you want to DM from your command arguments. Make sure to validate it to avoid errors:

  • Check if the argument is a valid positive number
  • Ensure it doesn't exceed the total number of eligible voice channel members

Step 2: Randomly Select Members

Use a reliable random sampling method to pick the desired number of members without duplicates. The Fisher-Yates shuffle is a great choice here—it's efficient and unbiased.

Step 3: Integrate with Your Existing Code

Here's how to put it all together with your current code structure:

// Assuming you have access to the voice channel (e.g., message.member.voice.channel)
let vampireCommand = args[0];
if (vampireCommand.content.startsWith('vampir')) {
  // 1. Get eligible voice channel members (you mentioned you already handle this)
  const voiceChannel = message.member.voice.channel;
  if (!voiceChannel) {
    return message.reply('You need to be in a voice channel to use this command!');
  }
  // Filter out bots/members you can't DM (adjust this based on your existing logic)
  const eligibleMembers = voiceChannel.members.filter(member => !member.user.bot && member.user.dmChannel !== null);
  
  // 2. Parse and validate the target count from args
  const targetCount = parseInt(args[1]);
  if (isNaN(targetCount) || targetCount <= 0 || targetCount > eligibleMembers.size) {
    return message.reply(`Please provide a valid number between 1 and ${eligibleMembers.size}!`);
  }
  
  // 3. Randomly select the target number of members
  const memberArray = Array.from(eligibleMembers.values());
  // Fisher-Yates shuffle to randomize the array
  for (let i = memberArray.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [memberArray[i], memberArray[j]] = [memberArray[j], memberArray[i]];
  }
  // Pick the first N members from the shuffled array
  const selectedMembers = memberArray.slice(0, targetCount);
  
  // 4. Send DMs to selected members (your existing DM logic here)
  selectedMembers.forEach(member => {
    member.send('Your vampire-themed message here!')
      .catch(err => {
        message.channel.send(`Couldn't DM ${member.user.tag}: ${err.message}`);
      });
  });
  
  message.reply(`Successfully sent DMs to ${selectedMembers.length} random voice channel members!`);
}

Key Notes:

  • Validation: The check for targetCount ensures you don't try to select more members than available, or use invalid numbers.
  • Error Handling: The .catch() on member.send() handles cases where a member has DMs disabled, so your bot won't crash.
  • Eligibility Filter: Adjust the eligibleMembers filter to match your existing logic (e.g., include/exclude certain roles, online status, etc.).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 10:07:42