当我的机器人离开服务器时,如何从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)
答案 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)