当bot加入s时发送消息

2024-05-16 03:44:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在每次机器人被邀请到服务器时发送一条消息。然后应该写下这样的话:“你好,这是我的不和机器人”

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

@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

Tags: 代码inevent消息forasyncondef
1条回答
网友
1楼 · 发布于 2024-05-16 03:44:22

你的代码中有几个错误。下面是一个版本,它只在刚刚加入的服务器的#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))

相关问题 更多 >