Python 当bot加入服务器时发送消息

Python 当bot加入服务器时发送消息,python,discord,discord.py,discord.py-rewrite,Python,Discord,Discord.py,Discord.py Rewrite,每当机器人被邀请到服务器时,我都想发送一条消息。然后它应该写下类似这样的内容:“你好,这是我的discord机器人” 到目前为止,我有这个代码,它不会产生错误,但也不会发送消息 @bot.event async def on_server_join(ctx): for guild in bot.guilds: for channel in guild.text_channels: if channel.permissions_for(guild.m

每当机器人被邀请到服务器时,我都想发送一条消息。然后它应该写下类似这样的内容:“你好,这是我的discord机器人”

到目前为止,我有这个代码,它不会产生错误,但也不会发送消息

@bot.event
async def on_server_join(ctx):
    for guild in bot.guilds:
        for channel in guild.text_channels:
            if channel.permissions_for(guild.me).say:
                await ctx.message.channel.send('Hello! \n')
                break

您的代码中有几个错误。下面是一个版本,它只是在刚刚加入的服务器的
#general
文本频道中打招呼(与它所属的每个服务器的每个文本频道相反)。下面的代码用于重写分支

from discord.utils import find

@client.event
async def on_guild_join(guild):
    general = find(lambda x: x.name == 'general',  guild.text_channels)
    if general and general.permissions_for(guild.me).send_messages:
        await general.send('Hello {}!'.format(guild.name))

如果您在“重写”分支上,则事件已重命名为,并且没有上下文。唯一的参数应该是表示您加入的服务器的
Guild
对象。