diff --git a/src/cogs/commands/portfolio.py b/src/cogs/commands/portfolio.py index fab5f478..fa4dc1ac 100644 --- a/src/cogs/commands/portfolio.py +++ b/src/cogs/commands/portfolio.py @@ -33,15 +33,32 @@ def update_portfolio_db(self, new_db): update_db(new_db, "portfolio") @commands.dm_only() - @portfolios.command(name="add", description="Add a portfolio to the database.") + @portfolios.command( + name="add", description="Add a cryptocurrency portfolio to the database." + ) async def add( self, ctx: commands.Context, - input: Option( + exchange: Option( + str, + description="Provide the name of your crypto exchange.", + required=True, + ), + key: Option( + str, + description="Provide your API key.", + required=True, + ), + secret: Option( str, - description="Provide the follow information: ().", + description="Provide your API secret.", required=True, ), + passphrase: Option( + str, + description="Provide your API passphrase (only used for Kucoin).", + required=False, + ), ) -> None: """ Adds your portfolio to the database. @@ -57,33 +74,17 @@ async def add( The information specified after `!portfolio`. """ - # Split the input using the spaces - input = input.split(" ") - - if len(input) < 3 or len(input) > 4: - raise commands.UserInputError() - - exchange = input[0] - key = input[1] - secret = input[2] - passphrase = None - # Check if the exchange is supported if exchange.lower() not in ["binance", "kucoin"]: raise commands.BadArgument() - # Set the passphrase if necessary if exchange.lower() == "kucoin": - if len(input) == 4: - passphrase = input[3] - else: - raise commands.BadArgument() - - if exchange.lower() == "binance": - if len(input) != 3: - raise commands.BadArgument() + if not passphrase: + raise commands.UserInputError() + ccxt_exchange = ccxt.kucoin({"apiKey": key, "secret": secret}) - exchange = ccxt.binance({"apiKey": key, "secret": secret}) + elif exchange.lower() == "binance": + ccxt_exchange = ccxt.binance({"apiKey": key, "secret": secret}) new_data = pd.DataFrame( { @@ -97,10 +98,15 @@ async def add( index=[0], ) - # Before adding the portfolio, check if it already exists + # TODO: Before adding the portfolio, check if it already exists # Check if the API keys are valid - print(exchange.fetch_status()) + status = ccxt_exchange.fetch_status() + if status["status"] != "ok": + await ctx.respond( + f"Your API keys are not valid! Please check your API keys and try again." + ) + return # Update the databse util.vars.portfolio_db = pd.concat( @@ -109,7 +115,7 @@ async def add( update_db(util.vars.portfolio_db, "portfolio") await ctx.respond( - "Succesfully added your portfolio to the database!\nPlease ensure that you set the API for read-only access." + "Succesfully added your portfolio to the database!\n⚠️ Please ensure that you set the API for read-only access ⚠️" ) # Init Exchanges to start websockets @@ -179,7 +185,7 @@ async def add_error(self, ctx: commands.Context, error: Exception) -> None: ) elif isinstance(error, commands.UserInputError): await ctx.respond( - f"If using `/portfolio add`, you must specify an exchange, key, secret, and optionally a passphrase!" + f"If using `/portfolio add` with Kucoin, you must specify a passphrase!" ) elif isinstance(error, commands.PrivateMessageOnly): await ctx.respond( diff --git a/src/cogs/listeners/on_raw_reaction_add.py b/src/cogs/listeners/on_raw_reaction_add.py index b7de0fde..64dcee33 100644 --- a/src/cogs/listeners/on_raw_reaction_add.py +++ b/src/cogs/listeners/on_raw_reaction_add.py @@ -163,6 +163,10 @@ async def send_dm(self, message: discord.Message, user: discord.User) -> None: None """ + # Check if the message has an embed + if message.embeds == []: + return + # Get the old embed e = message.embeds[0] diff --git a/src/cogs/loops/stock_halts.py b/src/cogs/loops/stock_halts.py index 4fb54312..8ea84f1d 100644 --- a/src/cogs/loops/stock_halts.py +++ b/src/cogs/loops/stock_halts.py @@ -32,10 +32,15 @@ async def halt_embed(self): if afterHours(): return + # Get the data + html = await self.get_halt_data() + + if html == {}: + return + # Remove previous message first await self.channel.purge(limit=1) - html = await self.get_halt_data() df = self.format_df(html) # Create embed diff --git a/src/util/tv_data.py b/src/util/tv_data.py index d6613d01..135645f0 100644 --- a/src/util/tv_data.py +++ b/src/util/tv_data.py @@ -281,13 +281,15 @@ async def get_tv_data( print("TradingView websocket Error") await session.close() return (0, None, 0, None, website) + except aiohttp.ClientConnectionError: print("Temporary TradingView websocket error") - return (0, None, 0, None, website) except Exception: print(traceback.format_exc()) + return (0, None, 0, None, website) + def format_analysis(self, analysis: dict) -> str: """ Simple helper function to format the TA data into one string.