diff --git a/discordbot/economy/economy_menu.py b/discordbot/economy/economy_menu.py index dc996cafd7c..92b13314552 100644 --- a/discordbot/economy/economy_menu.py +++ b/discordbot/economy/economy_menu.py @@ -1,5 +1,6 @@ import asyncio import discord +import discord_components import discordbot.config_discordbot as cfg @@ -12,6 +13,11 @@ from discordbot.economy.futures import futures_command from discordbot.economy.usbonds import usbonds_command from discordbot.economy.glbonds import glbonds_command from discordbot.economy.currencies import currencies_command +from discordbot.economy.energy import energy_command +from discordbot.economy.metals import metals_command +from discordbot.economy.grains import grains_command +from discordbot.economy.meats import meats_command +from discordbot.economy.softs import softs_command from discordbot.economy.valuation import valuation_command from discordbot.economy.performance import performance_command @@ -60,6 +66,31 @@ class EconomyCommands(discord.ext.commands.Cog): """Global bonds overview [Wall St. Journal]""" await glbonds_command(ctx) + @discord.ext.commands.command(name="economy.energy") + async def energy(self, ctx: discord.ext.commands.Context): + """Displays energy futures data [Finviz]""" + await energy_command(ctx) + + @discord.ext.commands.command(name="economy.metals") + async def metals(self, ctx: discord.ext.commands.Context): + """Displays metals futures data [Finviz]""" + await metals_command(ctx) + + @discord.ext.commands.command(name="economy.meats") + async def meats(self, ctx: discord.ext.commands.Context): + """Displays meats futures data [Finviz]""" + await meats_command(ctx) + + @discord.ext.commands.command(name="economy.grains") + async def grains(self, ctx: discord.ext.commands.Context): + """Displays grains futures data [Finviz]""" + await grains_command(ctx) + + @discord.ext.commands.command(name="economy.softs") + async def softs(self, ctx: discord.ext.commands.Context): + """Displays softs futures data [Finviz]""" + await softs_command(ctx) + @discord.ext.commands.command(name="economy.currencies") async def currencies(self, ctx: discord.ext.commands.Context): """Currencies overview [Wall St. Journal]""" @@ -104,6 +135,9 @@ class EconomyCommands(discord.ext.commands.Cog): if cfg.DEBUG: print("!economy") + cols_temp = [] + cols = [] + text = ( "0️⃣ !economy.overview\n" "1️⃣ !economy.futures\n" @@ -113,81 +147,231 @@ class EconomyCommands(discord.ext.commands.Cog): "5️⃣ !economy.currencies\n" "6️⃣ !economy.feargreed\n" "7️⃣ !economy.valuation \n" - "8️⃣ !economy.performance " + "8️⃣ !economy.performance \n" + "9️⃣ !economy.energy" ) - - title = "Economy Menu" - embed = discord.Embed(title=title, description=text, colour=cfg.COLOR) - embed.set_author( - name=cfg.AUTHOR_NAME, - icon_url=cfg.AUTHOR_ICON_URL, + cols_temp.append(text) + text = ( + "0️⃣ !economy.metals\n" + "1️⃣ !economy.meats\n" + "2️⃣ !economy.grains\n" + "3️⃣ !economy.softs" ) - msg = await ctx.send(embed=embed) + cols_temp.append(text) + for col in cols_temp: + cols.append( + discord.Embed( + description=col, + colour=cfg.COLOR, + title="Economy Menu", + ).set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + ) - emoji_list = ["0️⃣", "1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣"] + emoji_list = [ + "0️⃣", + "1️⃣", + "2️⃣", + "3️⃣", + "4️⃣", + "5️⃣", + "6️⃣", + "7️⃣", + "8️⃣", + "9️⃣", + ] + 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) for emoji in emoji_list: - await msg.add_reaction(emoji) + await main_message.add_reaction(emoji) - def check(reaction, user): - return user == ctx.message.author and str(reaction.emoji) in emoji_list + 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=cfg.MENU_TIMEOUT, # Some seconds of inactivity + ) - try: - reaction, _ = await gst_bot.wait_for( - "reaction_add", timeout=cfg.MENU_TIMEOUT, check=check - ) - if reaction.emoji == "0️⃣": - if cfg.DEBUG: - print("Reaction selected: 0") - await overview_command(ctx) - elif reaction.emoji == "1️⃣": - if cfg.DEBUG: - print("Reaction selected: 1") - await futures_command(ctx) - elif reaction.emoji == "2️⃣": - if cfg.DEBUG: - print("Reaction selected: 2") - await usbonds_command(ctx) - elif reaction.emoji == "3️⃣": - if cfg.DEBUG: - print("Reaction selected: 3") - await glbonds_command(ctx) - elif reaction.emoji == "4️⃣": - if cfg.DEBUG: - print("Reaction selected: 4") - await indices_command(ctx) - elif reaction.emoji == "5️⃣": - if cfg.DEBUG: - print("Reaction selected: 5") - await currencies_command(ctx) - elif reaction.emoji == "6️⃣": - if cfg.DEBUG: - print("Reaction selected: 6") - await feargreed_command(ctx) - elif reaction.emoji == "7️⃣": - if cfg.DEBUG: - print("Reaction selected: 7") - await valuation_command(ctx) - elif reaction.emoji == "8️⃣": - if cfg.DEBUG: - print("Reaction selected: 8") - await performance_command(ctx) + # Getting the right list index + if interaction.component.id == "back": + current -= 1 + elif interaction.component.id == "front": + current += 1 - for emoji in emoji_list: - await msg.remove_reaction(emoji, ctx.bot.user) + # If its out of index, go back to start / end + if current == len(cols): + current = 0 + elif current < 0: + current = len(cols) - 1 - except asyncio.TimeoutError: - for emoji in emoji_list: - await msg.remove_reaction(emoji, ctx.bot.user) - embed = discord.Embed( - description="Error timeout - you snooze you lose! 😋", - colour=cfg.COLOR, - title="TIMEOUT Economy Menu", - ).set_author( - name=cfg.AUTHOR_NAME, - icon_url=cfg.AUTHOR_ICON_URL, - ) - await ctx.send(embed=embed) + # 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 + ) + + # pylint: disable=too-many-branches + @gst_bot.event + async def on_reaction_add(reaction, user): + if user == ctx.message.author and str(reaction.emoji) in emoji_list: + if reaction.emoji == "0️⃣": + if cfg.DEBUG: + print("Reaction selected: 0") + if current == 0: + await overview_command(ctx) + elif current == 1: + await metals_command(ctx) + elif reaction.emoji == "1️⃣": + if cfg.DEBUG: + print("Reaction selected: 1") + if current == 0: + await futures_command(ctx) + elif current == 1: + await meats_command(ctx) + elif reaction.emoji == "2️⃣": + if cfg.DEBUG: + print("Reaction selected: 2") + if current == 0: + await usbonds_command(ctx) + elif current == 1: + await grains_command(ctx) + elif reaction.emoji == "3️⃣": + if cfg.DEBUG: + print("Reaction selected: 3") + if current == 0: + await glbonds_command(ctx) + elif current == 1: + await softs_command(ctx) + elif reaction.emoji == "4️⃣": + if cfg.DEBUG: + print("Reaction selected: 4") + await indices_command(ctx) + elif reaction.emoji == "5️⃣": + if cfg.DEBUG: + print("Reaction selected: 5") + await currencies_command(ctx) + elif reaction.emoji == "6️⃣": + if cfg.DEBUG: + print("Reaction selected: 6") + await feargreed_command(ctx) + elif reaction.emoji == "7️⃣": + if cfg.DEBUG: + print("Reaction selected: 7") + await valuation_command(ctx) + elif reaction.emoji == "8️⃣": + if cfg.DEBUG: + print("Reaction selected: 8") + await performance_command(ctx) + elif reaction.emoji == "9️⃣": + if cfg.DEBUG: + print("Reaction selected: 9") + await energy_command(ctx) + for emoji in emoji_list: + await main_message.remove_reaction(emoji, ctx.bot.user) + 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) + return + + 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) + embed = discord.Embed( + description="Error timeout - you snooze you lose! 😋", + colour=cfg.COLOR, + title="TIMEOUT Economy Menu", + ).set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + await ctx.send(embed=embed) + + for emoji in emoji_list: + await main_message.remove_reaction(emoji, ctx.bot.user) + break def setup(bot: discord.ext.commands.Bot): diff --git a/discordbot/economy/energy.py b/discordbot/economy/energy.py new file mode 100644 index 00000000000..41c8f0b7a8f --- /dev/null +++ b/discordbot/economy/energy.py @@ -0,0 +1,58 @@ +import discord +import pandas as pd +import discordbot.config_discordbot as cfg + +from gamestonk_terminal.economy import finviz_model + + +async def energy_command(ctx): + """Displays energy futures data [Finviz]""" + + try: + # Debug user input + if cfg.DEBUG: + print("\n!economy.energy") + + # Retrieve data + d_futures = finviz_model.get_futures() + + df = pd.DataFrame(d_futures["Energy"]) + df = df.set_index("label") + df = df.sort_values(by="ticker", ascending=False) + + # Debug user output + if cfg.DEBUG: + print(df.to_string()) + + # Output data + if df.empty: + df_str = "No available energy futures data" + else: + df_str = ( + df[["prevClose", "last", "change"]].fillna("").to_string(index=True) + ) + + embed = discord.Embed( + title="Economy: [Finviz] Energy Futures", + description="```" + df_str + "```", + colour=cfg.COLOR, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed) + + except Exception as e: + embed = discord.Embed( + title="ERROR Economy: [Finviz] Energy Futures", + colour=cfg.COLOR, + description=e, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed) diff --git a/discordbot/economy/grains.py b/discordbot/economy/grains.py new file mode 100644 index 00000000000..1a678004253 --- /dev/null +++ b/discordbot/economy/grains.py @@ -0,0 +1,58 @@ +import discord +import pandas as pd +import discordbot.config_discordbot as cfg + +from gamestonk_terminal.economy import finviz_model + + +async def grains_command(ctx): + """Displays grains futures data [Finviz]""" + + try: + # Debug user input + if cfg.DEBUG: + print("\n!economy.grains") + + # Retrieve data + d_futures = finviz_model.get_futures() + + df = pd.DataFrame(d_futures["Grains"]) + df = df.set_index("label") + df = df.sort_values(by="ticker", ascending=False) + + # Debug user output + if cfg.DEBUG: + print(df.to_string()) + + # Output data + if df.empty: + df_str = "No available grains futures data" + else: + df_str = ( + df[["prevClose", "last", "change"]].fillna("").to_string(index=True) + ) + + embed = discord.Embed( + title="Economy: [Finviz] Grains Futures", + description="```" + df_str + "```", + colour=cfg.COLOR, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed) + + except Exception as e: + embed = discord.Embed( + title="ERROR Economy: [Finviz] Grains Futures", + colour=cfg.COLOR, + description=e, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed) diff --git a/discordbot/economy/meats.py b/discordbot/economy/meats.py new file mode 100644 index 00000000000..0234b70fd4c --- /dev/null +++ b/discordbot/economy/meats.py @@ -0,0 +1,58 @@ +import discord +import pandas as pd +import discordbot.config_discordbot as cfg + +from gamestonk_terminal.economy import finviz_model + + +async def meats_command(ctx): + """Displays meats futures data [Finviz]""" + + try: + # Debug user input + if cfg.DEBUG: + print("\n!economy.meats") + + # Retrieve data + d_futures = finviz_model.get_futures() + + df = pd.DataFrame(d_futures["Meats"]) + df = df.set_index("label") + df = df.sort_values(by="ticker", ascending=False) + + # Debug user output + if cfg.DEBUG: + print(df.to_string()) + + # Output data + if df.empty: + df_str = "No available meats futures data" + else: + df_str = ( + df[["prevClose", "last", "change"]].fillna("").to_string(index=True) + ) + + embed = discord.Embed( + title="Economy: [Finviz] Meats Futures", + description="```" + df_str + "```", + colour=cfg.COLOR, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed) + + except Exception as e: + embed = discord.Embed( + title="ERROR Economy: [Finviz] Meats Futures", + colour=cfg.COLOR, + description=e, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed) diff --git a/discordbot/economy/metals.py b/discordbot/economy/metals.py new file mode 100644 index 00000000000..932d4517056 --- /dev/null +++ b/discordbot/economy/metals.py @@ -0,0 +1,58 @@ +import discord +import pandas as pd +import discordbot.config_discordbot as cfg + +from gamestonk_terminal.economy import finviz_model + + +async def metals_command(ctx): + """Displays metals futures data [Finviz]""" + + try: + # Debug user input + if cfg.DEBUG: + print("\n!economy.metals") + + # Retrieve data + d_futures = finviz_model.get_futures() + + df = pd.DataFrame(d_futures["Metals"]) + df = df.set_index("label") + df = df.sort_values(by="ticker", ascending=False) + + # Debug user output + if cfg.DEBUG: + print(df.to_string()) + + # Output data + if df.empty: + df_str = "No available metals futures data" + else: + df_str = ( + df[["prevClose", "last", "change"]].fillna("").to_string(index=True) + ) + + embed = discord.Embed( + title="Economy: [Finviz] Metals Futures", + description="```" + df_str + "```", + colour=cfg.COLOR, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed) + + except Exception as e: + embed = discord.Embed( + title="ERROR Economy: [Finviz] Metals Futures", + colour=cfg.COLOR, + description=e, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed) diff --git a/discordbot/economy/overview.py b/discordbot/economy/overview.py index 95c4a7d2b64..79ba420accd 100644 --- a/discordbot/economy/overview.py +++ b/discordbot/economy/overview.py @@ -39,7 +39,7 @@ async def overview_command(ctx): except Exception as e: embed = discord.Embed( - title="ERROR Economy: [WSK] Market Overview", + title="ERROR Economy: [WSJ] Market Overview", colour=cfg.COLOR, description=e, ) diff --git a/discordbot/economy/softs.py b/discordbot/economy/softs.py new file mode 100644 index 00000000000..8beea0d6d31 --- /dev/null +++ b/discordbot/economy/softs.py @@ -0,0 +1,58 @@ +import discord +import pandas as pd +import discordbot.config_discordbot as cfg + +from gamestonk_terminal.economy import finviz_model + + +async def softs_command(ctx): + """Displays softs futures data [Finviz]""" + + try: + # Debug user input + if cfg.DEBUG: + print("\n!economy.softs") + + # Retrieve data + d_futures = finviz_model.get_futures() + + df = pd.DataFrame(d_futures["Softs"]) + df = df.set_index("label") + df = df.sort_values(by="ticker", ascending=False) + + # Debug user output + if cfg.DEBUG: + print(df.to_string()) + + # Output data + if df.empty: + df_str = "No available softs futures data" + else: + df_str = ( + df[["prevClose", "last", "change"]].fillna("").to_string(index=True) + ) + + embed = discord.Embed( + title="Economy: [Finviz] Softs Futures", + description="```" + df_str + "```", + colour=cfg.COLOR, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed) + + except Exception as e: + embed = discord.Embed( + title="ERROR Economy: [Finviz] Softs Futures", + colour=cfg.COLOR, + description=e, + ) + embed.set_author( + name=cfg.AUTHOR_NAME, + icon_url=cfg.AUTHOR_ICON_URL, + ) + + await ctx.send(embed=embed)