discord rewrite如何在离开服务器时使bot删除数据

时间:2020-05-23 13:04:23

标签: json discord.py

当我的机器人离开服务器时,如何从json文件中删除其数据。

@client.event
async def on_guild_leave(guild):
    guildname = str(guild)
    with open("serverchannel.json") as jsong_file:
        data = json.load(jsong_file)
    data = data.remove(guildname)
    with open("serverchannel.json", "w") as outfile:
        json.dump(data, outfile)

1 个答案:

答案 0 :(得分:0)

on_guild_leave()不是客户端事件,您应该使用on_guild_remove(),只要客户端不再在公会中就会调用该事件。原因可能包括离开,被禁止,被踢和被删除的行会。

@client.event
async def on_guild_remove(guild):
    guildname = guild.name
    with open("serverchannel.json") as jsong_file:
        data = json.load(jsong_file)
    data = data.remove(guildname)
    with open("serverchannel.json", "w") as outfile:
        json.dump(data, outfile)