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

如何实现Discord机器人首次加入服务器时自动发送指定消息?

Discord机器人首次加入服务器自动发送欢迎消息实现方案

Python (discord.py v2.x)

  • 监听on_guild_join事件,该事件会在机器人加入新服务器时触发:
import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.guilds = True  # 开启服务器相关权限
intents.messages = True  # 开启消息发送权限

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

@bot.event
async def on_guild_join(guild):
    # 优先发送到服务器默认的系统欢迎频道
    if guild.system_channel:
        await guild.system_channel.send("大家好!我是新来的机器人,以后请多指教😊")
    else:
        # 若没有系统频道,遍历找到第一个可发送消息的文本频道
        for channel in guild.text_channels:
            if channel.permissions_for(guild.me).send_messages:
                await channel.send("大家好!我是新来的机器人,以后请多指教😊")
                break

bot.run("你的机器人Token")
  • 关键注意事项:
    • 必须在Discord开发者门户开启对应的意图权限(Guilds、Messages)
    • 确保机器人在目标服务器拥有发送消息的频道权限

JavaScript (discord.js v14)

  • 监听guildCreate事件,对应机器人加入服务器的触发时机:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent
    ]
});

client.on('guildCreate', async (guild) => {
    // 优先发送到服务器系统频道
    if (guild.systemChannel) {
        await guild.systemChannel.send('大家好!我是新来的机器人,以后请多指教😊');
    } else {
        // 查找第一个可发送消息的文本频道
        const channel = guild.channels.cache.find(ch => 
            ch.type === 0 && ch.permissionsFor(guild.members.me).has('SendMessages')
        );
        if (channel) {
            await channel.send('大家好!我是新来的机器人,以后请多指教😊');
        }
    }
});

client.login('你的机器人Token');
  • 关键注意事项:
    • 在Discord开发者门户开启所需的意图权限
    • 确认机器人拥有目标频道的消息发送权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 00:10:35