【发布时间】:2022-01-16 12:06:57
【问题描述】:
我正在尝试编写一个不和谐的机器人,当你输入 !create 时,你可以输入每个人的分数,它应该将分数保存到一个文本文件以便以后检索它们,但它没有写任何东西到文本文件,我不知道为什么会这样。
我的代码:
import discord
client = discord.Client()
channel = client.get_channel("channelid")
file = open("TournamentScores.txt", "a")
tournament = False
scoreset1 = False
scoreset2 = False
@client.event
async def on_ready():
print("~Bot Started~")
channel = client.get_channel("channelid")
@client.event
async def on_message(message):
print("received message")
channel = client.get_channel("channelid")
global tournament, scoreset1, scoreset2
if message.content.startswith("!create") and not tournament:
print("Tournament Created")
tournament = True
await message.channel.send("Goals for Conner:")
file.write("~Tournament~ \n")
elif message.content.isdigit():
if tournament and not scoreset1:
scoreset1 = True
ConnerTemplate = "Conner - " + message.content
file.writelines(ConnerTemplate + " \n")
await message.channel.send("Goals for Ewan:")
elif scoreset1 and not scoreset2:
scoreset2 = True
EwanTemplate = "Ewan - " + message.content
file.writelines(EwanTemplate + " \n")
await message.channel.send("Goals for Jack:")
elif scoreset2 == True and tournament == True:
JackTemplate = "Jack - " + message.content
file.writelines(JackTemplate + " \n")
await message.channel.send("Goals for Thomas:")
tournament = False
elif not tournament and file == open:
ThomasTemplate = "Thomas - " + message.content
file.writelines(ThomasTemplate + " \n")
file.close()
tournament = False
【问题讨论】:
-
可能写文件的
if语句的条件都不成立? -
或者当您查看文件时,文件可能从未关闭并且尚未刷新?例如
file永远不会等于open函数,所以最后一个elif永远不会被执行。 -
尝试使用调试器单步执行函数。