Files
OpenBB/discordbot/helpers.py
Artem Veremey 1e8daa1f9a Enabling pylint for DiscordBot (#979)
* Enabling markdown linting for DiscordBot

* Clearning remaining linters

* Enabling pylint for DiscordBot

* Fix DiscordBot imports, pylint and mypy

* Flake8 fixes

* Add an exception for too-many-branches
2021-11-24 17:15:42 -08:00

103 lines
3.6 KiB
Python

import asyncio
import discord_components
import yfinance as yf
from discordbot.run_discordbot import gst_bot
def load(ticker, start_date):
df_stock_candidate = yf.download(ticker, start=start_date, progress=False)
df_stock_candidate.index.name = "date"
return df_stock_candidate
async def pagination(cols, ctx):
current = 0
components = [
[
discord_components.Button(
label="Prev", id="back", style=discord_components.ButtonStyle.red
),
discord_components.Button(
label=f"Page {int(cols.index(cols[current]))}/{len(cols)-1}",
id="cur",
style=discord_components.ButtonStyle.green,
disabled=True,
),
discord_components.Button(
label="Next", id="front", style=discord_components.ButtonStyle.green
),
]
]
main_message = await ctx.send(embed=cols[current], components=components)
while True:
# Try and except blocks to catch timeout and break
try:
interaction = await gst_bot.wait_for(
"button_click",
check=lambda i: i.component.id in ["back", "front"], # You can add more
timeout=30.0, # 30 seconds of inactivity
)
# Getting the right list index
if interaction.component.id == "back":
current -= 1
elif interaction.component.id == "front":
current += 1
# If its out of index, go back to start / end
if current == len(cols):
current = 0
elif current < 0:
current = len(cols) - 1
# Edit to new page + the center counter changes
components = [
[
discord_components.Button(
label="Prev",
id="back",
style=discord_components.ButtonStyle.red,
),
discord_components.Button(
label=f"Page {int(cols.index(cols[current]))}/{len(cols)-1}",
id="cur",
style=discord_components.ButtonStyle.green,
disabled=True,
),
discord_components.Button(
label="Next",
id="front",
style=discord_components.ButtonStyle.green,
),
]
]
await interaction.edit_origin(embed=cols[current], components=components)
except asyncio.TimeoutError:
# Disable and get outta here
components = [
[
discord_components.Button(
label="Prev",
id="back",
style=discord_components.ButtonStyle.green,
disabled=True,
),
discord_components.Button(
label=f"Page {int(cols.index(cols[current]))}/{len(cols)-1}",
id="cur",
style=discord_components.ButtonStyle.grey,
disabled=True,
),
discord_components.Button(
label="Next",
id="front",
style=discord_components.ButtonStyle.green,
disabled=True,
),
]
]
await main_message.edit(components=components)
break