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

Discord机器人加入服务器时无法发送嵌入消息的问题求助

问题修复方案

核心问题分析

你的代码存在几个导致机器人无法发送欢迎嵌入的问题:

  1. 频道类型判断错误:Discord.js v14中,文本频道的类型标识是ChannelType.GuildText,而非旧版的'text'
  2. 嵌入构造器版本不匹配:你同时导入了MessageEmbed(v13版本)和EmbedBuilder(v14版本),v14推荐使用EmbedBuilder
  3. 频道缓存可能未完全加载:直接使用guild.channels.cache.find可能找不到频道,因为缓存可能还没同步完成
  4. 权限检查不完整:只检查发送消息权限,缺少查看频道权限,没有这个权限机器人无法看到该频道

修复后的完整代码

const { Client, GatewayIntentBits, EmbedBuilder, ChannelType } = require('discord.js');
const config = require('./config.json');

const client = new Client({
    intents: [
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildPresences,
        GatewayIntentBits.MessageContent
    ]
});

const prefix = config.prefix;

client.on('ready', () => {
    console.log('Bot Ready');
});

client.on('messageCreate', message => {
    if (message.content === '!ping') {
        message.channel.send('pong');
    }
});

client.on('guildCreate', async guild => {
    // 先获取完整的频道列表,避免缓存不全问题
    await guild.channels.fetch();
    
    // 筛选第一个机器人有权限的文本频道
    const channel = guild.channels.cache.find(channel => 
        channel.type === ChannelType.GuildText &&
        channel.permissionsFor(guild.me).has(['SEND_MESSAGES', 'VIEW_CHANNEL'])
    );

    if (channel) {
        const welcomeEmbed = new EmbedBuilder()
            .setColor(0xF99CF8)
            .setTitle('**B**')
            .setAuthor({ name: 'S' })
            .setThumbnail('https://i.imgur.com/N4') // 注意这里的链接是否完整,无效链接会导致缩略图加载失败
            .setDescription('H');
        
        try {
            await channel.send({ embeds: [welcomeEmbed] });
            console.log(`已在服务器 ${guild.name} 的频道 ${channel.name} 发送欢迎消息`);
        } catch (error) {
            console.error(`发送消息失败: ${error}`);
        }
    } else {
        console.log(`服务器 ${guild.name} 中没有机器人可发送消息的文本频道`);
    }
});

client.login(config.token);

额外注意事项

  • 确保你的机器人邀请链接包含applications.commands和send_messages权限
  • 检查缩略图链接https://i.imgur.com/N4是否完整,无效链接会导致缩略图无法显示
  • 如果机器人仍然无法发送消息,检查服务器的频道权限设置,确保机器人在目标频道有发送嵌入消息的权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 01:07:52