Join voice channel (discord.py) - python

When I try to let my bot join my voice channel, I get this error:
await client.join_voice_channel(voice_channel) (line that generates the error)
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/discord/ext/commands/core.py", line 50, in wrapped
ret = yield from coro(*args, **kwargs)
File "bot.py", line 215, in sfx
vc = await client.join_voice_channel(voice_channel)
File "/usr/local/lib/python3.5/site-packages/discord/client.py", line 3176, in join_voice_channel
session_id_future = self.ws.wait_for('VOICE_STATE_UPDATE', session_id_found)
AttributeError: 'NoneType' object has no attribute 'wait_for'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/discord/ext/commands/bot.py", line 848, in process_commands
yield from command.invoke(ctx)
File "/usr/local/lib/python3.5/site-packages/discord/ext/commands/core.py", line 369, in invoke
yield from injected(*ctx.args, **ctx.kwargs)
File "/usr/local/lib/python3.5/site-packages/discord/ext/commands/core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'wait_for'
I'm getting this error with channel name and channel ID
Function:
description = "Bot"
bot_prefix = "!"
client = discord.Client()
bot = commands.Bot(description=description, command_prefix=bot_prefix)
#bot.command(pass_context=True)
async def join(ctx):
author = ctx.message.author
voice_channel = author.voice_channel
vc = await client.join_voice_channel(voice_channel)

This is the code i use to make it work.
#Bot.py
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.voice_client import VoiceClient
import asyncio
bot = commands.Bot(command_prefix="|")
async def on_ready():
print ("Ready")
#bot.command(pass_context=True)
async def join(ctx):
author = ctx.message.author
channel = author.voice_channel
await bot.join_voice_channel(channel)
bot.run("token")

Get rid of the
from discord.voice_client import VoiceClient
line and it shoudl be ok.

Try this
await bot.join_voice_channel(channel)

Related

Discord.py add_roles problems

I am trying to make a bot that gives a role to members when they join. However, it keeps coming up with the error message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 34, in lol
await member.add_roles(probation)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 764, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 248, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
I have given it the "manage roles permission (see below), but it still comes up with this error.
Is there a fix for this?
Also my python code:
import discord
from discord.ext import commands
from dotenv import load_dotenv
import os
import json
from datetime import datetime, timedelta
import asyncio
load_dotenv()
token = os.getenv('Gov')
intents=discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents = intents)
#bot.event
async def on_member_join(member):
join_guild = await bot.fetch_guild(793659768606425140)
probation = join_guild.get_role(838212050332549142)
await member.add_roles(probation)
I didn't see any problem on your code. You said bot has add roles permission but can you try to check the role that your bot gives to member is on top of your bot's role.
You could try:
#bot.event async def on_member_join(member):
channel =discord.utils.get(member.guild.text_channels,name="channelnamehere")
role = discord.utils.get(member.guild.roles, name='rolenamehere')
await member.add_roles(role)
await channel.send(f"{member.mention} welcome, you have been assigned {role}!")
This should work without having to pull channel IDs and whatnot!
If this doesn't work, my best advice would be checking that your bot is above all the roles you are trying to assign!

TypeError: 'NoneType' object is not callable with discord API and youtube_dl

i am making a discord music bot with a lot of other features and i am getting this error all the time
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
connect = await channel.connect()
await connect(channel)
#client.command(pass_context=True)
async def play(ctx, url):
voice_client = ctx.guild.voice_client
server = ctx.message.guild
voice_client = ctx.guild.voice_client(server)
player = await voice_client.create_ytdl_player(url)
players[server.id] = player
player.start()
this is the log line/terminal:
Ignoring exception in command play:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 55, in play
voice_client = ctx.guild.voice_client(server)
TypeError: 'NoneType' object is not callable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'NoneType' object is not callable
Can somebody help me with this thx.
You are trying to call guild.voice_client which is a property, not a method from what I can see in the docs.
You are getting a 'NoneType' object is not callable error because your guild object does not have a VoiceClient associated with it.
If you look at the docs for VoiceClient it looks like you retrieve one via VoiceChannel.connect()

Discord.py - 'VoiceState' object does not have 'voice_channel' attribute

Making a music bot discord. Code that throws an error:
import config
import discord
import youtube_dl
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
player = {}
#client.event
async def on_ready():
print('Logged on as BOT!')
#client.command(pass_context=True)
async def join(ctx):
voice_channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(voice_channel)
print("Joined channel" + str(voice_channel))
I tried to change the value voice_channel = ctx.message.author.voice.voice_channel на
voice_channel = ctx.message.author.voice.channel
When using the command .join, in the console issues:
Ignoring exception in command join:
Traceback (most recent call last):
File "C:\Users\diram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
ret = await coro(*args, **kwargs)
File "music.py", line 15, in join
voice_channel = ctx.message.author.voice.voice_channel
AttributeError: 'VoiceState' object has no attribute 'voice_channel'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\diram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\diram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 728, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\diram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceState' object has no attribute 'voice_channel'
Please try voice_channel = ctx.message.author.voice.channel instead. It is .channel, not .voice_channel, as per the documentation: https://discordpy.readthedocs.io/en/latest/api.html.

How to fix "discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing." in discord.py

I'm trying do a simple bot with subclass to handler the commands but i got this error
Here Are the TraceBack:
Ignoring exception in command teste:
Traceback (most recent call last):
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 721, in invoke
await self.prepare(ctx)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 685, in prepare
await self._parse_arguments(ctx)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 599, in _parse_arguments
transformed = await self.transform(ctx, param)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 445, in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.
And here are the code:
import discord
from discord.ext import commands
import inspect
class BotLibertarin(commands.Bot):
client = discord.Client()
#client.event
async def on_message(self,message):
print(f"message from {message.author} what he said {message.content}")
await self.process_commands(message)
class CommandsHandler(BotLibertarin):
def __init__(self):
super().__init__(command_prefix=".")
members = inspect.getmembers(self)
for name, member in members:
if isinstance(member,commands.Command):
if member.parent is None:
self.add_command(member)
#commands.command()
async def teste(self,ctx):
await ctx.channel.send("teste")
I really suggest that you spend time reading the docs.
I'd also remind you that it's against ToS to send messages without a specific command being issued (such as !commandname) [Even though no one follows ToS apparently].
Either way. Try this:
import discord
from discord.ext import commands
client = commands.Bot(command.prefix='!')
#client.event
async def on_message(message):
print(f"message from {message.author} what he said {message.content}")
await message.channel.send(message.content)
#client.command()
async def teste(ctx):
await ctx.channel.send("teste")

Play audio in vc discord using discord.py - InvalidArgument: Channel passed must be a voice channel

I tried to play audio using discord.py, while I join in voice chat, but I face a problem:
InvalidArgument: Channel passed must be a voice channel
How can I make it work?
Here is my code:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from discord import Game
Client = discord.client
client = Bot('.')
Clientdiscord = discord.Client()
#client.command(pass_context=True)
async def yt(ctx):
url = ctx.message.content
url = url.strip('yt')
author = ctx.message.author
voice_channel = author.voice_channel
vc = await client.join_voice_channel(voice_channel)
player = await vc.create_ytdl_player(url)
player.start()
client.run('my Token')
After that I tired to play the video, after I joined the voice channel:
(.yt https://www.youtube.com/watc........)
Error list:
Ignoring exception in command yt
Traceback (most recent call last):
File "C:\Users\Hema\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\discord\ext\commands\core.py", line 50, in wrapped
ret = yield from coro(*args, **kwargs)
File "C:\Users\Hema\Desktop\Music - Copy - Copy.py", line 21, in yt
vc = await client.join_voice_channel(voice_channel)
File "C:\Users\Hema\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\discord\client.py", line 3172, in join_voice_channel
raise InvalidArgument('Channel passed must be a voice channel')
discord.errors.InvalidArgument: Channel passed must be a voice channel
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Hema\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\discord\ext\commands\bot.py", line 846, in process_commands
yield from command.invoke(ctx)
File "C:\Users\Hema\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\discord\ext\commands\core.py", line 374, in invoke
yield from injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Hema\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\discord\ext\commands\core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an
exception: InvalidArgument: Channel passed must be a voice channel
the voice_channel = author.voice_channelis the error replace it with
voice_channel = author.voice.channel good luck :)

Categories

Resources