From 15cfa5bf9473413cb23325e2a65d5f0222a15875 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Fri, 18 Feb 2022 20:54:17 -0500 Subject: [PATCH 1/8] Added check to see if the user sending ping is itself - This will be important to figure out who is a bot --- bot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index c20698a..05add51 100644 --- a/bot.py +++ b/bot.py @@ -75,7 +75,8 @@ class Bot(commands.Bot): # Test command to see if the bot is on (help command can also be used) @self.command(help="Use this to test if the bot is alive", brief="Sends a 'pong' in response") async def ping(ctx): - await ctx.send('pong') + if ctx.author.id != self.user.id: + await ctx.send('pong') # 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", From 90ad6f89cd4d3cb971473175414dd00ebf64d963 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Fri, 18 Feb 2022 22:18:13 -0500 Subject: [PATCH 2/8] Check the main variable for bot IDs --- modules/LinkCop/cog.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/modules/LinkCop/cog.py b/modules/LinkCop/cog.py index b2ccdf9..4942376 100644 --- a/modules/LinkCop/cog.py +++ b/modules/LinkCop/cog.py @@ -2,6 +2,7 @@ import re import discord from discord.ext import commands import random +from BotResources import PDB_KNOWN_BOT_IDS regex_link = re.compile('(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)') @@ -40,11 +41,12 @@ class LinkCop(commands.Cog): 758792783020163084 # Bots ] - self.whitelisted_ID = [ - 756327271597473863, # Greada ID - 915064996994633729, # Jorn ID + # Bring in the known bot IDs from PDB bots + self.whitelisted_ID = PDB_KNOWN_BOT_IDS + + self.whitelisted_ID.append([ 235148962103951360 # Carl Bot - ] + ]) self.add_events() From 04f3d25fd93872f4891d95132a267bc8ff0f40a0 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Fri, 18 Feb 2022 22:19:36 -0500 Subject: [PATCH 3/8] Add the initial revision of 'check for other bots' --- BotResources.py | 1 + bot.py | 74 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 58 insertions(+), 17 deletions(-) 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 - - From 54c2eeedbff4d04b8adbfefcc52b6190673c5af2 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Fri, 18 Feb 2022 22:34:47 -0500 Subject: [PATCH 4/8] Trying an on_message function that gets disabled after the bot is fully loaded --- bot.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 80977cf..4259566 100644 --- a/bot.py +++ b/bot.py @@ -27,6 +27,9 @@ class Bot(commands.Bot): self.Handler = kwargs['Handler'] self.Command_Prefix = kwargs['command_prefix'] + # Init variable to check if startup has completed + self.Startup_Complete = False + # Init Variable for sound self.streamHandler = None @@ -69,7 +72,6 @@ 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", @@ -229,11 +231,20 @@ class Bot(commands.Bot): # Add discord events to the bot def add_events(self): + # Run any functions that need to have the bot running to complete @self.event async def on_ready(): # Check the ./modules folder for any modules (cog.py) await self.check_for_modules() + # Set the startup completed variable as true + self.Startup_Complete = True + + @self.event + async def on_message(message): + if not self.Startup_Complete: + await self.process_commands(message) + # Check to see if other bots are online async def check_other_bots_online(self): print('Checking if other bots are online') From 57225413fc2fcb6d468bc6f381fe8345a84f73f1 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Fri, 18 Feb 2022 22:43:15 -0500 Subject: [PATCH 5/8] Trying an on_message function that gets disabled after the bot is fully loaded --- bot.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/bot.py b/bot.py index 4259566..0821d5c 100644 --- a/bot.py +++ b/bot.py @@ -27,9 +27,6 @@ class Bot(commands.Bot): self.Handler = kwargs['Handler'] self.Command_Prefix = kwargs['command_prefix'] - # Init variable to check if startup has completed - self.Startup_Complete = False - # Init Variable for sound self.streamHandler = None @@ -220,7 +217,7 @@ class Bot(commands.Bot): await ctx.send(f"Ok {str(member).capitalize()}, I reloaded {module}") else: await ctx.send(f"{str(member).capitalize()}, something went wrong. Please check the console") - + @self.command(name='startsdr', hidden=True) async def _startsdr(ctx, member: discord.Member = None): self.start_sdr() @@ -237,12 +234,9 @@ class Bot(commands.Bot): # Check the ./modules folder for any modules (cog.py) await self.check_for_modules() - # Set the startup completed variable as true - self.Startup_Complete = True - @self.event async def on_message(message): - if not self.Startup_Complete: + if "ping" in message.content: await self.process_commands(message) # Check to see if other bots are online @@ -263,12 +257,11 @@ class Bot(commands.Bot): await channel.send(f"{self.Command_Prefix}ping") seconds_waited = 0 - while seconds_waited < 5: + while seconds_waited < 2: 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}") @@ -372,7 +365,7 @@ class Bot(commands.Bot): # Wait for the running processes to close if self.Handler == 'op25': self.OP25Handler.close_op25() - #self.OP25Handler.join() + # self.OP25Handler.join() # Need a way to 'close' GQRX self.sdr_started = False From eaaeee710946d9415b77b91604b77be7ed756a75 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Fri, 18 Feb 2022 22:51:51 -0500 Subject: [PATCH 6/8] Trying an on_message function that gets disabled after the bot is fully loaded --- bot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot.py b/bot.py index 0821d5c..4d56726 100644 --- a/bot.py +++ b/bot.py @@ -236,6 +236,7 @@ class Bot(commands.Bot): @self.event async def on_message(message): + print(message.content) if "ping" in message.content: await self.process_commands(message) From e36c77a7a544a7d9d271fc511a0be39690da9f72 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Fri, 18 Feb 2022 23:08:01 -0500 Subject: [PATCH 7/8] BUGFIX - Working on the startup check --- bot.py | 37 ++++++++++++++++++++++++------------- modules/LinkCop/cog.py | 3 ++- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/bot.py b/bot.py index 4d56726..850d6ae 100644 --- a/bot.py +++ b/bot.py @@ -236,9 +236,7 @@ class Bot(commands.Bot): @self.event async def on_message(message): - print(message.content) - if "ping" in message.content: - await self.process_commands(message) + await self.check_and_reply_to_ping(message) # Check to see if other bots are online async def check_other_bots_online(self): @@ -266,6 +264,11 @@ class Bot(commands.Bot): print(f"Bots Online: {bots_online}") + if len(bots_online) == 0: + return False + elif len(bots_online) > 0: + return True + # Check the handler being used during init def check_handler(self): if self.Handler == "gqrx": @@ -304,16 +307,15 @@ class Bot(commands.Bot): # Search the ./modules folder for any modules to load 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] == '.': - continue - elif os.path.exists(os.path.join("modules", folder_name, "cog.py")): - print(f"Loaded extension: {folder_name}") - self.load_extension(f"modules.{folder_name}.cog") + # Check to see if other bots are online and don't load the modules if they are + if not 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] == '.': + continue + elif os.path.exists(os.path.join("modules", folder_name, "cog.py")): + print(f"Loaded extension: {folder_name}") + self.load_extension(f"modules.{folder_name}.cog") # Reload a selected module for changes def reload_modules(self, module): @@ -433,3 +435,12 @@ class Bot(commands.Bot): return False else: return False + + # Check if message is a ping request and respond even if it is a bot + async def check_and_reply_to_ping(self, message): + if "ping" in message.content: + ctx = await self.get_context(message) + await self.invoke(ctx) + return True + else: + return False diff --git a/modules/LinkCop/cog.py b/modules/LinkCop/cog.py index 4942376..ab8fcbd 100644 --- a/modules/LinkCop/cog.py +++ b/modules/LinkCop/cog.py @@ -73,7 +73,8 @@ class LinkCop(commands.Cog): print(f"Link Cop Error: '{err}'") print(f"Bot or other non-user that has not been whitelisted sent a message") - await self.bot.process_commands(ctx) + if not await self.bot.check_and_reply_to_ping(ctx): + await self.bot.process_commands(ctx) async def send_message(self, message): send_channel = self.bot.get_channel(id=self.reply_channel_id) From 174d3bcb57199216d5b33b01d2fb995e3d417bd5 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Fri, 18 Feb 2022 23:12:22 -0500 Subject: [PATCH 8/8] BUGFIX - Working on the startup check --- bot.py | 2 +- modules/LinkCop/cog.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index 850d6ae..bd8e063 100644 --- a/bot.py +++ b/bot.py @@ -247,7 +247,6 @@ class Bot(commands.Bot): 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) @@ -443,4 +442,5 @@ class Bot(commands.Bot): await self.invoke(ctx) return True else: + await self.process_commands(message) return False diff --git a/modules/LinkCop/cog.py b/modules/LinkCop/cog.py index ab8fcbd..9d70c13 100644 --- a/modules/LinkCop/cog.py +++ b/modules/LinkCop/cog.py @@ -73,8 +73,7 @@ class LinkCop(commands.Cog): print(f"Link Cop Error: '{err}'") print(f"Bot or other non-user that has not been whitelisted sent a message") - if not await self.bot.check_and_reply_to_ping(ctx): - await self.bot.process_commands(ctx) + await self.bot.check_and_reply_to_ping(ctx) async def send_message(self, message): send_channel = self.bot.get_channel(id=self.reply_channel_id)