async def set_presence(self, *, status_identifier=None, status_by_key=True, activity_identifier=None, activity_by_key=True, activity_message=None): activity = status = None if status_identifier is None: status_identifier = self.bot.config.get('status', None) status_by_key = False try: if status_by_key: status = Status[status_identifier] else: status = Status(status_identifier) except (KeyError, ValueError): if status_identifier is not None: msg = f'Invalid status type: {status_identifier}' logger.warning(error(msg)) if activity_identifier is None: if activity_message is not None: raise ValueError('activity_message must be None ' 'if activity_identifier is None.') activity_identifier = self.bot.config.get('activity_type', None) activity_by_key = False try: if activity_by_key: activity_type = ActivityType[activity_identifier] else: activity_type = ActivityType(activity_identifier) except (KeyError, ValueError): if activity_identifier is not None: msg = f'Invalid activity type: {activity_identifier}' logger.warning(error(msg)) else: url = None activity_message = (activity_message or self.bot.config.get( 'activity_message', '')).strip() if activity_type == ActivityType.listening: if activity_message.lower().startswith('to '): # The actual message is after listening to [...] # discord automatically add the "to" activity_message = activity_message[3:].strip() elif activity_type == ActivityType.streaming: url = self.bot.config.get( 'twitch_url', 'https://www.twitch.tv/discord-modmail/') if activity_message: activity = Activity(type=activity_type, name=activity_message, url=url) else: msg = 'You must supply an activity message to use custom activity.' logger.warning(error(msg)) await self.bot.change_presence(activity=activity, status=status) presence = { 'activity': (None, 'No activity has been set.'), 'status': (None, 'No status has been set.') } if activity is not None: to = 'to ' if activity.type == ActivityType.listening else '' msg = f'Activity set to: {activity.type.name.capitalize()} ' msg += f'{to}{activity.name}.' presence['activity'] = (activity, msg) if status is not None: msg = f'Status set to: {status.value}.' presence['status'] = (status, msg) return presence
async def set_presence( self, *, status_identifier=None, status_by_key=True, activity_identifier=None, activity_by_key=True, activity_message=None, ): activity = status = None if status_identifier is None: status_identifier = self.bot.config.get("status", None) status_by_key = False try: if status_by_key: status = Status[status_identifier] else: status = Status(status_identifier) except (KeyError, ValueError): if status_identifier is not None: msg = f"Invalid status type: {status_identifier}" logger.warning(error(msg)) if activity_identifier is None: if activity_message is not None: raise ValueError( "activity_message must be None " "if activity_identifier is None." ) activity_identifier = self.bot.config.get("activity_type", None) activity_by_key = False try: if activity_by_key: activity_type = ActivityType[activity_identifier] else: activity_type = ActivityType(activity_identifier) except (KeyError, ValueError): if activity_identifier is not None: msg = f"Invalid activity type: {activity_identifier}" logger.warning(error(msg)) else: url = None activity_message = ( activity_message or self.bot.config.get("activity_message", "") ).strip() if activity_type == ActivityType.listening: if activity_message.lower().startswith("to "): # The actual message is after listening to [...] # discord automatically add the "to" activity_message = activity_message[3:].strip() elif activity_type == ActivityType.streaming: url = self.bot.config.get( "twitch_url", "https://www.twitch.tv/discord-modmail/" ) if activity_message: activity = Activity(type=activity_type, name=activity_message, url=url) else: msg = "You must supply an activity message to use custom activity." logger.warning(error(msg)) await self.bot.change_presence(activity=activity, status=status) presence = { "activity": (None, "No activity has been set."), "status": (None, "No status has been set."), } if activity is not None: use_to = "to " if activity.type == ActivityType.listening else "" msg = f"Activity set to: {activity.type.name.capitalize()} " msg += f"{use_to}{activity.name}." presence["activity"] = (activity, msg) if status is not None: msg = f"Status set to: {status.value}." presence["status"] = (status, msg) return presence
async def on_ready(self): """Bot startup, sets uptime.""" await self._connected.wait() print( dedent(f""" {LINE} {Fore.CYAN}Client ready. {LINE} {Fore.CYAN}Logged in as: {self.user} {Fore.CYAN}User ID: {self.user.id} {Fore.CYAN}Guild ID: {self.guild.id if self.guild else 0} {LINE}""").strip()) if not self.guild: print(f'{Fore.RED}{Style.BRIGHT}WARNING - The GUILD_ID ' f'provided does not exist!{Style.RESET_ALL}') else: await self.threads.populate_cache() # Wait until config cache is populated with stuff from db await self.config.wait_until_ready() # activities activity_type = self.config.get('activity_type', -1) message = self.config.get('activity_message', '') try: activity_type = ActivityType(activity_type) except ValueError: activity_type = -1 if activity_type >= 0 and message: normalized_message = message.strip() if activity_type == ActivityType.listening: if message.lower().startswith('to '): # Must be listening to... normalized_message = message[3:].strip() else: normalized_message = '' if normalized_message: if activity_type == ActivityType.streaming: url = self.config.get( 'twitch_url', 'https://www.twitch.tv/discord-modmail/') else: url = None activity = discord.Activity(type=activity_type, name=normalized_message, url=url) await self.change_presence(activity=activity) # TODO: Trim message print(f'{Fore.CYAN}Activity set to: ' f'{activity_type.name} {message}.') else: print(f'{Fore.CYAN}No activity message set.') # closures closures = self.config.closures.copy() print(f'{Fore.CYAN}There are {len(closures)} thread(s) pending ' 'to be closed.') for recipient_id, items in closures.items(): after = (datetime.fromisoformat(items['time']) - datetime.utcnow()).total_seconds() if after < 0: after = 0 recipient = self.get_user(int(recipient_id)) thread = await self.threads.find(recipient=recipient) if not thread: # If the recipient is gone or channel is deleted self.config.closures.pop(str(recipient_id)) await self.config.update() continue # TODO: Low priority, # Retrieve messages/replies when bot is down, from history? await thread.close(closer=self.get_user(items['closer_id']), after=after, silent=items['silent'], delete_channel=items['delete_channel'], message=items['message']) print(LINE)
async def set_presence(self, *, status_identifier=None, status_by_key=True, activity_identifier=None, activity_by_key=True, activity_message=None): activity = status = None if status_identifier is None: status_identifier = self.bot.config.get('status', None) status_by_key = False try: if status_by_key: status = Status[status_identifier] else: status = Status(status_identifier) except (KeyError, ValueError): if status_identifier is not None: msg = f'Invalid status type: {status_identifier}' logger.warning(error(msg)) if activity_identifier is None: if activity_message is not None: raise ValueError('activity_message doit être None ' 'si activity_identifier est None.') activity_identifier = self.bot.config.get('activity_type', None) activity_by_key = False try: if activity_by_key: activity_type = ActivityType[activity_identifier] else: activity_type = ActivityType(activity_identifier) except (KeyError, ValueError): if activity_identifier is not None: msg = f'Type d\'activité invalide: {activity_identifier}' logger.warning(error(msg)) else: url = None activity_message = (activity_message or self.bot.config.get( 'activity_message', '')).strip() if activity_type == ActivityType.listening: if activity_message.lower().startswith('to '): # The actual message is after listening to [...] # discord automatically add the "to" activity_message = activity_message[3:].strip() elif activity_type == ActivityType.streaming: url = self.bot.config.get('twitch_url', 'https://www.twitch.tv/discordfr_') if activity_message: activity = Activity(type=activity_type, name=activity_message, url=url) else: msg = 'Vous devez fournir un message d\'activité pour utiliser une activité personnalisée.' logger.warning(error(msg)) await self.bot.change_presence(activity=activity, status=status) presence = { 'activity': (None, 'Aucune activité n\'a été définie.'), 'status': (None, 'Aucun statut n\'a été défini.') } if activity is not None: to = 'à ' if activity.type == ActivityType.listening else '' msg = f'Activité réglée sur: {activity.type.name.capitalize()} ' msg += f'{to}{activity.name}.' presence['activity'] = (activity, msg) if status is not None: msg = f'Statut défini sur: {status.value}.' presence['status'] = (status, msg) return presence