Discord.py ctx.guild.edit有效,但self.bot.guild.edit无效吗?

问题描述:

就像标题说的那样,我正在尝试对行会进行编辑,但是要进行事件处理.这是我的代码的一部分:

Like the title says, I'm trying to do guild edits but on an events. Here's part of my code:

    @commands.guild_only()
    async def on_ready(self):
    server = self.bot.get_guild("serverid")
        while True:
            await self.bot.guild.edit(guild=server, name="foo")
            await asyncio.sleep(1)
            await self.bot.guild.edit(guild=server, name="bar")
            await asyncio.sleep(1)

我已经用一个独立的命令对其进行了测试,所以我知道ctx.guild.edit可以工作,但是我不确定如何在事件中使用它.

I've already tested it with a standalone command, so I know that ctx.guild.edit works but I'm not sure how to get it to work in an event.

您应致电 Guild 对象 server

async def on_ready(self):
server = self.bot.get_guild(SERVER_ID)
while server is not None:
    await server.edit(name="foo")
    await asyncio.sleep(1)
    await server.edit(name="bar")
    await asyncio.sleep(1)

此外,请确保将公会的ID作为整数而不是字符串传递,并且 guild_only 装饰器仅应在命令上使用.

Also, make sure that you're passing the id of the guild as an int and not a string, and the guild_only decorator should only be used on commands.