不和谐的用户键入.findtest,如果满足某些临界条件,请问他们一个问题。我需要保存回复。由于某种原因,保存的“响应”是他们键入的初始“ .findtest”。将不胜感激。谢谢。
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True, pass_content=True)
async def findtest(self, ctx):
discord_id = str(ctx.message.author.id)
with open(("C:/Users/Administrator/Desktop/DATABASE/test.json")) as f:
data = json.load(f)
if discord_id in data:
await self.bot.say("User Found, Finding Game")
#more code to come#
else:
await self.bot.say('Type in your Display Name exactly as it appears')
async def on_message(message, ctx, pass_context=True, pass_content=True):
message_d= str(ctx.message.content)
k = {(discord_id): message_d}
with open("C:/Users/Administrator/Desktop/DATABASE/test.json") as f:
data = json.load(f)
data.update(k)
with open("C:/Users/Administrator/Desktop/DATABASE/test.json", 'w') as f:
json.dump(data, f)
答案 0 :(得分:1)
使用Client.wait_for_message
获得用户对您问题的回答。对于您的机器人来说,记住data
的内容并仅在这些内容更改时打开文件进行更新也更有意义:
class Cog:
def __init__(self, bot):
self.bot = bot
self.path = "C:/Users/Administrator/Desktop/DATABASE/test.json"
with open(self.path) as f:
self.data = json.load(f)
@commands.command(pass_context=True)
async def findtest(self, ctx):
discord_id = str(ctx.message.author.id)
if discord_id in self.data:
await self.bot.say("User Found, Finding Game")
#more code to come#
else:
await self.bot.say('Type in your Display Name exactly as it appears')
response = await self.bot.wait_for_message(author=ctx.message.author,
channel=ctx.message.channel)
message_d = response.content
k = {(discord_id): message_d}
self.data.update(k)
with open(self.path, 'w') as f:
json.dump(self.data, f)