Reputation:
I looked all the questions about connecting discord bot to voice channel for playing sound purposes but I couldn't find the answer I need. I'm new to the discord library and didn't solve how things work so when I try given answers in site I mostly get errors like this:
AttributeError: 'NoneType' object has no attribute 'channel'
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'channel'
My code looks like this:
import discord
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
token = "XXXXXXXXXXXXXXXXX"
client = commands.Bot(command_prefix='.')
voice = discord.VoiceChannel
@client.command(name="join")
async def join(ctx):
channel = ctx.author.voice.channel
voice = get(self.bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
client.run(Token)
This is just the voice part of the code other send or event parts are working.
Upvotes: 0
Views: 12775
Reputation: 1
@client.command()
async def enter(ctx):
connected = ctx.author.voice
if not connected:
await ctx.send("You need to be connected in a voice channel to use this command!")
return
global vc
vc = await connected.channel.connect()
BTW, don't forget to install discord,py[voice] module. I hope it has helped :D
Upvotes: 0
Reputation: 84
here is what i use, i commented it as i can
@ client.command(name='join',aliases = ['summon']) # CREATING COMMAND "JOIN" WITH ALIAS SUMMON
async def _join(ctx, *, channel: discord.VoiceChannel = None): # TAKING ARGUMENT CHANNEL SO PPL CAN MAKE THE BOT JOIN A VOICE CHANNEL THAT THEY ARE NOT IN
"""Joins a voice channel."""
destination = channel if channel else ctx.author.voice.channel # CHOOSING THE DESTINATION, MIGHT BE THE REQUESTED ONE, BUT IF NOT THEN WE PICK AUTHORS VOICE CHANNEL
if ctx.voice_client: # CHECKING IF THE BOT IS PLAYING SOMETHING
await ctx.voice_state.voice.move_to(destination) # IF THE BOT IS PLAYING WE JUST MOVE THE BOT TO THE DESTINATION
return
await destination.connect() # CONNECTING TO DESTINATION
await ctx.send(f"Succesfully joined the voice channel: {destination.name} ({destination.id}).")
BTW music and voice bots are really complicated, if you're a starter, better do easier stuff, like moderation commands and games.
Upvotes: 1