-
-
Save gregorynicholas/4b2c260f44118c5586e96273ed0ecdea to your computer and use it in GitHub Desktop.
Discord.py Pagination example for anyone to use. I'm using it for Anime list API from Kitsu.io that have multiple list of data (json format)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aiohttp | |
import asyncio | |
import json | |
import discord | |
from discord.ext import commands | |
""" | |
Variable | |
""" | |
prefix = 'z!' | |
token = 'Enter your bot token here or just copy the things below' | |
#aiohttp mode | |
async def kitsu(query, num): | |
num = num - 1 | |
api_link = 'https://kitsu.io/api/edge/anime?filter[text]=%s' % query | |
async with aiohttp.ClientSession() as session: | |
try: | |
async with session.get(api_link) as r: | |
try: | |
data = await r.json() | |
except ValueError: | |
return 'ERROR: Something bad happening with the result, try again.' | |
except aiohttp.ClientError: | |
return "ERROR: Connection Timeout." | |
if aiohttp.ClientResponse.status != 200: | |
if aiohttp.ClientResponse.status == 404: | |
return "ERROR: Not Found" | |
elif aiohttp.ClientResponse.status == 500: | |
return "ERROR: Internal Server Error" | |
try: | |
entry = data["data"][num] | |
except IndexError: | |
return "ERROR: No Result." | |
title = entry['attributes']['titles']['en_jp'] | |
synop = entry['attributes']['synopsis'] | |
rate = entry['attributes']['averageRating'] | |
start = entry['attributes']['startDate'] | |
end = entry['attributes']['endDate'] | |
status = entry['attributes']['status'] | |
epTotal = entry['attributes']['episodeCount'] | |
dataLen = len(data['data']) | |
img = entry['attributes']['posterImage']['small'] | |
finalResult = {"title": title, "episode": epTotal, 'status': status, 'score': rate, 'startDate': start, 'endDate': end, 'posterImg': img, 'synopsis': synop, 'dataLength': dataLen} | |
return finalResult | |
bot = commands.Bot(commands.when_mentioned_or(prefix)) | |
@bot.command(pass_context=True) | |
async def animu(ctx, *, title): | |
title = title.replace(' ', '%20') | |
init = await kitsu(title, 1) | |
if type(init) is str: | |
await bot.say(init) | |
return 'No result' | |
else: | |
pass | |
maxPage = int(init['dataLength']) | |
firstRun = True | |
while True: | |
if firstRun: | |
firstRun = False | |
num = 1 | |
find = await kitsu(title, num) | |
embed=discord.Embed(title="Anime Info", color=0x81e28d) | |
embed.set_image(url=find['posterImg']) | |
embed.add_field(name='Title', value=find['title'], inline=False) | |
embed.add_field(name='Episode', value=find['episode'], inline=True) | |
embed.add_field(name='Status', value=find['episode'], inline=True) | |
embed.add_field(name='Score', value=find['score'], inline=True) | |
embed.add_field(name='Start Date', value=find['startDate'], inline=True) | |
embed.add_field(name='End Date', value=find['endDate'], inline=True) | |
msg = await bot.say(embed=embed) | |
msg2 = await bot.say('```{}```'.format(str(find['synopsis']))) | |
if maxPage == 1 and num == 1: | |
print('{}/{}'.format(str(num),str(maxPage))) | |
toReact = ['✅'] | |
elif num == 1: | |
print('{}/{}'.format(str(num),str(maxPage))) | |
toReact = ['⏩', '✅'] | |
elif num == maxPage: | |
print('{}/{}'.format(str(num),str(maxPage))) | |
toReact = ['⏪', '✅'] | |
elif num > 1 and num < maxPage: | |
print('{}/{}'.format(str(num),str(maxPage))) | |
toReact = ['⏪', '⏩', '✅'] | |
for reaction in toReact: | |
await bot.add_reaction(msg2, reaction) | |
#feel free to change ✅ to 🆗 or the opposite | |
def checkReaction(reaction, user): | |
e = str(reaction.emoji) | |
return e.startswith(('⏪', '⏩', '✅')) | |
res = await bot.wait_for_reaction(message=msg2, user=ctx.message.author, timeout=10, check=checkReaction) | |
if res is None: | |
await bot.delete_message(ctx.message) | |
await bot.delete_message(msg) | |
await bot.delete_message(msg2) | |
break | |
elif '⏪' in str(res.reaction.emoji): | |
num = num - 1 | |
find = await kitsu(title, num) | |
embed=discord.Embed(title="Anime Info", color=0x81e28d) | |
embed.set_image(url=find['posterImg']) | |
embed.add_field(name='Title', value=find['title'], inline=False) | |
embed.add_field(name='Episode', value=find['episode'], inline=True) | |
embed.add_field(name='Status', value=find['episode'], inline=True) | |
embed.add_field(name='Score', value=find['score'], inline=True) | |
embed.add_field(name='Start Date', value=find['startDate'], inline=True) | |
embed.add_field(name='End Date', value=find['endDate'], inline=True) | |
fmtSyn = '```{}```'.format(str(find['synopsis'])) | |
await bot.delete_message(msg) | |
await bot.delete_message(msg2) | |
msg = await bot.say(embed=embed) | |
msg2 = await bot.say(fmtSyn) | |
elif '⏩' in str(res.reaction.emoji): | |
num = num + 1 | |
find = await kitsu(title, num) | |
embed=discord.Embed(title="Anime Info", color=0x81e28d) | |
embed.set_image(url=find['posterImg']) | |
embed.add_field(name='Title', value=find['title'], inline=False) | |
embed.add_field(name='Episode', value=find['episode'], inline=True) | |
embed.add_field(name='Status', value=find['episode'], inline=True) | |
embed.add_field(name='Score', value=find['score'], inline=True) | |
embed.add_field(name='Start Date', value=find['startDate'], inline=True) | |
embed.add_field(name='End Date', value=find['endDate'], inline=True) | |
fmtSyn = '```{}```'.format(str(find['synopsis'])) | |
await bot.delete_message(msg) | |
await bot.delete_message(msg2) | |
msg = await bot.say(embed=embed) | |
msg2 = await bot.say(fmtSyn) | |
elif '✅' in str(res.reaction.emoji): | |
await bot.delete_message(ctx.message) | |
await bot.delete_message(msg) | |
await bot.delete_message(msg2) | |
break | |
@animu.error | |
async def animu_handler(error, ctx): | |
if isinstance(error, commands.MissingRequiredArgument): | |
await bot.say("Correct argument are:\n**{}animu <title>**".format(prefix)) | |
@bot.event | |
async def on_ready(): | |
print('Connected.') | |
presence = '{}help for... help.'.format(prefix) | |
await bot.change_presence(game=discord.Game(name=presence)) | |
print('---------------------------------------------------------------') | |
print('Logged in as:') | |
print('Bot name: ' + bot.user.name) | |
print('With Client ID: ' + bot.user.id) | |
print('---------------------------------------------------------------') | |
bot.run(token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment