Update: Added SDR stop command and stopped SDR when the bot leaves

This commit is contained in:
Logan Cusano
2021-12-28 02:21:59 -05:00
parent f64d562c5e
commit 8391df4209

33
bot.py
View File

@@ -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']> <xxx.xxxM>",
@@ -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