- Args
- Better configs
- Other shit too, idr
This commit is contained in:
Logan Cusano
2022-02-27 13:40:21 -05:00
parent c68cdf9d7b
commit 62968654d2
5 changed files with 146 additions and 62 deletions

123
bot.py
View File

@@ -116,7 +116,8 @@ class Bot(commands.Bot):
@self.command(help="Use this command to have the bot leave your channel",
brief="Leaves the current voice channel")
async def leave(ctx):
async def leave(ctx, member: discord.Member = None):
member = member or ctx.author.display_name
if self.Bot_Connected:
# Stop the sound handlers
self.streamHandler.clean_up()
@@ -128,9 +129,23 @@ class Bot(commands.Bot):
self.stop_sdr()
# 'Unlock' the bot
self.Bot_Connected = False
await ctx.send(f"Goodbye {str(member).capitalize()}.")
else:
await ctx.send(f"{str(member).capitalize()}, I'm not in a channel")
# Add commands for GQRX and OP25
if self.Handler == 'gqrx' or self.Handler == 'op25':
if self.Handler in BotResources.PDB_ACCEPTABLE_HANDLERS.keys():
# Command to display the current config
@self.command(name='displayprofile', help="Use this command to display the current configuration of the bot.\n"
"Example command:\n"
"\t@ displayprofile",
breif="Display current bot config")
async def _displayprofile(ctx, member: discord.Member = None):
member = member or ctx.author.display_name
message = self.display_current_radio_config()
await ctx.send(f"Ok {str(member).capitalize()}\n{message}")
# Command to change the current frequency and mode
@self.command(name='chfreq', help="Use this command to change the frequency the bot is listening to.\n"
"Example GQRX command:\n"
"\tTune to 104.7Mhz Wideband FM (Radio) - '@ chfreq wfm 104700000\n"
@@ -140,21 +155,13 @@ class Bot(commands.Bot):
brief="Changes radio frequency")
async def chfreq(ctx, mode: str, freq: str, member: discord.Member = None):
# Possible band-types that can be used
possible_modes = []
if self.Handler == 'gqrx':
possible_modes = ['wfm', 'fm']
elif self.Handler == 'op25':
possible_modes = ['d', 'p25']
member = member or ctx.author.display_name
# Check to make sure the frequency input matches the syntax needed
if len(freq) >= 6:
self.freq = freq
# Check to make sure the selected mode is valid
if mode in possible_modes:
if mode in self.possible_modes:
self.mode = mode
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the mode to "
@@ -169,7 +176,7 @@ class Bot(commands.Bot):
await self.set_activity()
else:
await ctx.send(f"{str(member).capitalize()}, {mode} is not valid."
f" You may only enter {possible_modes}")
f" You may only enter {self.possible_modes}")
else:
await ctx.send(f"{str(member).capitalize()}, {freq} is not valid. "
f"Please refer to the help page '@ help chfreq'")
@@ -208,12 +215,6 @@ class Bot(commands.Bot):
else:
await ctx.send(f"{str(member).capitalize()}, there is no profile with the name '{profile_name}'")
@self.command(name='displayprofile', hidden=True)
async def _displayprofile(ctx, member: discord.Member = None):
member = member or ctx.author.display_name
message = self.display_current_radio_config()
await ctx.send(f"Ok {str(member).capitalize()}\n{message}")
# Hidden admin commands
@self.command(name='reload', hidden=True)
async def _reload(ctx, module: str, member: discord.Member = None):
@@ -249,20 +250,21 @@ class Bot(commands.Bot):
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}")
print(f"Testing in: {channel}")
bots_online = []
def verify_bot_msg(msg):
if msg.author.id in BotResources.PDB_KNOWN_BOT_IDS:
bots_online.append(msg.author.id)
if msg.author.id in BotResources.PDB_KNOWN_BOT_IDS.keys():
bots_online.append(BotResources.PDB_KNOWN_BOT_IDS[msg.author.id])
await self.wait_until_ready()
# Send the ping command with the prefix the current bot is using
await channel.send(f"{self.Command_Prefix}ping")
seconds_waited = 0
while seconds_waited < 2:
while seconds_waited < 3:
try:
await self.wait_for("message", check=verify_bot_msg, timeout=1)
except asyncio.exceptions.TimeoutError:
@@ -281,35 +283,46 @@ class Bot(commands.Bot):
print("Starting GQRX handler")
from gqrxHandler import GQRXHandler
self.GQRXHandler = GQRXHandler()
self.possible_modes = BotResources.PDB_ACCEPTABLE_HANDLERS['gqrx']['Modes']
elif self.Handler == 'op25':
print("Starting OP25 handler")
from op25Handler import OP25Handler
self.OP25Handler = OP25Handler()
self.possible_modes = BotResources.PDB_ACCEPTABLE_HANDLERS['op25']['Modes']
# Load the proper OPUS library for the device being used
def load_opus(self):
# Check the system type and load the correct library
if self.system_os_type == 'Linux_32':
discord.opus.load_opus('./opus/libopus.so')
elif self.system_os_type == 'Linux_64':
# Linux ARM AARCH64 running 32bit OS
if self.system_os_type == 'Linux_ARMv7l':
discord.opus.load_opus('./opus/libopus_armv7l.so')
# Linux ARM AARCH64 running 64bit OS
if self.system_os_type == 'Linux_AARCH64':
discord.opus.load_opus('./opus/libopus_aarcch64.so')
elif self.system_os_type == 'Windows':
discord.opus.load_opus('./opus/libopus.dll')
# Windows 64bit
if self.system_os_type == 'Windows_x64':
discord.opus.load_opus('./opus/libopus_amd64.dll')
# Check to make sure the selected device is still available and has not changed it's index
def check_device(self):
for device, index in self.Devices_List:
if int(index) == self.DEVICE_ID and str(device) == self.DEVICE_NAME:
return True
# Check to make sure the selected device is still available and has not changed its index
def check_device(self, _override):
# Check to see if an override has been passed
if not _override:
for device, index in self.Devices_List:
if int(index) == self.DEVICE_ID and str(device) == self.DEVICE_NAME:
return True
for device, index in self.Devices_List:
if str(device) == self.DEVICE_NAME:
self.DEVICE_ID = int(index)
return True
for device, index in self.Devices_List:
if str(device) == self.DEVICE_NAME:
self.DEVICE_ID = int(index)
return True
else:
return False
else:
return False
# If an override has been passed just reply true
return True
# Search the ./modules folder for any modules to load
async def check_for_modules(self):
@@ -337,18 +350,19 @@ class Bot(commands.Bot):
# Check and store the OS type of the system for later use
def check_os_type(self):
processor = platform.machine()
if os.name == 'nt':
self.system_os_type = 'Windows'
if processor == "AMD64":
self.system_os_type = 'Windows_x64'
else:
processor = platform.architecture()[0]
if processor == "64bit":
self.system_os_type = 'Linux_64'
elif processor == "32bit":
self.system_os_type = 'Linux_32'
if processor == "aarch64":
self.system_os_type = 'Linux_AARCH64'
elif processor == "armv7l":
self.system_os_type = 'Linux_ARMv7l'
# Check to see if there is only one frequency
def start_sdr(self):
if self.Handler in ['gqrx', 'op25']:
if self.Handler in BotResources.PDB_ACCEPTABLE_HANDLERS.keys():
if type(self.freq) == str:
# Single freq sent
# Stop the SDR if it is running
@@ -372,6 +386,7 @@ class Bot(commands.Bot):
def stop_sdr(self):
if self.sdr_started:
# Wait for the running processes to close
# Close the OP25 handler
if self.Handler == 'op25':
self.OP25Handler.close_op25()
# self.OP25Handler.join()
@@ -381,7 +396,7 @@ class Bot(commands.Bot):
# Set the activity of the bot
async def set_activity(self, connected=True):
if connected:
if self.Handler in ['gqrx', 'op25']:
if self.Handler in BotResources.PDB_ACCEPTABLE_HANDLERS.keys():
if self.profile_name is None:
await self.change_presence(activity=discord.Activity(type=discord.ActivityType.listening,
name=f"{self.freq[:-1]}"
@@ -399,12 +414,14 @@ class Bot(commands.Bot):
await self.change_presence(activity=discord.Game(name=f"@ me"), status=discord.Status.idle)
# Save the current radio settings as a profile
async def save_radio_config(self, profile_name):
async def save_radio_config(self, profile_name: str):
config = configparser.SafeConfigParser()
if os.path.exists('./profiles.ini'):
config.read('./profiles.ini')
profile_name = str(profile_name).upper()
if not config.has_section(str(profile_name)):
config.add_section(str(profile_name))
@@ -426,11 +443,11 @@ class Bot(commands.Bot):
config = configparser.ConfigParser()
if os.path.exists('./profiles.ini'):
config.read('./profiles.ini')
if config.has_section(str(profile_name)):
self.profile_name = profile_name
self.freq = config[str(profile_name)]['Frequency']
self.mode = config[str(profile_name)]['Mode']
self.squelch = float(config[str(profile_name)]['Squelch'])
if config.has_section(str(profile_name).upper()):
self.profile_name = str(profile_name).upper()
self.freq = config[self.profile_name]['Frequency']
self.mode = config[self.profile_name]['Mode']
self.squelch = float(config[self.profile_name]['Squelch'])
if self.sdr_started:
self.start_sdr()
@@ -445,9 +462,9 @@ class Bot(commands.Bot):
def display_current_radio_config(self):
message_body = ""
if self.profile_name:
message_body += f"Profile Name: {self.profile_name}\n"
message_body += f"Profile Name: {str(self.profile_name).upper()}\n"
message_body += f"Frequency: {self.freq}\n" \
f"Mode: {self.mode}\n"
f"Mode: {str(self.mode).upper()}\n"
if self.squelch:
message_body += f"Squelch: {self.squelch}"