aiotrivia
Async Wrapper for the OpenTDB API
Installation
$ pip install git+https://github.com/niztg/aiotrivia
Example Usage
import asyncio
import aiotrivia
client = aiotrivia.TriviaClient()
async def main():
data = await client.get_specific_question(category=20)
for i in data:
print('%s | %s' % (i.question, i.responses))
await client.close() # after you're done with everything
asyncio.get_event_loop().run_until_complete(main())
Returns:
Which figure from Greek mythology traveled to the underworld to return his wife Eurydice to the land of the living? | ['Daedalus', 'Hercules', 'Perseus', 'Orpheus']
discord.py command usage
from aiotrivia import TriviaClient, AiotriviaException
from discord.ext import commands
import asyncio
import random
class TriviaCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.trivia = TriviaClient()
@commands.command()
async def trivia(self, ctx, difficulty):
try:
question = await self.trivia.get_random_question(difficulty)
except AiotriviaException as error:
return await ctx.send(f"{error.__class__.__name__}: {error}")
answers = question.responses
random.shuffle(answers)
final_answers = '\n'.join([f"{index}. {value}" for index, value in enumerate(answers, 1)])
message = await ctx.send(f"**{question.question}**\n{final_answers}\n{question.type.capitalize()} Question about {question.category}")
answer = answers.index(question.answer)+1
await self.trivia.close() # cleaning up
try:
while True:
msg = await self.client.wait_for('message', timeout=15, check=lambda m: m.id != message.id)
if str(answer) in msg.content:
return await ctx.send(f"{answer} was correct! ({question.answer})")
except asyncio.TimeoutError:
await ctx.send(f"The correct answer was {question.answer}")
def setup(bot):
bot.add_cog(TriviaCog(bot))
For more info, read the documentation
Or join the discord server
Release files for aiotrivia 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aiotrivia-2.0.1.tar.gz | 4.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aiotrivia-2.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.8 kB
Release files / aiotrivia-2.0.1.tar.gz
| Download URL | aiotrivia-2.0.1.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ef057c2765b0c6167edbacd284e919fd8c5827c46f142ffc8eb177282cc22a49
|
|
BLAKE2b-256 checksum How to use checksums |
d27ff41a91f67bd700444dc3767fec27fa78f50f44fad2ddb4a165e6c789cb92
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3rc1
|
Release files / aiotrivia-2.0.1-py3-none-any.whl
| Download URL | aiotrivia-2.0.1-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1fce48405eed4eabdf5ce7076d9c7cdac9cf8fc527656ca14eb9c687658cc71d
|
|
BLAKE2b-256 checksum How to use checksums |
741c831284bf4e9048f95645ebaf21eb7108e2631e66a94c0d5ba68fbb58d91f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3rc1
|