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

Discord.js 新人欢迎Bot失效求助:无法@用户发送问候

Fixing Your Discord.js Welcome Bot Issues

Let's walk through the problems in your code and fix them step by step:

  • Variable name typo: You initialized const newUser = new Discord.Collection(); but used newUsers later in the guildMemberAdd event. This mismatch breaks the collection's functionality entirely.
  • Client instance mix-up: You created your bot with var bot = new Discord.Client(); but tried to listen for events on client (which doesn't exist). Swap that to bot.on("guildMemberAdd", ...) (or rename your bot instance to client for consistency).
  • Outdated channel fetching: In Discord.js v12+, guild.channels.find() is replaced with guild.channels.cache.find()—channels are stored in a cache now, so the old method won't work.
  • Missing Gateway Intents (v13+): If you're using Discord.js v13 or higher, you need to enable the GuildMembers intent in your Discord Developer Portal and pass it when initializing the client. Without this, the guildMemberAdd event won't fire at all.
  • Extra closing bracket: There's an extra }); before bot.login(TOKEN); that will throw a syntax error.

Here's the corrected, working version of your code:

const Discord = require("discord.js");
const TOKEN = "private";
const PREFIX = "!";
// Fixed variable name to match usage later
const newUsers = new Discord.Collection(); 
const talkedRecently = new Set();

// For Discord.js v13+, include required intents
const client = new Discord.Client({
  intents: [
    Discord.Intents.FLAGS.GUILDS,
    Discord.Intents.FLAGS.GUILD_MEMBERS
  ]
});

var fortunes = [ "Yes", "No", "Maybe", "IDK" ];

client.on("ready", () => {
  console.log("Bot is online and ready!"); // Confirm bot is running
});

client.on("guildMemberAdd", (member) => {
  const guild = member.guild;
  newUsers.set(member.id, member.user);

  if (newUsers.size > 0) {
    // Use cache to find a channel the bot can send messages to
    const defaultChannel = guild.channels.cache.find(c => c.permissionsFor(guild.me).has("SEND_MESSAGES"));
    const userlist = newUsers.map(u => u.toString()).join(" ");
    // Clean up line break formatting
    defaultChannel.send(`Hello Creativistian!\n${userlist}`);
    newUsers.clear();
  }
});

client.login(TOKEN);

Quick Extra Tips:

  • Don't forget to enable the Server Members Intent in your Discord Developer Portal under your bot's "Privileged Gateway Intents" section—this is mandatory for the guildMemberAdd event to trigger in v13+.
  • If you're using an older Discord.js version (v11 or lower), remove the intents block and switch back to guild.channels.find().
  • The user.toString() method automatically turns the user object into a mention, so once the rest of the code works, the @mention feature will too.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:42:01