New logging
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import configparser
|
||||
import logging
|
||||
from datetime import date
|
||||
from os.path import exists
|
||||
from NoiseGatev2 import AudioStream
|
||||
|
||||
@@ -12,6 +14,8 @@ PDB_KNOWN_BOT_IDS = {756327271597473863: "Greada", 915064996994633729: "Jorn", 9
|
||||
|
||||
DEFAULT_NOISEGATE_THRESHOLD = 50
|
||||
|
||||
LOGGER = logging.getLogger('Discord_Radio_Bot.Bot_Resources')
|
||||
|
||||
|
||||
def check_if_config_exists():
|
||||
if exists('./config.ini'):
|
||||
@@ -38,9 +42,9 @@ def read_config_file():
|
||||
'Handler': str(config['Config']['Handler'])
|
||||
}
|
||||
|
||||
print("Found config options:")
|
||||
LOGGER.debug("Found config options:")
|
||||
for key in config_return.keys():
|
||||
print(f"\t{key} : {config_return[key]}")
|
||||
LOGGER.debug(f"\t{key} : {config_return[key]}")
|
||||
|
||||
return config_return
|
||||
|
||||
@@ -91,14 +95,14 @@ def write_config_file(**kwargs):
|
||||
with open('./config.ini', 'w') as config_file:
|
||||
config.write(config_file)
|
||||
|
||||
print("Config Saved")
|
||||
LOGGER.info("Config Saved")
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def get_device_list():
|
||||
list_of_devices = sound.query_devices().items()
|
||||
print(list_of_devices)
|
||||
list_of_devices = AudioStream().list_devices()
|
||||
LOGGER.debug(list_of_devices)
|
||||
return list_of_devices
|
||||
|
||||
|
||||
@@ -106,38 +110,38 @@ def get_user_device_selection():
|
||||
device_list = get_device_list()
|
||||
org_device_list = []
|
||||
for device, dev_id in device_list:
|
||||
print(f"{dev_id + 1}\t-\t{device}")
|
||||
LOGGER.debug(f"{dev_id + 1}\t-\t{device}")
|
||||
org_device_list.append((dev_id, device))
|
||||
selected_list_id = None
|
||||
selected_device = None
|
||||
while not selected_list_id:
|
||||
print(f"selected device: {selected_list_id}")
|
||||
print(device_list)
|
||||
LOGGER.debug(f"selected device: {selected_list_id}")
|
||||
LOGGER.debug(device_list)
|
||||
|
||||
try:
|
||||
selected_list_id = int(input(f"Please select the input device from above:\t"))
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
LOGGER.warning(e)
|
||||
continue
|
||||
|
||||
if int(selected_list_id) < int(len(device_list)):
|
||||
print("ford")
|
||||
LOGGER.debug("Selected ID within range")
|
||||
continue
|
||||
elif selected_list_id > int(len(device_list)):
|
||||
print("Out of range, try again...")
|
||||
LOGGER.debug("Out of range, try again...")
|
||||
selected_list_id = None
|
||||
continue
|
||||
else:
|
||||
selected_list_id = None
|
||||
print("Internal error, try again")
|
||||
LOGGER.error("Internal error, try again")
|
||||
continue
|
||||
|
||||
for dev_dict in org_device_list:
|
||||
print(list(device_list)[selected_list_id-1][0])
|
||||
LOGGER.debug(list(device_list)[selected_list_id-1][0])
|
||||
if dev_dict[1] == list(device_list)[selected_list_id-1][0]:
|
||||
selected_device = dev_dict
|
||||
print(selected_device)
|
||||
LOGGER.debug(selected_device)
|
||||
|
||||
return selected_device
|
||||
|
||||
@@ -149,7 +153,7 @@ def get_user_token():
|
||||
if len(token) == 59:
|
||||
return token
|
||||
else:
|
||||
print('Length error in token, please try again...')
|
||||
LOGGER.error('Length error in token, please try again...')
|
||||
token = None
|
||||
continue
|
||||
|
||||
@@ -168,7 +172,7 @@ def get_user_mention_channel_id():
|
||||
if len(str(channel_id)) == len('757379843792044102'):
|
||||
return channel_id
|
||||
else:
|
||||
print("Length error in ID, please try again")
|
||||
LOGGER.error("Length error in ID, please try again")
|
||||
channel_id = None
|
||||
continue
|
||||
|
||||
@@ -204,3 +208,36 @@ async def check_and_reply_to_ping(bot, message):
|
||||
else:
|
||||
await bot.process_commands(message)
|
||||
return False
|
||||
|
||||
|
||||
# Create the logger
|
||||
def init_global_logger(_verbose_level: str = "WARNING"):
|
||||
numeric_log_level = getattr(logging, _verbose_level.upper(), None)
|
||||
if not isinstance(numeric_log_level, int):
|
||||
raise ValueError('Invalid log level: %s' % _verbose_level)
|
||||
else:
|
||||
# create logger
|
||||
init_logger = logging.getLogger('Discord_Radio_Bot')
|
||||
init_logger.setLevel(logging.DEBUG)
|
||||
|
||||
# create file handler which logs even debug messages
|
||||
fh = logging.FileHandler(f'DRB-{date.today()}.log')
|
||||
fh.setLevel(logging.DEBUG)
|
||||
|
||||
# create console handler with a higher log level
|
||||
ch = logging.StreamHandler()
|
||||
ch.setLevel(numeric_log_level)
|
||||
|
||||
# create formatter and add it to the handlers
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
fh.setFormatter(formatter)
|
||||
ch.setFormatter(formatter)
|
||||
|
||||
# add the handlers to the logger
|
||||
init_logger.addHandler(fh)
|
||||
init_logger.addHandler(ch)
|
||||
|
||||
|
||||
#if __name__ is not 'main':
|
||||
# init_global_logger()
|
||||
# LOGGER = logging.getLogger('Discord_Radio_Bot')
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import audioop
|
||||
import logging
|
||||
import math
|
||||
import pyaudio
|
||||
import discord
|
||||
@@ -6,6 +7,7 @@ import numpy
|
||||
|
||||
voice_connection = None
|
||||
|
||||
LOGGER = logging.getLogger("Discord_Radio_Bot.NoiseGateV2")
|
||||
|
||||
class AudioStream:
|
||||
def __init__(self, _channels: int = 2, _sample_rate: int = 48000, _frames_per_buffer: int = 1024,
|
||||
@@ -24,19 +26,19 @@ class AudioStream:
|
||||
if _input:
|
||||
self.paInstance_kwargs['input_device_index'] = _input_device_index
|
||||
else:
|
||||
print(f"[AudioStream.__init__]:\tInput was not enabled."
|
||||
LOGGER.warning(f"[AudioStream.__init__]:\tInput was not enabled."
|
||||
f" Reinitialize with '_input=True'")
|
||||
|
||||
if _output_device_index:
|
||||
if _output:
|
||||
self.paInstance_kwargs['output_device_index'] = _output_device_index
|
||||
else:
|
||||
print(f"[AudioStream.__init__]:\tOutput was not enabled."
|
||||
LOGGER.warning(f"[AudioStream.__init__]:\tOutput was not enabled."
|
||||
f" Reinitialize with '_output=True'")
|
||||
|
||||
if _init_on_startup:
|
||||
# Init PyAudio instance
|
||||
print("Creating PyAudio instance")
|
||||
LOGGER.info("Creating PyAudio instance")
|
||||
self.paInstance = pyaudio.PyAudio()
|
||||
|
||||
# Define and initialize stream object if we have been passed a device ID (pyaudio.open)
|
||||
@@ -44,7 +46,7 @@ class AudioStream:
|
||||
|
||||
if _output_device_index or _input_device_index:
|
||||
if _init_on_startup:
|
||||
print("Init stream")
|
||||
LOGGER.info("Init stream")
|
||||
self.init_stream()
|
||||
|
||||
def init_stream(self, _new_output_device_index: int = None, _new_input_device_index: int = None):
|
||||
@@ -53,14 +55,14 @@ class AudioStream:
|
||||
if self.paInstance_kwargs['input']:
|
||||
self.paInstance_kwargs['input_device_index'] = _new_input_device_index
|
||||
else:
|
||||
print(f"[AudioStream.init_stream]:\tInput was not enabled when initialized."
|
||||
LOGGER.warning(f"[AudioStream.init_stream]:\tInput was not enabled when initialized."
|
||||
f" Reinitialize with '_input=True'")
|
||||
|
||||
if _new_output_device_index:
|
||||
if self.paInstance_kwargs['output']:
|
||||
self.paInstance_kwargs['output_device_index'] = _new_output_device_index
|
||||
else:
|
||||
print(f"[AudioStream.init_stream]:\tOutput was not enabled when initialized."
|
||||
LOGGER.warning(f"[AudioStream.init_stream]:\tOutput was not enabled when initialized."
|
||||
f" Reinitialize with '_output=True'")
|
||||
|
||||
self.close_if_open()
|
||||
@@ -74,7 +76,7 @@ class AudioStream:
|
||||
if self.stream.is_active():
|
||||
self.stream.stop_stream()
|
||||
self.stream.close()
|
||||
print(f"[ReopenStream.close_if_open]:\t Stream was open; It was closed.")
|
||||
LOGGER.debug(f"[ReopenStream.close_if_open]:\t Stream was open; It was closed.")
|
||||
|
||||
def list_devices(self, _display_input_devices: bool = True, _display_output_devices: bool = True):
|
||||
info = self.paInstance.get_host_api_info_by_index(0)
|
||||
@@ -89,13 +91,13 @@ class AudioStream:
|
||||
input_device = self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')
|
||||
devices['Input'][i] = input_device
|
||||
if _display_input_devices:
|
||||
print("Input Device id ", i, " - ", input_device)
|
||||
LOGGER.debug("Input Device id ", i, " - ", input_device)
|
||||
|
||||
if (self.paInstance.get_device_info_by_host_api_device_index(0, i).get('maxOutputChannels')) > 0:
|
||||
output_device = self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')
|
||||
devices['Output'][i] = output_device
|
||||
if _display_output_devices:
|
||||
print("Output Device id ", i, " - ", output_device)
|
||||
LOGGER.debug("Output Device id ", i, " - ", output_device)
|
||||
|
||||
return devices
|
||||
|
||||
@@ -125,6 +127,7 @@ class NoiseGate(AudioStream):
|
||||
if self.stream.is_active:
|
||||
self.stream.stop_stream()
|
||||
|
||||
|
||||
class NoiseGateStream(discord.AudioSource):
|
||||
def __init__(self, _stream):
|
||||
super(NoiseGateStream, self).__init__()
|
||||
@@ -142,7 +145,7 @@ class NoiseGateStream(discord.AudioSource):
|
||||
buffer_decibel = 20 * math.log10(buffer_rms)
|
||||
|
||||
if self.process_set_count % 10 == 0:
|
||||
print(f"{buffer_decibel} db")
|
||||
LOGGER.debug(f"{buffer_decibel} db")
|
||||
|
||||
if buffer_decibel >= self.stream.THRESHOLD:
|
||||
self.NG_fadeout_count = self.NG_fadeout
|
||||
@@ -153,12 +156,13 @@ class NoiseGateStream(discord.AudioSource):
|
||||
else:
|
||||
if self.NG_fadeout_count > 0:
|
||||
self.NG_fadeout_count -= 1
|
||||
print(f"Frames in fadeout remaining: {self.NG_fadeout_count}")
|
||||
LOGGER.debug(f"Frames in fadeout remaining: {self.NG_fadeout_count}")
|
||||
self.process_set_count += 1
|
||||
if curr_buffer:
|
||||
return bytes(curr_buffer)
|
||||
|
||||
except OSError as e:
|
||||
LOGGER.warning(e)
|
||||
pass
|
||||
|
||||
def audio_datalist_set_volume(self, datalist, volume):
|
||||
|
||||
88
bot.py
88
bot.py
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
import discord
|
||||
@@ -17,6 +18,9 @@ class Bot(commands.Bot):
|
||||
commands.Bot.__init__(self, command_prefix=commands.when_mentioned_or(kwargs['command_prefix']),
|
||||
activity=discord.Game(name=f"@ me"), status=discord.Status.idle)
|
||||
|
||||
# Create the logger for the bot
|
||||
self.logger = logging.getLogger("Discord_Radio_Bot.Bot")
|
||||
|
||||
# Init the core bot variables
|
||||
self.DEVICE_ID = kwargs['Device_ID']
|
||||
self.DEVICE_NAME = kwargs['Device_Name']
|
||||
@@ -82,7 +86,7 @@ class Bot(commands.Bot):
|
||||
brief="Joins the voice channel that the caller is in")
|
||||
async def join(ctx, *, member: discord.Member = None):
|
||||
member = member or ctx.author.display_name
|
||||
print(f"Join requested by {member}")
|
||||
self.logger.info(f"Join requested by {member}")
|
||||
|
||||
if not self.Bot_Connected:
|
||||
# Wait for the bot to be ready to connect
|
||||
@@ -93,15 +97,15 @@ class Bot(commands.Bot):
|
||||
|
||||
if discord.opus.is_loaded():
|
||||
channel = ctx.author.voice.channel
|
||||
print("Sending hello")
|
||||
self.logger.debug("Sending hello")
|
||||
await ctx.send(f"Ok {str(member).capitalize()}, I'm joining {channel}")
|
||||
|
||||
# Join the voice channel with the audio stream
|
||||
print('Joining')
|
||||
self.logger.debug('Joining')
|
||||
voice_connection = await channel.connect()
|
||||
|
||||
# Create an audio stream from selected device
|
||||
print("Starting noisegate/stream handler")
|
||||
self.logger.debug("Starting noisegate/stream handler")
|
||||
self.streamHandler = NoiseGatev2.NoiseGate(_input_device_index=self.DEVICE_ID,
|
||||
_voice_connection=voice_connection,
|
||||
_noise_gate_threshold=self.noisegate_sensitivity)
|
||||
@@ -109,61 +113,62 @@ class Bot(commands.Bot):
|
||||
self.streamHandler.run()
|
||||
|
||||
# Start the SDR and begin playing to the audio stream
|
||||
print("Starting SDR")
|
||||
self.logger.debug("Starting SDR")
|
||||
self.start_sdr()
|
||||
|
||||
# Change the activity to the channel and band-type being used
|
||||
print("Changing presence")
|
||||
self.logger.debug("Changing presence")
|
||||
await self.set_activity()
|
||||
|
||||
# 'Lock' the bot from connecting
|
||||
print("Locking the bot")
|
||||
self.logger.debug("Locking the bot")
|
||||
self.Bot_Connected = True
|
||||
|
||||
else:
|
||||
# Return that the opus library would not load
|
||||
await ctx.send("Opus won't load")
|
||||
print("OPUS didn't load properly")
|
||||
self.logger.critical("OPUS didn't load properly")
|
||||
|
||||
else:
|
||||
await ctx.send(f"{str(member).capitalize()}, I'm already connected")
|
||||
print("Bot is already in a channel")
|
||||
self.logger.info("Bot is already in a channel")
|
||||
|
||||
@self.command(help="Use this command to have the bot leave your channel",
|
||||
brief="Leaves the current voice channel")
|
||||
async def leave(ctx, member: discord.Member = None):
|
||||
member = member or ctx.author.display_name
|
||||
print(f"Leave requested by {member}")
|
||||
self.logger.info(f"Leave requested by {member}")
|
||||
|
||||
if self.Bot_Connected:
|
||||
# Stop the sound handlers
|
||||
# Disconnect the client from the voice channel
|
||||
print("Disconnecting")
|
||||
self.logger.debug("Disconnecting")
|
||||
await self.streamHandler.close()
|
||||
|
||||
print("Changing presence")
|
||||
self.logger.debug("Changing presence")
|
||||
# Change the presence to away and '@ me'
|
||||
await self.set_activity(False)
|
||||
|
||||
# Stop the SDR so it can cool off
|
||||
print("Stopping SDR")
|
||||
self.logger.debug("Stopping SDR")
|
||||
self.stop_sdr()
|
||||
|
||||
print("Unlocking the bot")
|
||||
self.logger.debug("Unlocking the bot")
|
||||
# 'Unlock' the bot
|
||||
self.Bot_Connected = False
|
||||
|
||||
print("Sending Goodbye")
|
||||
self.logger.debug("Sending Goodbye")
|
||||
await ctx.send(f"Goodbye {str(member).capitalize()}.")
|
||||
else:
|
||||
await ctx.send(f"{str(member).capitalize()}, I'm not in a channel")
|
||||
self.logger.info("Bot is not in a channel")
|
||||
|
||||
# Add command to change the NoiseGate threshold
|
||||
@self.command(help="Use this command to change the threshold of the noisegate",
|
||||
brief="Change noisegate sensitivity")
|
||||
async def chngth(ctx, _threshold: int, member: discord.Member = None):
|
||||
member = member or ctx.author.display_name
|
||||
print(f"Change of NoiseGate threshold requested by {member}")
|
||||
self.logger.info(f"Change of NoiseGate threshold requested by {member}")
|
||||
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the threshold from "
|
||||
f"{self.streamHandler.THRESHOLD} to {_threshold}")
|
||||
self.streamHandler.THRESHOLD = _threshold
|
||||
@@ -182,6 +187,7 @@ class Bot(commands.Bot):
|
||||
member = member or ctx.author.display_name
|
||||
message = self.display_current_radio_config()
|
||||
await ctx.send(f"Ok {str(member).capitalize()},\n{message}")
|
||||
self.logger.info(f"Displaying current profile; requested by {member}")
|
||||
|
||||
# Command to display the current config
|
||||
@self.command(name='displayprofiles',
|
||||
@@ -193,6 +199,7 @@ class Bot(commands.Bot):
|
||||
member = member or ctx.author.display_name
|
||||
message = self.display_saved_radio_configs()
|
||||
await ctx.send(f"Ok {str(member).capitalize()},\n{message}")
|
||||
self.logger.info(f"Displaying all profiles; requested by {member}")
|
||||
|
||||
# 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"
|
||||
@@ -205,6 +212,7 @@ class Bot(commands.Bot):
|
||||
async def chfreq(ctx, mode: str, freq: str, member: discord.Member = None):
|
||||
# Possible band-types that can be used
|
||||
member = member or ctx.author.display_name
|
||||
self.logger.info(f"{member} requested change of frequency to Mode: {mode}, Freq: {freq}")
|
||||
|
||||
# Check to make sure the frequency input matches the syntax needed
|
||||
if len(freq) >= 6:
|
||||
@@ -240,6 +248,7 @@ class Bot(commands.Bot):
|
||||
brief="Changes radio squelch")
|
||||
async def chsquelch(ctx, squelch: float, member: discord.Member = None):
|
||||
member = member or ctx.author.display_name
|
||||
self.logger.info(f"{member} requested change of squelch to: {squelch}")
|
||||
|
||||
self.squelch = squelch
|
||||
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the squelch to {self.squelch}")
|
||||
@@ -289,13 +298,13 @@ class Bot(commands.Bot):
|
||||
async def on_ready():
|
||||
# Check the ./modules folder for any modules (cog.py)
|
||||
await self.check_for_modules()
|
||||
print("Bot started!")
|
||||
self.logger.info("Bot started!")
|
||||
|
||||
# Check to see if other bots are online
|
||||
async def check_other_bots_online(self):
|
||||
print('Checking if other bots are online')
|
||||
self.logger.info('Checking if other bots are online')
|
||||
channel = self.get_channel(self.Default_Channel_ID)
|
||||
print(f"Testing in: {channel}")
|
||||
self.logger.debug(f"Testing in: {channel}")
|
||||
|
||||
bots_online = []
|
||||
|
||||
@@ -315,7 +324,7 @@ class Bot(commands.Bot):
|
||||
except asyncio.exceptions.TimeoutError:
|
||||
seconds_waited += 1
|
||||
|
||||
print(f"Bots Online: {bots_online}")
|
||||
self.logger.debug(f"Bots Online: {bots_online}")
|
||||
|
||||
if len(bots_online) == 0:
|
||||
return False
|
||||
@@ -325,13 +334,13 @@ class Bot(commands.Bot):
|
||||
# Check the handler being used during init
|
||||
def check_handler(self):
|
||||
if self.Handler == "gqrx":
|
||||
print("Starting GQRX handler")
|
||||
self.logger.info("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")
|
||||
self.logger.info("Starting OP25 handler")
|
||||
from op25Handler import OP25Handler
|
||||
self.OP25Handler = OP25Handler()
|
||||
self.OP25Handler.start()
|
||||
@@ -342,22 +351,22 @@ class Bot(commands.Bot):
|
||||
# Check the system type and load the correct library
|
||||
# Linux ARM AARCH64 running 32bit OS
|
||||
if self.system_os_type == 'Linux_ARMv7l':
|
||||
print(f"Loaded OPUS library for {self.system_os_type}")
|
||||
self.logger.debug(f"Loaded OPUS library for {self.system_os_type}")
|
||||
discord.opus.load_opus('./opus/libopus_armv7l.so')
|
||||
# Linux ARM AARCH64 running 64bit OS
|
||||
if self.system_os_type == 'Linux_AARCH64':
|
||||
print(f"Loaded OPUS library for {self.system_os_type}")
|
||||
self.logger.debug(f"Loaded OPUS library for {self.system_os_type}")
|
||||
discord.opus.load_opus('./opus/libopus_aarcch64.so')
|
||||
# Windows 64bit
|
||||
if self.system_os_type == 'Windows_x64':
|
||||
print(f"Loaded OPUS library for {self.system_os_type}")
|
||||
self.logger.debug(f"Loaded OPUS library for {self.system_os_type}")
|
||||
discord.opus.load_opus('./opus/libopus_amd64.dll')
|
||||
|
||||
# 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:
|
||||
print(f"Device list {self.Devices_List}")
|
||||
self.logger.debug(f"Device list {self.Devices_List}")
|
||||
for device, index in self.Devices_List['Input']:
|
||||
if int(index) == self.DEVICE_ID and str(device) == self.DEVICE_NAME:
|
||||
return True
|
||||
@@ -382,19 +391,19 @@ class Bot(commands.Bot):
|
||||
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.logger.debug(f"Loaded extension: {folder_name}")
|
||||
self.load_extension(f"modules.{folder_name}.cog")
|
||||
|
||||
# Reload a selected module for changes
|
||||
def reload_modules(self, module):
|
||||
try:
|
||||
self.unload_extension(f"modules.{module}.cog")
|
||||
print(f"Unloaded {module}")
|
||||
self.logger.debug(f"Unloaded {module}")
|
||||
self.load_extension(f"modules.{module}.cog")
|
||||
print(f"Loaded {module}")
|
||||
self.logger.debug(f"Loaded {module}")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(e)
|
||||
self.logger.error(e)
|
||||
return False
|
||||
|
||||
# Check and store the OS type of the system for later use
|
||||
@@ -403,14 +412,14 @@ class Bot(commands.Bot):
|
||||
if os.name == 'nt':
|
||||
if processor == "AMD64":
|
||||
self.system_os_type = 'Windows_x64'
|
||||
print(f"OS/Arch is {self.system_os_type}")
|
||||
self.logger.debug(f"OS/Arch is {self.system_os_type}")
|
||||
else:
|
||||
if processor == "aarch64":
|
||||
self.system_os_type = 'Linux_AARCH64'
|
||||
print(f"OS/Arch is {self.system_os_type}")
|
||||
self.logger.debug(f"OS/Arch is {self.system_os_type}")
|
||||
elif processor == "armv7l":
|
||||
self.system_os_type = 'Linux_ARMv7l'
|
||||
print(f"OS/Arch is {self.system_os_type}")
|
||||
self.logger.debug(f"OS/Arch is {self.system_os_type}")
|
||||
|
||||
# Check to see if there is only one frequency
|
||||
def start_sdr(self):
|
||||
@@ -421,7 +430,7 @@ class Bot(commands.Bot):
|
||||
self.stop_sdr()
|
||||
|
||||
# Start the radio
|
||||
print(f"Starting freq: {self.freq}")
|
||||
self.logger.debug(f"Starting freq: {self.freq}")
|
||||
|
||||
if self.Handler == 'gqrx':
|
||||
# Set the settings in GQRX
|
||||
@@ -466,7 +475,7 @@ class Bot(commands.Bot):
|
||||
|
||||
# Save the current radio settings as a profile
|
||||
async def save_radio_config(self, _profile_name: str):
|
||||
print(f"Saving profile {_profile_name}")
|
||||
self.logger.debug(f"Saving profile {_profile_name}")
|
||||
config = configparser.SafeConfigParser()
|
||||
|
||||
if os.path.exists('./profiles.ini'):
|
||||
@@ -493,7 +502,7 @@ class Bot(commands.Bot):
|
||||
|
||||
# Load a saved profile into the current settings
|
||||
async def load_radio_config(self, profile_name):
|
||||
print(f"Loading profile {profile_name}")
|
||||
self.logger.debug(f"Loading profile {profile_name}")
|
||||
config = configparser.ConfigParser()
|
||||
if os.path.exists('./profiles.ini'):
|
||||
config.read('./profiles.ini')
|
||||
@@ -505,8 +514,9 @@ class Bot(commands.Bot):
|
||||
try:
|
||||
self.noisegate_sensitivity = int(config[self.profile_name]['Noisegate_Sensitivity'])
|
||||
except KeyError:
|
||||
print(f"Config does not contain a 'noisegate sensitivity' value, "
|
||||
f"creating one now with the default value: {BotResources.DEFAULT_NOISEGATE_THRESHOLD}")
|
||||
self.logger.warning(f"Config does not contain a 'noisegate sensitivity' value, "
|
||||
f"creating one now with the default value: "
|
||||
f"{BotResources.DEFAULT_NOISEGATE_THRESHOLD}")
|
||||
self.noisegate_sensitivity = BotResources.DEFAULT_NOISEGATE_THRESHOLD
|
||||
await self.save_radio_config(self.profile_name)
|
||||
|
||||
@@ -544,7 +554,7 @@ class Bot(commands.Bot):
|
||||
try:
|
||||
message_body += f"\tNoisegate Sensitivity:\t{config[section]['Noisegate_Sensitivity']}\n"
|
||||
except KeyError:
|
||||
print(f"Config does not contain a 'noisegate sensitivity' value. Please load the profile")
|
||||
self.logger.warning(f"Config does not contain a 'noisegate sensitivity' value. Please load the profile")
|
||||
|
||||
message_body += f"\tSquelch:\t\t\t\t{config[section]['Squelch']}\n"
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import logging
|
||||
from telnetlib import Telnet
|
||||
from BotResources import check_negative
|
||||
from time import sleep
|
||||
|
||||
class GQRXHandler():
|
||||
def __init__(self, hostname: str = "localhost", port: int = 7356):
|
||||
self.logger = logging.getLogger("Discord_Radio_Bot.GQRXHandler")
|
||||
self.hostname = hostname
|
||||
self.port = port
|
||||
|
||||
@@ -12,24 +14,24 @@ class GQRXHandler():
|
||||
self.create_telnet_connection()
|
||||
|
||||
def create_telnet_connection(self):
|
||||
print("Creating connection")
|
||||
self.logger.info("Creating connection")
|
||||
self.tel_conn = Telnet(self.hostname, self.port)
|
||||
self.tel_conn.open(self.hostname, self.port)
|
||||
|
||||
def change_freq(self, freq):
|
||||
print(f"Changing freq to {freq}")
|
||||
self.logger.debug(f"Changing freq to {freq}")
|
||||
self.tel_conn.write(bytes(f"F {int(freq)}", 'utf-8'))
|
||||
self.tel_conn.read_until(b'RPRT 0')
|
||||
|
||||
def change_squelch(self, squelch):
|
||||
if not check_negative(squelch):
|
||||
squelch = float(-abs(squelch))
|
||||
print(f"Changing squelch to {squelch}")
|
||||
self.logger.debug(f"Changing squelch to {squelch}")
|
||||
self.tel_conn.write(bytes(f"L SQL {float(squelch)}", 'utf-8'))
|
||||
self.tel_conn.read_until(b'RPRT 0')
|
||||
|
||||
def change_mode(self, mode):
|
||||
print(f"Changing mode to {mode}")
|
||||
self.logger.debug(f"Changing mode to {mode}")
|
||||
self.tel_conn.write(bytes(f"M {str(mode)}", 'utf-8'))
|
||||
self.tel_conn.read_until(b'RPRT 0')
|
||||
|
||||
|
||||
34
main.py
34
main.py
@@ -1,8 +1,9 @@
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
import bot
|
||||
import argparse
|
||||
from BotResources import check_if_config_exists, write_config_file, read_config_file
|
||||
import BotResources
|
||||
|
||||
# Jorn
|
||||
#token = 'OTE1MDY0OTk2OTk0NjMzNzI5.YaWKsA.Y9yaCGg_VXRL_qQVbs05vo7gSAc'
|
||||
@@ -15,26 +16,28 @@ from BotResources import check_if_config_exists, write_config_file, read_config_
|
||||
|
||||
#name = "VoiceMeeter Output"
|
||||
|
||||
LOGGER = logging.getLogger("Discord_Radio_Bot.Main")
|
||||
|
||||
|
||||
class BotDeviceNotFound(Exception):
|
||||
def __init__(self, device):
|
||||
print(f"Unable to find the device: {device}")
|
||||
LOGGER.debug(f"Unable to find the device: {device}")
|
||||
try:
|
||||
os.remove('./config.ini')
|
||||
except OSError:
|
||||
print("Config file not found, restarting.")
|
||||
LOGGER.warning("Config file not found, restarting.")
|
||||
#os.execv(__file__, sys.argv)
|
||||
|
||||
|
||||
def main(**passed_config):
|
||||
# Don't create a config if the user passed parameters
|
||||
if len(passed_config.keys()) == 0:
|
||||
print('Checking config file...')
|
||||
if not check_if_config_exists():
|
||||
print("No config file exists, please enter this information now")
|
||||
write_config_file(init=True)
|
||||
LOGGER.info('Checking config file...')
|
||||
if not BotResources.check_if_config_exists():
|
||||
LOGGER.warning("No config file exists, please enter this information now")
|
||||
BotResources.write_config_file(init=True)
|
||||
|
||||
config = read_config_file()
|
||||
config = BotResources.read_config_file()
|
||||
|
||||
# Overwrite config options if they were passed
|
||||
if len(passed_config.keys()) == 0:
|
||||
@@ -43,13 +46,13 @@ def main(**passed_config):
|
||||
if sub in passed_config and passed_config[sub]:
|
||||
config[sub] = passed_config[sub]
|
||||
|
||||
print('Starting Bot...')
|
||||
LOGGER.info('Starting Bot...')
|
||||
|
||||
discord_bot_client = bot.Bot(Token=config['Bot Token'], Device_ID=config['Device ID'],
|
||||
Device_Name=config['Device Name'], Mention_Group=config['Mention Group'],
|
||||
Channel_ID=config['Channel ID'], Handler=config['Handler'])
|
||||
|
||||
print(f"Verifying audio device:\t{config['Device Name']}")
|
||||
LOGGER.debug(f"Verifying audio device:\t{config['Device Name']}")
|
||||
|
||||
if not discord_bot_client.check_device(_override=bool("Device ID" in passed_config.keys())):
|
||||
raise BotDeviceNotFound(config['Device Name'])
|
||||
@@ -112,20 +115,21 @@ def cmd_arguments():
|
||||
|
||||
if __name__ == '__main__':
|
||||
cmd_args = cmd_arguments()
|
||||
BotResources.init_global_logger()
|
||||
|
||||
if not all(cmd_args.values()):
|
||||
print("Passed arguments:")
|
||||
LOGGER.debug("Passed arguments:")
|
||||
for arg in cmd_args:
|
||||
if cmd_args[arg]:
|
||||
print(f"\t{arg}:\t{cmd_args[arg]}")
|
||||
LOGGER.debug(f"\t{arg}:\t{cmd_args[arg]}")
|
||||
|
||||
try:
|
||||
print('Starting...')
|
||||
LOGGER.info('Starting...')
|
||||
while True:
|
||||
try:
|
||||
main(**cmd_args)
|
||||
except BotDeviceNotFound:
|
||||
print("Restarting...")
|
||||
LOGGER.info("Restarting...")
|
||||
time.sleep(2)
|
||||
except KeyboardInterrupt:
|
||||
print("Exiting...")
|
||||
LOGGER.error("Exiting...")
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import re
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
@@ -26,6 +27,8 @@ random_message = ["{%mtn_user%}, tsk tsk. Links belong here:\n{%ref_og_msg%}", "
|
||||
# welcome 757379843792044102
|
||||
# the-round-table 367396189529833474
|
||||
|
||||
LOGGER = logging.getLogger("Discord_Radio_Bot.LinkCop")
|
||||
|
||||
class LinkCop(commands.Cog):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
@@ -59,7 +62,7 @@ class LinkCop(commands.Cog):
|
||||
try:
|
||||
if not any(item.id in self.whitelisted_groups for item in ctx.author.roles):
|
||||
if check_message(ctx.content):
|
||||
print('Msg with links detected in a blocked channel')
|
||||
LOGGER.info('Msg with links detected in a blocked channel')
|
||||
|
||||
response_text = self.generate_response(ctx)
|
||||
|
||||
@@ -68,8 +71,8 @@ class LinkCop(commands.Cog):
|
||||
# Delete the original message
|
||||
await ctx.delete()
|
||||
except AttributeError as err:
|
||||
print(f"Link Cop Error: '{err}'")
|
||||
print(f"Bot or other non-user that has not been whitelisted sent a message")
|
||||
LOGGER.error(f"Link Cop Error: '{err}'\nBot or other non-user that has not "
|
||||
f"been whitelisted sent a message")
|
||||
|
||||
await check_and_reply_to_ping(self.bot, ctx)
|
||||
|
||||
@@ -123,7 +126,7 @@ def check_message(message_text):
|
||||
def get_links(message_text):
|
||||
links = regex_link.findall(message_text)
|
||||
|
||||
print(links)
|
||||
LOGGER.error(links)
|
||||
|
||||
if len(links) > 0:
|
||||
return links
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import shutil
|
||||
import threading
|
||||
import subprocess
|
||||
@@ -21,6 +22,8 @@ class OP25Handler(threading.Thread):
|
||||
|
||||
self.Stop_OP25 = False
|
||||
|
||||
self.logger = logging.getLogger("Discord_Radio_Bot.OP25Handler")
|
||||
|
||||
def run(self) -> None:
|
||||
while True:
|
||||
if self.Start_OP25:
|
||||
@@ -56,17 +59,17 @@ class OP25Handler(threading.Thread):
|
||||
p25_kwargs = [f"./rx.py", "--args", "rtl", "-N", "LNA:49", "-s", "200000", "-o", "25600", "-U", "-f",
|
||||
f"{self.Frequency}e6", "-X", "-2"]
|
||||
|
||||
print(f"Starting OP25")
|
||||
self.logger.info(f"Starting OP25")
|
||||
# Change the interpreter's working directory (idr why)
|
||||
if self.HTTP_ENABLED:
|
||||
p25_kwargs.extend(["-l", "http:0.0.0.0:8080"])
|
||||
|
||||
print(f"OP25 Keyword Args: {p25_kwargs}")
|
||||
self.logger.debug(f"OP25 Keyword Args: {p25_kwargs}")
|
||||
|
||||
self.OP25Proc = subprocess.Popen(p25_kwargs, executable=self.OP25EXE, shell=False, cwd=self.OP25Dir)
|
||||
|
||||
def close_op25(self):
|
||||
print(f"Closing OP25")
|
||||
self.logger.info(f"Closing OP25")
|
||||
try:
|
||||
self.OP25Proc.kill()
|
||||
|
||||
@@ -74,11 +77,11 @@ class OP25Handler(threading.Thread):
|
||||
while self.OP25Proc.poll() is None:
|
||||
# Terminate the process every 5 seconds
|
||||
if seconds_waited % 5 == 0:
|
||||
print("Terminating OP25")
|
||||
self.logger.debug("Terminating OP25")
|
||||
self.OP25Proc.terminate()
|
||||
time.sleep(1)
|
||||
print(f"Waited {seconds_waited} seconds")
|
||||
self.logger.debug(f"Waited {seconds_waited} seconds")
|
||||
seconds_waited += 1
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
self.logger.error(e)
|
||||
|
||||
Reference in New Issue
Block a user