Discord.js V13 入群欢迎消息报错GuildChannel无.send方法求助
问题原因及修复方案
1. 核心问题说明
- 缺失必要的Gateway Intent:discord.js v13 中
guildMemberAdd事件的触发依赖GUILD_MEMBERS特权意图,你的初始化配置中未添加该意图,首先会导致事件无法正常触发。 - 未做频道类型校验:
guild.channels.cache.get()返回的是通用GuildChannel基类实例,只有可发送消息的文本频道(TextChannel)、公告频道等子类才存在send方法,v13默认不做类型推导,直接调用会报错。 - 额外说明:v13的
send方法完全支持直接传入普通文本字符串,并非仅能发送Embed。
2. 前置操作
进入Discord开发者平台对应Bot的管理页,在「Privileged Gateway Intents」区域开启Server Members Intent开关。
3. 修正后代码
客户端初始化代码
const { Client, Intents } = require('discord.js'); // 添加GUILD_MEMBERS意图 const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS] });
入群欢迎事件代码
client.on('guildMemberAdd', guildMember => { // 获取频道后先校验是否为可发消息的文本频道 const welcomeChannel = guildMember.guild.channels.cache.get('474431129613762571'); if (welcomeChannel?.isText()) { welcomeChannel.send(`**Welcome to the discord server, <@${guildMember.user.id}>!**`); } });
内容的提问来源于stack exchange,提问作者Defective
相关产品推荐
相关产品推荐

