Rapptz / discord.py

An API wrapper for Discord written in Python.
http://discordpy.rtfd.org/en/latest
MIT License
14.7k stars 3.74k forks source link

Help moving bot to another voice channel #587

Closed Sneakyninja0129 closed 7 years ago

Sneakyninja0129 commented 7 years ago

@client.command(pass_context=True) async def joinvoice(ctx): """Joins your voice channel"""

author = ctx.message.author
server = ctx.message.server
voice_channel = author.voice_channel
if voice_channel is not None: #is in a channel
    discord.VoiceClient.move_to(voice_channel) <------------- This line is the only not working part
await client.join_voice_channel(voice_channel)

Heres the code i have i need it to either disconnect from the voice channel or for it to move the bot to voice_channel

Gorialis commented 7 years ago

You're accessing the attribute move_to of the VoiceClient class, not an instance of it. Judging by your code, this line is unnecessary anyway, as you don't have a voice client to move yet.

if voice_channel is not None:
    voice_client = await client.join_voice_channel(voice_channel)
Sneakyninja0129 commented 7 years ago

Thats not what i need. What the problem is, is i want it to be able to move channels and if it is in a channel ie: if voice_channel is not None: I want it to disconnect and join the new channel

Gorialis commented 7 years ago

You can use server.voice_client to obtain the voice client for a given server, if there is one. Don't try and use methods from discord.VoiceClient directly because that is the class, not the instance.

Sneakyninja0129 commented 7 years ago

Thats still not what i need. My bot can join any channel. But once it is in a channel i cant get it to disconnect from the voice chat

Gorialis commented 7 years ago

Voice clients have a move_to coroutine you can use to shift them to another channel. If you need to disconnect (you don't for this), you can use disconnect instead.

Sneakyninja0129 commented 7 years ago

Could you write an example? Im confused about legit everything ive been coding for like 10 hours and im super tired

PapyrusThePlant commented 7 years ago

Those issues are not to help you with your code, but to report bugs within the library. You can join the discord api server to chat about code and stuff if you wish but please stop using github as a help desk as it spams over a thousand people's email with your inability to search the documentation.

AXAz0r commented 7 years ago

To disconnect from the current voice channel on the server:

await message.guild.voice_client.disconnect()

To move to a new channel:

await message.guild.voice_client.move_to(voice_channel)
Gorialis commented 7 years ago

@AXAz0r The property is only message.guild in rewrite, which in general you should assume users are not using unless they specify otherwise.

AXAz0r commented 7 years ago

@Gorialis True enough, but the method is the same in async where you'd only replace .guild with .server

Sneakyninja0129 commented 7 years ago

Where are you getting "message".server.voice_client.disconnect()?

lepeli commented 7 years ago

http://python.swaroopch.com/ (for complete beginners to programming) https://learnxinyminutes.com/docs/python3/ (for people who know programming already) https://docs.python.org/3.5/tutorial/ (for the in-between group, i.e. knows some programming but not a lot) see also: http://www.codeabbey.com/ (exercises for beginners)

DOCS : http://discordpy.rtfd.io/en/latest/api.html

Phxntxm commented 7 years ago

Can't believe no one has answered this question. Everything given so far for the rewrite, not async version (except links to the documentation). He is using message.server. He is not using the rewrite. Y'all are petty.

http://discordpy.readthedocs.io/en/latest/api.html#discord.VoiceClient.move_to What you're looking for is this. You need an instance of the VoiceClient. Notice the text "This client is created solely through Client.join_voice_channel() and its only purpose is to transmit voice."

This means that you need to save a reference to the voice VoiceClient somewhere. You can use this example to base off of: https://github.com/Rapptz/discord.py/blob/async/examples/playlist.py

The idea is the state of the voice connection is saved, when trying to join a channel, check if that voice connection exists ( https://github.com/Rapptz/discord.py/blob/async/examples/playlist.py#L115 ) if it does, use move_to if it doesn't, use join_voice_channel.

The same goes for disconnecting, you have that VoiceState saved and if you have a connected VoiceClient, disconnect.