Error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceState' object has no attribute 'voice_channel'
Code:
from discord.ext import commands
import random
import youtube_dl
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(channel)```
Try using voice.channel instead of voice.voice_channel.
from discord.ext import commands
import random
import youtube_dl
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
await client.join_voice_channel(channel)
Also client.join_voice_channel will not work. Use something like this:
from discord.ext import commands
from discord.utils import get
import random
import youtube_dl
#client.command(pass_context=True)
async def join(ctx):
chn = ctx.message.author.voice.channel
if not chn:
await ctx.send("Error: Connect to voice channel")
return
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(chn)
Related
I have this code in a module for my discord bot to join a vc:
import discord
import ffmpeg
from discord.ext import commands
client = commands.Bot(command_prefix = "!")
async def join(cxt):
vc = ctx.message.author.voice_channel
await client.join_voice_channel(channel)
But the main code doesn't run it and whenever I try my !join command it doesn't work. The bot still goes online, it just won't go on a voice channel. The module is commands. Please help!!
Main code:
import discord
import os
import requests
import json
import random
import youtube_dl
import commands
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
message.content = message.content.lower()
if message.author == client.user:
return
client.run(os.getenv('TOKEN'))
voice_channel is not an attribute of Member, which is the type of ctx.message.author. Instead, you want to look for a VoiceState in Member.voice:
async def join(ctx):
vc = ctx.message.author.voice.channel
if vc:
await vc.connect()
else:
# user isn't in voice
Similarly, you can't connect to a voice channel with Bot.join_voice_channel. You need to call connect() on the VoiceChannel or StageChannel object (if it exists, which is why you should check to see if Member.voice.channel is None.
I'm trying to create a music bot for discord and I finish the code and tried to run it and when I run the play command it simply says this.
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "play" is not found
Here's my code.
import discord
from discord.ext import commands
import youtube_dl
TOKEN = "Token here"
bot = discord.ext.commands.Bot(command_prefix = "s ");
#bot.event
async def on_ready():
channel = discord.utils.get(bot.get_all_channels(), id=794444804607574026)
await channel.connect()
async def play(ctx, url):
player = await voice_client.create_ytdl_player(url)
player.start()
bot.run(TOKEN)
You haven't added #bot.command(name="play") above the play function
import discord
from discord.ext import commands
import youtube_dl
TOKEN = "Token here"
bot = discord.ext.commands.Bot(command_prefix = "s ");
#bot.event
async def on_ready():
channel = discord.utils.get(bot.get_all_channels(), id=794444804607574026)
await channel.connect()
#bot.command(name="play")
async def play(ctx, url):
player = await voice_client.create_ytdl_player(url)
player.start()
bot.run(TOKEN)
before
async def play(ctx, url):
player = await voice_client.create_ytdl_player(url)
player.start()
after
#bot.command()
async def play(ctx, url):
player = await voice_client.create_ytdl_player(url)
player.start()
Simply use this code:
main.py
from discord.ext import commands
from discord.ext.commands import bot
import discord
import os
import youtube_dl
bot = commands.Bot(command_prefix="s")
#bot.event
async def on_ready()
print("{0.user} is online".format(bot))
#bot.command()
async def play(ctx, url):
player = await voice_client.create_ytdl_player(url)
player.start()
.env
TOKEN=<paste your token here>
Hope it’s helped you (:
import discord
from discord.ext import commands
import random
import asyncio
client = commands.Bot(command_prefix= '+')
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
await client.change_presence(status=discord.Status.offline)
#client.command()
async def kick(ctx, member : discord.Member):
await member.disconnect()
client.run(<TOKEN>)
I want to make a commands that disconnect target from voice chat what should i use or do?
You can use Member.move_to and pass None to it to remove someone from a voice channel.
I am currently trying to create my discord bot. sadly, this does not work and I have no Idea why...
import discord
import os
import time
from ka import keep_alive
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix = '.')
prefix = '.'
#client.event
async def on_ready():
print("I'm ready! {0.user}".format(client))
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Croissants!"))
#client.command()
async def join(ctx):
channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(channel)
await ctx.send("On my way!")
client.run(os.getenv('TOKEN'))
there are NO errors. But no output aswell. I try making it join my vc by writing: .join
channel returns None, because ctx.message.author don't have voice attribute. Also, Client.join_voice_channel is deprecated since v1.0 (read here)
Instead of that, try this:
import discord
import os
import time
from ka import keep_alive
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix = '.')
prefix = '.'
#client.event
async def on_ready():
print("I'm ready! {0.user}".format(client))
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Croissants!"))
#client.command()
async def join(ctx):
channel = ctx.guild.get_member(ctx.author.id).voice.channel # This
await channel.connect()
await ctx.send("On my way!")
client.run(os.getenv('TOKEN'))
Do client.add_command(join). You currently have the function for your command, but you haven't told the bot to recognize it as a command.
im trying to show the discord bot typing but getting the error for command for class client unresolved. I need the command attribute so I can show the bot is typing when I ask it a question.
from asyncio import sleep
import asyncio
import discord
from discord.ext.commands import bot
import random
client = discord.Client()
#client.event
async def on_message(message):
message.content.lower()
if message.author == client.user:
return
elif message.content.startswith('hello'):
await sleep(4)
await message.channel.send('hi how are you ')
Commands
#client.command()
async def typing(ctx):
# Start typing
async with ctx.typing():
await asyncio.sleep(2)
await ctx.send('Done Typing')