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

Discord机器人加入服务器时无法发送指定消息求助

Fix: Discord Bot Not Sending Welcome Message on Guild Join

It sounds like your bot's guild join event isn't firing properly, or there's a permission/intent issue preventing the message from being sent. Let's break down the fixes for the two most popular Discord libraries:

For discord.py (v2.x+)

The most common culprit here is missing intents or incorrect event handling. Discord requires bots to explicitly enable certain intents to access guild-related events. Here's a working example:

import discord
from discord.ext import commands

# Enable necessary intents
intents = discord.Intents.default()
intents.guilds = True  # Required to detect when the bot joins a guild
intents.messages = True  # Required to send messages

bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_guild_join(guild):
    # Try sending to the server's system channel first (the default welcome channel)
    system_channel = guild.system_channel
    if system_channel and system_channel.permissions_for(guild.me).send_messages:
        await system_channel.send("Hello this is my discord bot")
    else:
        # Fallback: Find the first text channel the bot can send messages to
        for channel in guild.text_channels:
            if channel.permissions_for(guild.me).send_messages:
                await channel.send("Hello this is my discord bot")
                break

bot.run("YOUR_BOT_TOKEN")

Key Checks for discord.py:

  • Enable Intents in Developer Portal: Head to your Discord Developer Dashboard → Your Bot → "Gateway Intents" (double-check the Guilds intent is toggled on).
  • Event Name: Make sure you're using on_guild_join (the old on_server_join is deprecated).
  • Permissions: Ensure your bot has the SendMessages permission in at least one text channel of the server it's invited to.

For discord.js (v14+)

If you're using Discord.js, the issue likely stems from missing intents or incorrect event listening. Here's a reliable implementation:

const { Client, GatewayIntentBits, PermissionsBitField } = require('discord.js');

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds, // Mandatory to trigger guild join events
        GatewayIntentBits.GuildMessages // Required to send messages
    ]
});

client.on('guildCreate', async (guild) => {
    // Try sending to the server's system channel first
    const systemChannel = guild.systemChannel;
    if (systemChannel && systemChannel.permissionsFor(client.user).has(PermissionsBitField.Flags.SendMessages)) {
        await systemChannel.send('Hello this is my discord bot');
    } else {
        // Fallback to the first usable text channel
        const usableChannel = guild.channels.cache.find(
            ch => ch.type === 0 && ch.permissionsFor(client.user).has(PermissionsBitField.Flags.SendMessages)
        );
        if (usableChannel) {
            await usableChannel.send('Hello this is my discord bot');
        }
    }
});

client.login('YOUR_BOT_TOKEN');

Key Checks for discord.js:

  • Intent Configuration: The Guilds intent is non-negotiable for triggering the guildCreate event.
  • Event Name: Use guildCreate (not the outdated guildJoin).
  • Permission Checks: Always verify the bot can send messages to a channel before attempting to send—this avoids silent failures that are hard to debug.

General Troubleshooting Tips

  • Test with a Fresh Server: Invite your bot to a new test server to rule out permission conflicts in existing servers.
  • Add Debug Logs: Insert a print/console log in your event handler to confirm if the event is firing at all (e.g., print("Joined a new guild!") in Python or console.log('Joined a new guild!') in JS).
  • Double-Check Your Token: Ensure you're using the correct bot token from the Discord Developer Dashboard.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:37:04