当新用户在系统消息传递通道中加入服务器时,如何发送消息?

时间:2020-08-17 06:54:23

标签: python discord bots discord.py discord.py-rewrite

我有此代码:

class member_greeting(commands.Cog):
def __init__(self, bot):
    self.bot = bot

@commands.Cog.listener()
async def on_guild_join(self):
    for guild in self.bot.guilds:
        await guild.system_channel.send("I'm ready to go!")

def setup(bot):
    bot.add_cog(member_greeting(bot))

不将消息发送到系统消息传递通道 如何正确书写?

1 个答案:

答案 0 :(得分:1)

on_guild_join事件具有一个guild参数:

class member_greeting(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_guild_join(self, guild):
        await guild.system_channel.send("I'm ready to go!")

def setup(bot):
    bot.add_cog(member_greeting(bot))

如果您希望机器人在加入新公会时在它所在的每台服务器中发送一条消息,我相信您必须使用async for guild in self.bot.guilds:而不是for guild in self.bot.guilds

class member_greeting(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_guild_join(self):
        async for guild in self.bot.guilds:
            await guild.system_channel.send("I'm ready to go!")

    def setup(bot):
        bot.add_cog(member_greeting(bot))