首页 > 解决方案 > 我使用 discord.py 制作了一个 Welcomer 机器人。启动成功,但不向频道发送指定消息

问题描述

我使用 discord.py 制作了一个 Welcomer 机器人。它启动成功,但不向通道发送指定的消息。为什么是这样?

import discord
from discord.ext import commands
import asyncio
import datetime
bot = commands.Bot(command_prefix="/")

@bot.event
async def on_member_join(member):
    emb = discord.Embed(
        color = 0xff0000,
        title = "{member.name}, welcome to my server!",
        description = f"Total participants: {len(list(member.guild.members))}",
        timestamp = datetime.datetime.utcnow()
    )
    
    channel = bot.get_channel(CHANNEL ID)
    await channel.send(embed = emb)

bot.run ("BOT TOKEN")

标签: pythondiscord.py

解决方案


您的代码没有问题。我怀疑意图已关闭。转到Discord Developer Portal ,然后在您的机器人应用程序中打开Server Members Intent 。

在此处输入图像描述

on_member_join如果没有关闭此意图,事件将不会执行。

编辑:

还可以尝试在代码中定义意图:

import discord
#....
from discord import Intents
intents = Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="/", intents = intents)

推荐阅读