Added 'lock' when bot joins to avoid double join or leave when not joined error

This commit is contained in:
Logan Cusano
2022-02-12 20:02:24 -05:00
parent e1ca0bfd99
commit dda6b64e14

67
bot.py
View File

@@ -24,9 +24,12 @@ class Bot(commands.Bot):
self.Default_Mention_Group = kwargs['Mention_Group']
self.Handler = kwargs['Handler']
# Init Variable forsound
# Init Variable for sound
self.streamHandler = None
# Init 'lock' variable for when the bot is joined
self.Bot_Connected = False
# Init the audio devices list
self.Devices_List = sound.query_devices().items()
@@ -70,47 +73,55 @@ class Bot(commands.Bot):
async def join(ctx, *, member: discord.Member = None):
member = member or ctx.author.display_name
# Wait for the bot to be ready to connect
await self.wait_until_ready()
if not self.Bot_Connected:
# Wait for the bot to be ready to connect
await self.wait_until_ready()
# Load respective opus library
self.load_opus()
# Load respective opus library
self.load_opus()
if discord.opus.is_loaded():
channel = ctx.author.voice.channel
await ctx.send(f"Ok {str(member).capitalize()}, I'm joining {channel}")
if discord.opus.is_loaded():
channel = ctx.author.voice.channel
await ctx.send(f"Ok {str(member).capitalize()}, I'm joining {channel}")
# Join the voice channel with the audio stream
voice_connection = await channel.connect()
# Join the voice channel with the audio stream
voice_connection = await channel.connect()
# Create an audio stream from selected device
self.streamHandler = sound.PCMStream(voice_connection)
# Ensure the selected device is available and start the audio stream
self.streamHandler.change_device(self.DEVICE_ID)
# Start the SDR and begin playing to the audio stream
self.start_sdr()
# Create an audio stream from selected device
self.streamHandler = sound.PCMStream(voice_connection)
# Ensure the selected device is available and start the audio stream
self.streamHandler.change_device(self.DEVICE_ID)
# Change the activity to the channel and band-type being used
await self.set_activity()
# Start the SDR and begin playing to the audio stream
self.start_sdr()
# 'Lock' the bot from connecting
self.Bot_Connected = True
# Change the activity to the channel and band-type being used
await self.set_activity()
else:
# Return that the opus library would not load
await ctx.send("Opus won't load")
else:
# Return that the opus library would not load
await ctx.send("Opus won't load")
await ctx.send(f"{str(member).capitalize()}, I'm already connected")
@self.command(help="Use this command to have the bot leave your channel",
brief="Leaves the current voice channel")
async def leave(ctx):
# Stop the sound handlers
self.streamHandler.cleanup()
# Disconnect the client from the voice channel
await ctx.voice_client.disconnect()
# Change the presence to away and '@ me'
await self.set_activity(False)
# Stop the SDR so it can cool off
self.stop_sdr()
if self.Bot_Connected:
# Stop the sound handlers
self.streamHandler.clean_up()
# Disconnect the client from the voice channel
await ctx.voice_client.disconnect()
# Change the presence to away and '@ me'
await self.set_activity(False)
# Stop the SDR so it can cool off
self.stop_sdr()
# 'Unlock' the bot
self.Bot_Connected = False
if self.Handler == 'gqrx':
@self.command(name='chfreq', help="Use this command to change the frequency the bot is listening to. "