How do I make a bot join a voice channel in rewrite?
Anything I try doesn't work, and it doesn't give an error either.
Below are my attempts:
#client.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
#client.command()
async def join(ctx):
channel = client.get_channel(ctx.author.voice.channel.id)
await channel.connect()
Edit: I turned off the error handler and now I get this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'channel'
Edit 2: I am in a voice channel that the bot can connect to. ctx.author.voice_channel doesn't work, I get this this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Member' object has no attribute 'voice_channel'
Edit 3: The command works. I installed PyNaCl and it works just fine.
Try these code.
#client.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
#client.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
Related
I am trying to delete the command message (the one sent by the user not by bot)
Ive tried it like this.
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("-"):
wait(5)
await message.delete()
and this
#bot.command()
async def creator(ctx, message):
await ctx.send(f"Example text")
wait(5)
await message.delete()
but none of them work. If you know why please post a solution. Thanks!
In the first code :
import time
#bot.event
async def on_message(message):
if message.author.bot : return
if message.content.startswith("-"):
time.sleep(5)
await message.delete()
Second Code
import time
#bot.command()
async def creator(ctx):
await ctx.send("Example text")
time.sleep(5)
await ctx.message.delete()
If you are using the both of on_message and commands in your bot,
You may want to add this in the end of on_message Event So your bot will handle the on_message event, Then it goes to process commands.
Without it the bot commands wont work .
await bot.process_commands(message)
Eg :
#bot.event
async def on_message(message) :
await message.channel.send("Example")
await bot.process_commands(message)
I get an error message because of the command process_commands and the commands doesn't work, while the evnts do work properly.
import discord
from discord import commands
import os
bot = commands.Bot(command_prefix="!")
client = discord.Client()
#client.event
async def on_ready():
print('Logged in as as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('Hello'):
await message.channel.send('Hello!')
#here comes the error message
await bot.process_commands(message)
#this command doesn't work
#bot.command()
async def testymesty(ctx):
await ctx.send('test')
keep_alive()
client.run(os.environ['TOKEN'])
Simple error, you defined client as commands.bot then its going to be :
await client.process_commands(message)
decide between client and bot.
Don't use client and bot at the same time
I have tried making an error message when a user does not have the permissions to use the command, this is because I kept getting an error in the console the code is supposed to send a message in chat when the user doesn't have permissions to use it, the error has disappeared from the console but it does not output a message
#client.command()
#has_permissions(manage_messages=True)
async def clear(ctx, limit: int):
await ctx.channel.purge(limit = limit)
await ctx.send('Cleared by {}'.format(ctx.author.mention))
#clear.error
async def clear_error(error, ctx):
if isinstance(error, MissingPermissions):
await ctx.send('Sorry, you do not have permissions to do that!')
When you're using an error handling function, you have to do async def clear_error(ctx, error):. Basically you just have to swap the parameters' places.
#clear.error
async def clear_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send('Sorry, you do not have permissions to do that!')
I'm having issues with my code saying
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: edit() missing 1 required positional argument: 'self'
when I run the command. Self shouldn't need to be defined, right?
Also, when I do add self I get an issue with ctx.
The code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="/")
#client.command(pass_context=True)
async def join(ctx, member=discord.Member):
channel = ctx.author.voice.channel
await channel.connect()
await member.edit(mute=True)
#client.command(pass_context=True)
async def leave(ctx):
await ctx.voice_client.disconnect()
client.run("Token")
I Managed to find the problem and fixed it.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="/")
#client.command()
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
await ctx.author.edit(mute=True)
#client.command()
async def leave(ctx):
await ctx.voice_client.disconnect()
client.run("Token")
the problem is, you included member on your funcion. If you want it to send, edit, or do something with the message author just do ctx.author and it will be set on the author of the message.
I'm creating a new bot (my third time now, but it's been a while) and I am creating a simple ban command. It is line-for-line the same as my other commands, with the addition of the #commands.has_permissions() decorator and an embed to display the ban. However, after adding some prints to show where it gets to, it doesn't make it past await user.ban().
# ---BAN---
#client.command(name="ban", pass_ctx=True)
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.User=None, *, reason: str=None):
if not user:
await ctx.send("Specify a user")
return
embed = discord.Embed (
color = discord.Color.magenta()
)
embed.add_field(name=f"{user} was banned!", value=f"For reason: {reason}", inline=False)
await user.ban()
await ctx.send(embed=embed)
#ban.error
async def ban_error(ctx, error):
if isinstance(error, BadArgument):
await ctx.send("Please specify a **valid** user!")
No error is thrown, but it only gets to await user.ban() before just stopping. Is my user wrong somehow, or did I make an obvious mistake?
'User' object has no attribute 'ban' , instead you need to pass a member object:
async def ban(ctx, user: discord.Member=None, *, reason: str=None):
And you're not getting any errors because #ban.error is catching them but is only handling the BadArgument exception while the rest are ignored.