diff --git a/BotResources.py b/BotResources.py index 4052be6..b205b11 100644 --- a/BotResources.py +++ b/BotResources.py @@ -3,6 +3,7 @@ import configparser from os.path import exists PDB_ACCEPTABLE_HANDLERS = ['gqrx', 'op25'] +PDB_KNOWN_BOT_IDS = [756327271597473863, 915064996994633729, 943742040255115304] def check_if_config_exists(): diff --git a/bot.py b/bot.py index 05add51..80977cf 100644 --- a/bot.py +++ b/bot.py @@ -1,6 +1,9 @@ +import asyncio import os import platform import discord + +import BotResources import sound import configparser from discord.ext import commands @@ -22,6 +25,7 @@ class Bot(commands.Bot): self.Default_Channel_ID = kwargs['Channel_ID'] self.Default_Mention_Group = kwargs['Mention_Group'] self.Handler = kwargs['Handler'] + self.Command_Prefix = kwargs['command_prefix'] # Init Variable for sound self.streamHandler = None @@ -51,20 +55,8 @@ class Bot(commands.Bot): # Add discord commands to the bot self.add_commands() - # Check the ./modules folder for any modules (cog.py) - self.check_for_modules() - - # Check the handler being used during init - def check_handler(self): - if self.Handler == "gqrx": - print("Starting GQRX handler") - from gqrxHandler import GQRXHandler - self.GQRXHandler = GQRXHandler() - - elif self.Handler == 'op25': - print("Starting OP25 handler") - from op25Handler import OP25Handler - self.OP25Handler = OP25Handler() + # Add discord events to the bot + self.add_events() # Start the bot def start_bot(self): @@ -77,6 +69,7 @@ class Bot(commands.Bot): async def ping(ctx): if ctx.author.id != self.user.id: await ctx.send('pong') + await self.process_commands(ctx) # Command to join the bot the voice channel the user who called the command is in @self.command(help="Use this command to join the bot to your channel", @@ -234,6 +227,52 @@ class Bot(commands.Bot): async def _stopsdr(ctx, member: discord.Member = None): self.stop_sdr() + # Add discord events to the bot + def add_events(self): + @self.event + async def on_ready(): + # Check the ./modules folder for any modules (cog.py) + await self.check_for_modules() + + # Check to see if other bots are online + async def check_other_bots_online(self): + print('Checking if other bots are online') + channel = self.get_channel(self.Default_Channel_ID) + print(f"Channel to be tested in: {channel}") + + bots_online = [] + + def verify_bot_msg(msg): + print(f"Response ID: {msg.author.id}") + if msg.author.id in BotResources.PDB_KNOWN_BOT_IDS: + bots_online.append(msg.author.id) + + await self.wait_until_ready() + + await channel.send(f"{self.Command_Prefix}ping") + + seconds_waited = 0 + while seconds_waited < 5: + try: + await self.wait_for("message", check=verify_bot_msg, timeout=1) + except asyncio.exceptions.TimeoutError: + seconds_waited += 1 + print(seconds_waited) + + print(f"Bots Online: {bots_online}") + + # Check the handler being used during init + def check_handler(self): + if self.Handler == "gqrx": + print("Starting GQRX handler") + from gqrxHandler import GQRXHandler + self.GQRXHandler = GQRXHandler() + + elif self.Handler == 'op25': + print("Starting OP25 handler") + from op25Handler import OP25Handler + self.OP25Handler = OP25Handler() + # Load the proper OPUS library for the device being used def load_opus(self): # Check the system type and load the correct library @@ -259,7 +298,10 @@ class Bot(commands.Bot): return False # Search the ./modules folder for any modules to load - def check_for_modules(self): + async def check_for_modules(self): + print('Checking modules') + await self.check_other_bots_online() + # A valid module must be built as a 'cog', refer to the docs for more information for folder_name in os.listdir("modules"): if str(folder_name)[0] == '.': @@ -386,5 +428,3 @@ class Bot(commands.Bot): return False else: return False - -