def _check_invalid_sub_currencies(self): """ Check if currencies have become invalid If invalid, the currencies will be removed from the subscriber currency list """ try: remove_currencies = defaultdict(list) subscriber_list = self.subscriber_data for channel in subscriber_list: channel_settings = subscriber_list[channel] for currency in channel_settings["currencies"]: if self.market_list is not None: if currency not in self.market_list: remove_currencies[channel].append(currency) for channel in remove_currencies: for currency in remove_currencies[channel]: subscriber_list[channel]["currencies"].remove(currency) logger.error("Removed '{}' from channel {}".format( currency, channel)) if remove_currencies: self._save_subscriber_file(self.subscriber_data) except Exception as e: raise CurrencyException("Failed to validate sub " "currencies: {}".format(str(e)))
async def add_currency(self, ctx, currency): """ Adds a cryptocurrency to the subscriber settings @param ctx - context of the command sent @param currency - the cryptocurrency to add """ try: if not self._check_permission(ctx): return if currency.upper() in self.acronym_list: currency = self.acronym_list[currency.upper()] if "Duplicate" in currency: await self._say_msg(currency) return if currency not in self.market_list: raise CurrencyException( "Currency is invalid: ``{}``".format(currency)) channel = ctx.message.channel.id subscriber_list = self.subscriber_data if channel in subscriber_list: channel_settings = subscriber_list[channel] if currency in channel_settings["currencies"]: await self._say_msg("``{}`` is already added.".format( currency.title())) return channel_settings["currencies"].append(currency) self._save_subscriber_file(self.subscriber_data) await self._say_msg("``{}`` was successfully added.".format( currency.title())) else: await self._say_msg("The channel needs to be subscribed first." ) except Forbidden: pass except CurrencyException as e: logger.error("CurrencyException: {}".format(str(e))) await self._say_msg(e) except CoinMarketException as e: print("An error has occured. See error.log.") logger.error("CoinMarketException: {}".format(str(e))) except Exception as e: print("An error has occured. See error.log.") logger.error("Exception: {}".format(str(e)))
async def add_alert(self, ctx, currency, operator, user_value, fiat, **kwargs): """ Adds an alert to alerts.json @param currency - cryptocurrency to set an alert of @param operator - operator condition to notify the channel @param user_value - price or percent for condition to compare @param fiat - desired fiat currency (i.e. 'EUR', 'USD') """ try: alert_num = None ucase_fiat = self.coin_market.fiat_check(fiat) if currency.upper() in self.acronym_list: currency = self.acronym_list[currency.upper()] if "Duplicate" in currency: await self._say_msg(currency) return if currency not in self.market_list: raise CurrencyException( "Currency is invalid: ``{}``".format(currency)) try: if not self._check_alert(currency, operator, user_value, ucase_fiat, kwargs): await self._say_msg( "Failed to create alert. Current price " "of **{}** already meets the condition." "".format(currency.title())) return except Exception: await self._say_msg("Invalid operator: **{}**".format(operator) ) return user_id = ctx.message.author.id if user_id not in self.alert_data: self.alert_data[user_id] = {} for i in range(1, len(self.alert_data[user_id]) + 2): if str(i) not in self.alert_data[user_id]: alert_num = str(i) if alert_num is None: raise Exception("Something went wrong with adding alert.") alert_cap = int(self.alert_capacity) if int(alert_num) > alert_cap: await self.bot.say( "Unable to add alert, user alert capacity of" " **{}** has been reached.".format(alert_cap)) return alert_list = self.alert_data[user_id] alert_list[alert_num] = {} channel_alert = alert_list[alert_num] channel_alert["currency"] = currency channel_alert["channel"] = ctx.message.channel.id if operator in self.supported_operators: channel_alert["operation"] = operator else: await self._say_msg( "Invalid operator: {}. Your choices are **<*" "*, **<=**, **>**, or **>=**" "".format(operator)) return if kwargs: if "btc" in kwargs: user_value = "{}".format(user_value).rstrip('0') if user_value.endswith('.'): user_value = user_value.replace('.', '') channel_alert["unit"] = { "btc": "{}".format(user_value).rstrip('0') } else: channel_alert["percent"] = ( "{}".format(user_value)).rstrip('0') for arg in kwargs: channel_alert["percent_change"] = arg else: channel_alert["price"] = ( "{:.6f}".format(user_value)).rstrip('0') if channel_alert["price"].endswith('.'): channel_alert["price"] = channel_alert["price"].replace( '.', '') channel_alert["fiat"] = ucase_fiat self._save_alert_file(self.alert_data) await self._say_msg("Alert has been set. This bot will post the " "alert in this specific channel.") except CurrencyException as e: logger.error("CurrencyException: {}".format(str(e))) await self._say_msg(str(e)) except FiatException as e: logger.error("FiatException: {}".format(str(e))) await self._say_msg(str(e)) except Exception as e: print("Failed to add alert. See error.log.") logger.error("Exception: {}".format(str(e)))
async def add_alert(self, ctx, currency, operator, price, fiat): """ Adds an alert to alerts.json @param currency - cryptocurrency to set an alert of @param operator - operator condition to notify the channel @param price - price for condition to compare @param fiat - desired fiat currency (i.e. 'EUR', 'USD') """ try: i = 1 alert_num = None ucase_fiat = self.coin_market.fiat_check(fiat) if currency.upper() in self.acronym_list: currency = self.acronym_list[currency.upper()] if "Duplicate" in currency: await self.bot.say(currency) return if currency not in self.market_list: raise CurrencyException( "Currency is invalid: ``{}``".format(currency)) try: if not self._check_alert_(currency, operator, price, ucase_fiat): await self.bot.say("Failed to create alert. Current price " "of **{}** already meets the condition." "".format(currency.title())) return except Exception: await self.bot.say("Invalid operator: **{}**".format(operator)) return user_id = str(ctx.message.author.id) if user_id not in self.alert_data: self.alert_data[user_id] = {} while i <= len(self.alert_data[user_id]) + 1: if str(i) not in self.alert_data[user_id]: alert_num = str(i) i += 1 if alert_num is None: raise Exception("Something went wrong with adding alert.") alert_cap = int(self.subscriber_data["alert_capacity"]) if int(alert_num) > alert_cap: await self.bot.say( "Unable to add alert, user alert capacity of" " **{}** has been reached.".format(alert_cap)) return alert_list = self.alert_data[user_id] alert_list[alert_num] = {} channel_alert = alert_list[alert_num] channel_alert["currency"] = currency if operator in self.supported_operators: channel_alert["operation"] = operator else: await self.bot.say( "Invalid operator: {}. Your choices are **<*" "*, **<=**, **>**, or **>=**" "".format(operator)) return channel_alert["price"] = ("{:.6f}".format(price)).rstrip('0') if channel_alert["price"].endswith('.'): channel_alert["price"] = channel_alert["price"].replace( '.', '') channel_alert["fiat"] = ucase_fiat with open('alerts.json', 'w') as outfile: json.dump(self.alert_data, outfile, indent=4) await self.bot.say("Alert has been set.") except CurrencyException as e: logger.error("CurrencyException: {}".format(str(e))) await self.bot.say(e) except FiatException as e: logger.error("FiatException: {}".format(str(e))) self.live_on = False await self.bot.say(e) except Exception as e: print("An error has occured. See error.log.") logger.error("Exception: {}".format(str(e)))