Python 我制作了一个discord.py机器人,我想知道如何使用sqlite 3创建一个数据库,每当有人在琐事命令中正确回答时,该数据库都可以更新
我已经创建了一个表,其标题为成员id和分数。我希望每次用户得到正确答案时,数据库都会更新。 感谢所有帮助过我的人,如果你想在不和谐中帮助我,S C A R E D#1711Python 我制作了一个discord.py机器人,我想知道如何使用sqlite 3创建一个数据库,每当有人在琐事命令中正确回答时,该数据库都可以更新,python,Python,我已经创建了一个表,其标题为成员id和分数。我希望每次用户得到正确答案时,数据库都会更新。 感谢所有帮助过我的人,如果你想在不和谐中帮助我,S C A R E D#1711 @commands.command() async def trivia(self, ctx, difficulty=None): '''Trivia using the Open Trivia Database''' ez = ['easy','medium','hard']
@commands.command()
async def trivia(self, ctx, difficulty=None):
'''Trivia using the Open Trivia Database'''
ez = ['easy','medium','hard']
if difficulty==None:
question1 = await self.trivia.get_random_question(random.choice(ez))
else:
question1 = await self.trivia.get_random_question(difficulty.lower())
try:
question = question1
except AiotriviaException as error:
return await ctx.send(f"\nDifficulty Levels:\n`Easy`\n`Medium`\n`Hard`")
answers = question.responses
random.shuffle(answers)
final_answers = '\n'.join([f"{index}. {value}" for index, value in enumerate(answers, 1)])
message = await ctx.reply(f"**`{question.question}`**\n`{final_answers}`\n`{question.type.capitalize()}` `{question.category}`", allowed_mentions=discord.AllowedMentions(replied_user=False))
answer = answers.index(question.answer)+1
await self.trivia.close()
try:
while True:
msg = await self.bot.wait_for('message', timeout=15, check=lambda m: m.author == ctx.message.author )
if str(answer) in msg.content:
db = sqlite3.connect('main.sqlite')
cursor = db.cursor()
cursor.execute(f"SELECT member_id,score FROM main WHERE member_id = {ctx.message.author.id}")
result = cursor.fetchone()
if result is None:
sql = ("INSERT INTO main(member_id,score) VALUES(?,?)")
val = (ctx.message.author.id, 0)
cursor.execute(sql, val)
db.commit()
else:
cursor.execute(f"SELECT member_id,score FROM main WHERE member_id ='{ctx.message.author.id}'")
result1 = cursor.fetchone()
score = int(result1[2])
sql = ("UPDATE main SET score = ? WHERE member_id = ?")
val = (score + 1, ctx.message.author.id)
cursor.execute(sql, val)
db.commit()
db.close()
return await ctx.send(f"`{question.answer}` `is correct! wow ur so proo` ")
else:
return await ctx.send(f"`haha noob. The correct answer is:` `{question.answer}`")
except asyncio.TimeoutError:
await ctx.send(f"`Time ran out!` `{question.answer}` `is the corect answer`")