async def prefix_change(self, context: commands.Context, new_prefix: str): """ Changes the current server command prefix Args: context (commands.Context): context of invocation new_prefix (str): the new prefix to be set """ guild_id: int = context.guild.id Log.info( f'Changing prefix in guild "{context.guild.name} to "{new_prefix}"') set_server_prefix(guild_id, new_prefix) await context.send(f'Changed server prefix to {new_prefix}')
def load_guild_latest(guild_id: int): # Path to reference to callendar-events collection events_ref_string = f'bot-root/{guild_id}/calendar-events' # Firestore client db_client: FirestoreClient = firestore.client() # Collection holding the callendar events events_ref = db_client.collection(events_ref_string) # Retriving and sorting all callendar events query = events_ref.order_by(u'time', direction='ASCENDING').stream() for doc in query: Log.warning(f'{doc.id} => {doc.to_dict()}')
def set_server_prefix(guild_id: int, prefix: str) -> None: """ Updates server prefix Args: guild_id (int): guild id prefix (str): prefix to be set """ Log.info(f'Updating server({guild_id}) prefix to {prefix}') # Retrive Firestore Client db_client: FirestoreClient = firestore.client() # Path to server config path_to_server_config: str = f'bot-root/{guild_id}/server-specific/server-config' # Update cached prefix server_prefixes[guild_id] = prefix # Retrive document pointer doc_ref: DocumentReference = db_client.document(path_to_server_config) # Update value doc_ref.set({'prefix': prefix}, merge=True)
def get_server_prefix(guild_id: int) -> str: """ Retrives sever prefix for given guild-id from firestore and caches it Returns: str: server prefix """ # If prefix is arleady cached if guild_id in server_prefixes: Log.debug(f'Using cached prefix for server {guild_id}') return server_prefixes[guild_id] ################################# Log.debug(f'Retriving prefix from firestore for server {guild_id}') # Retrive firestore client db_client: FirestoreClient = firestore.client() # Path to the document holding server configuration path_to_server_config: str = f'bot-root/{guild_id}/server-specific/server-config' # Get document reference doc_reference: DocumentReference = db_client.document( path_to_server_config) # Retrive server configuration server_configuration: dict = doc_reference.get().to_dict() # Check if server configuration exists if server_configuration is None: Log.debug( f'Firestore entry does noe exist for guild {guild_id}, creating entry') server_prefixes[guild_id] = '' doc_reference.set({'prefix': ''}) # Sets document with return '' prefix = server_configuration.get('prefix', '') server_prefixes[guild_id] = prefix return prefix
async def on_command_error(self, ctx: commands.Context, error: commands.CommandError) -> None: """ Listener to handle error caused by improper use of bot commands Args: ctx (commands.Context): context that invoked error error (commands.CommandError): error that was invoked """ Log.error(f'Command error: "{error}"') # Check for error caused by DM bot directly with forbitted command if isinstance(error, commands.NoPrivateMessage): Log.warning( f'User: "******" id:{ctx.author.id} ' 'attempted to use command' f'"{ctx.command}" in private DM which is not permitted') await ctx.reply( f'Command "{ctx.command}" cannot be used in a private message. Sorry :(') if isinstance(error, commands.MissingPermissions): Log.warning( f'User: "******" id:{ctx.author.id}' f'attempted to use command "{ctx.command}" without needed permissions') await ctx.reply('You need Admin permissions to use that command')
def next_uid(guild_id: int): Log.debug(f'Genereting new uid for callendar event in guild {guild_id}') # Initialize random seed random.seed() # Random number from 1000 to 9999 inclusive rand_number: int = random.randrange(1000, 10000) # The following part checks if the random id is unique db_client: FirestoreClient = firestore.client() # The firestore client # String path to events path_to_events: str = f'bot-root/{guild_id}/calendar-events' coll_ref = db_client.collection(path_to_events) # Collection reference Log.debug('Accesing firestore to verify uid') query = coll_ref.where(u'id', u'==', rand_number).get() # If it's unique already if not query: Log.debug(f'Generated {rand_number}') return rand_number else: return next_uid(guild_id)
def main() -> None: """ Main function of bot application """ print('Starting up') # Setting up the configuration configuration.load_configuration() # Setup Loggers from config Log.config_init() Log.info('Logging is now available') # Retrive the app configuration bot_configuration: configuration.Config = configuration.get_config() # List the given configuration Log.warning('Listing bot configuration:') for key, value in bot_configuration.__dict__.items(): Log.warning(f'\t{key}: {value}') Log.warning('End of configuration') # Setting up the firebase Log.warning('Initializing firebase connection') cred = credentials.Certificate('.firebase') firebase_admin.initialize_app(credential=cred) Log.warning('Firebase was initiatied successfully') # Creating bot instance Log.warning('Creating BotClient instance') client = BotClient() # Run client client.run()
async def on_ready(self): """ Action that will be invoked when bot logs in into discord """ Log.info('Bot is ready') Log.info(self.client.emojis)