Reputation: 1
I have made a bot that joins a voice channel whenever a discord member joins said channel, but sometimes when the member leaves and joins back I get the error that the bot is already connected, so I added the is_connected
function to check if the bot is already connected before attempting to join. For some reason this function works only half the time, if I join and leave the call multiple times eventually it just stops joining and it leaves no error code, it just stops.
This is the code:
@client.event
async def on_ready():
print("Logged in as {0.user}".format(client))
@client.event
async def on_voice_state_update(member, before, after):
if not before.channel and after.channel and member.id == 450333776485285919 or member.id == 232855365082021890 or member.id == 282234566066962433 or member.id == 256759154524291074:
channel = client.get_channel(988050675604803648)
await is_connected()
@client.event
async def is_connected():
channel = client.get_channel(988050675604803648)
voice_client = discord.utils.get(client.voice_clients)
if(voice_client == None):
await channel.connect()
else:
print("is connected:")
Did i use the function wrong ?
Upvotes: 0
Views: 116
Reputation: 736
You created an entire function for is_connected()
which isn't entirely necessary. You need to use that event within the on_voice_state_update
to check if the bot is already connected to that current voice channel.
@client.event
async def on_voice_state_update(member, before, after):
if not before.channel and after.channel and member.id == 450333776485285919 or member.id == 232855365082021890 or member.id == 282234566066962433 or member.id == 256759154524291074:
channel = client.get_channel(988050675604803648)
if client.voice.is_connected():
return print("already connected")
await channel.connect()
Upvotes: 0