OpenTDB API的异步API包装。(https://opentdb.com/)
aiotrivia的Python项目详细描述
艾奥特里维亚
OpenTDB API的异步包装器
安装
$ pip install git+https://github.com/niztg/aiotrivia
示例用法
^{pr2}$返回:
Which figure from Greek mythology traveled to the underworld to return his wife Eurydice to the land of the living? | ['Daedalus', 'Hercules', 'Perseus', 'Orpheus']
在不和.py命令用法
fromaiotriviaimportTriviaClient,AiotriviaExceptionfromdiscord.extimportcommandsimportasyncioimportrandomclassTriviaCog(commands.Cog):def__init__(self,bot):self.bot=botself.trivia=TriviaClient()@commands.command()asyncdeftrivia(self,ctx,difficulty):try:question=awaitself.trivia.get_random_question(difficulty)exceptAiotriviaExceptionaserror:returnawaitctx.send(f"{error.__class__.__name__}: {error}")answers=question.responsesrandom.shuffle(answers)final_answers='\n'.join([f"{index}. {value}"forindex,valueinenumerate(answers,1)])message=awaitctx.send(f"**{question.question}**\n{final_answers}\n{question.type.capitalize()} Question about {question.category}")answer=answers.index(question.answer)+1awaitself.trivia.close()# cleaning uptry:whileTrue:msg=awaitself.client.wait_for('message',timeout=15,check=lambdam:m.id!=message.id)ifstr(answer)inmsg.content:returnawaitctx.send(f"{answer} was correct! ({question.answer})")exceptasyncio.TimeoutError:awaitctx.send(f"The correct answer was {question.answer}")defsetup(bot):bot.add_cog(TriviaCog(bot))
For more info, read the documentation
Or join the discord server
- 项目
标签: