async def store_latest_chat_messages(self, channel: TextChannel, is_backfill: bool = False) -> None: """ Attempts to load chat messages since the last timestamp in the database. @param channel: The discord text channel. @param is_backfill: Whether or not backfill from beginning of time. """ last_timestamp = self.db_manager.get_last_message_timestamp(channel.id) after = (datetime.utcfromtimestamp(last_timestamp) if last_timestamp else None) if is_backfill: after = None self.db_manager.reset_cache() messages_processed = 0 async for msg in channel.history(limit=None, after=after): self.db_manager.add_new_message( discord_user=DiscordUser(msg.author.name, msg.author.display_name, msg.author.discriminator), timestamp=int( msg.created_at.replace(tzinfo=timezone.utc).timestamp()), message_channel_id=channel.id, message_word_count=len(msg.content.split()), message_char_count=len(msg.content)) messages_processed += 1 if messages_processed % 100 == 0: print(messages_processed)
def handle_add_alert_command(self, author: discord.User, channel: TextChannel, low: int, high: int, symbol: str, note: str) -> str: """ Adds a stock alert, if symbol is not being tracked it will attempt to track it :param author: _description_ :param channel: _description_ :param low: _description_ :param high: _description_ :param symbol: _description_ :param note: _description_ :return: _description_ """ print(f'Adding alert {symbol}: {low} {high}') symbols = set(self.db_manager.get_all_tracking_symbols()) if symbol not in symbols: valid = self.finance_manager.check_valid_stock( self.finance_manager.get_stock_item(symbol)) if not valid: return f'Alert wasnt added, {symbol} is not a valid symbol.' self.db_manager.add_stock_track(symbol) self.db_manager.add_stock_alert( discord_user=DiscordUser(author.name, author.display_name, author.discriminator), timestamp=datetime.now(timezone.utc).timestamp(), channel_id=channel.id, symbol=symbol, low=low, high=high, note=note) return f'Alert from {author.display_name} for {symbol} added!'
def test_user_get_or_create() -> None: DB.get_instance().setup_db('sqlite://') test_user = DiscordUser("John", "Jonny", "1234") with DB.get_instance().make_session() as db_session: user_id = User.get_or_create(db_session=db_session, discord_user=test_user) assert user_id is not None assert user_id == User.get_or_create(db_session=db_session, discord_user=test_user)
def upsert_gif_keyword(self, author: discord.User, keyword: str) -> None: """ Inserts a Gif keyword preference for a particular user. @param author: A Discord User @param keyword: Keyword used to find a gif. @return: None """ self.db_manager.upsert_new_gif_entry(discord_user=DiscordUser( author.name, author.display_name, author.discriminator), keyword=keyword)
def handle_gif_cooldown(self, author: discord.User, message_ts: int) -> str: """ Handles whether or not the bot should post a Gif to the discord Channel. @param author: A Discord User @param message_ts: Timestamp of the latest message sent by user. @return: a Gif url string. """ gif_url = "" discord_user = DiscordUser(author.name, author.display_name, author.discriminator) (keyword, gif_timestamp) = self.db_manager.get_last_gif_preference(discord_user) if keyword: if message_ts - gif_timestamp >= 60 * 60 * 24 * 3: # 3 days gif_url = self.media_manager.get_gif(keyword) self.db_manager.upsert_new_gif_entry(discord_user=discord_user, keyword=keyword, timestamp=message_ts) return gif_url
def _get_sample_users() -> List[DiscordUser]: return [DiscordUser("John", "Jonny", "1234"), DiscordUser("Jane", "Jenny", "4312"), DiscordUser("Bob", "Bobby", "3134"), DiscordUser("Michael", "Mike", "2312")]
def test_gif_workflow() -> None: """ Tests the 'upsert' and 'get' method of the Gif class. Test procedure: 1- Reads empty db. 2- Inserts data to empty db using upsert. 3- Reads db. 4- Updates data to db using upsert. 5- Reads updated db. """ users = [ DiscordUser("John", "Jonny", "1234"), DiscordUser("Jane", "Jenny", "4312"), DiscordUser("Bob", "Bobby", "3134"), DiscordUser("Michael", "Mike", "2312") ] preferences = [(users[0], "Matrix", 150), (users[1], "The Notebook", 150000), (users[2], "Stranger Things", 0), (users[3], "Iron Man", 10000)] result = {} select_person = users[2] update_data = (select_person, "Twin Peaks", 75000) output = { 'Read empty': ('', 0), 'Initial preference': ('Stranger Things', 0), 'Read update': ('Twin Peaks', 75000) } DB.get_instance().setup_db("sqlite://") analytics_engine = AnalyticsEngine() database_manager = DatabaseManager() plot_manager = PlotManager() finance_manager = FinanceManager() media_manager = MediaManager("") discord_manager = DiscordManager(db_manager=database_manager, analytics_engine=analytics_engine, media_manager=media_manager, plot_manager=plot_manager, finance_manager=finance_manager) # Query empty DB for Bob - should return nothing result["Read empty"] = \ discord_manager.db_manager.get_last_gif_preference(select_person) # discord_manager.db_manager.upsert_new_gif_entry() for preference in preferences: (user_name, keyword, timestamp) = preference discord_manager.db_manager.upsert_new_gif_entry( user_name, keyword, timestamp) # Read for Bob - should initial preference result[ "Initial preference"] = discord_manager.db_manager.get_last_gif_preference( select_person) # Update Bob's preferences (user_name, keyword, timestamp) = update_data discord_manager.db_manager.upsert_new_gif_entry(user_name, keyword, timestamp) # Read for Bob - should initial preference result["Read update"] = discord_manager.db_manager.get_last_gif_preference( select_person) assert (result == output)