From 8391df420995b3511b7e376ef9037b3ac57c20e1 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Tue, 28 Dec 2021 02:21:59 -0500 Subject: [PATCH] Update: Added SDR stop command and stopped SDR when the bot leaves --- bot.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/bot.py b/bot.py index 1d7d973..f3f172c 100644 --- a/bot.py +++ b/bot.py @@ -83,6 +83,7 @@ class Bot(commands.Bot): async def leave(ctx): await ctx.voice_client.disconnect() await self.change_presence(activity=discord.Game(name=f"@me"), status=discord.Status.idle) + self.stop_sdr() @self.command(name='chfreq', help="Use this command to change the frequency the bot is listening to. Note: 'M'" "is required\nExmple command: '@ chfreq <['fm', 'wbfm']> ", @@ -163,10 +164,15 @@ class Bot(commands.Bot): await ctx.send(f"{member}, something went wrong. Please check the console") @self.command(name='startsdr', hidden=True) - async def _startsdr(ctx, input_freq: str, member: discord.Member = None): + async def _startsdr(ctx, member: discord.Member = None): member = member or ctx.author.display_name self.start_sdr() + @self.command(name='stopsdr', hidden=True) + async def _stopsdr(ctx, member: discord.Member = None): + member = member or ctx.author.display_name + self.stop_sdr() + def check_device(self): for device, index in self.Devices_List: if int(index) == self.DEVICE_ID and str(device) == self.DEVICE_NAME: @@ -208,17 +214,7 @@ class Bot(commands.Bot): def start_sdr(self): if type(self.freq) == str: # Single freq sent - # Wait for the running processes to close - if self.sdr_started: - while self.sdr_process.poll() is None and self.sdr_output_process.poll() is None: - self.sdr_process.terminate() - self.sdr_process.kill() - time.sleep(1) - self.sdr_output_process.terminate() - self.sdr_output_process.kill() - time.sleep(1) - - self.sdr_started = False + self.stop_sdr() # Start the radio print(f"Starting freq: {self.freq}") @@ -228,3 +224,16 @@ class Bot(commands.Bot): self.sdr_output_process = Popen(["play", "-t", "raw", "-r", str(self.play_sample_rate), "-es", "-b", "16", "-c", "1", "-V1", "-"], stdin=self.sdr_process.stdout) self.sdr_started = True + + def stop_sdr(self): + # Wait for the running processes to close + if self.sdr_started: + while self.sdr_process.poll() is None and self.sdr_output_process.poll() is None: + self.sdr_process.terminate() + self.sdr_process.kill() + time.sleep(1) + self.sdr_output_process.terminate() + self.sdr_output_process.kill() + time.sleep(1) + + self.sdr_started = False