command object has no attribute missingpermissions - python

I am writing an error handler with my discord bot and it was working well until I installed and imported youtube_dl and pynacl (which I am using to make it play music).
Code:
#client.event
async def on_command_error(ctx,error):
if isinstance(error,commands.MissingPermissions):
await ctx.send("You do not have rights to this command.")
await ctx.message.delete()
elif isinstance(error,commands.MissingRequiredArgument):
await ctx.send("You need an argument.")
await ctx.message.delete()
elif isinstance(error,commands.CommandNotFound):
await ctx.send("Invalid Command.")
await ctx.message.delete()
else:
raise error
error:
Ignoring exception in on_command_error
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "C:\Users\hensd\bots\bot.py", line 127, in on_command_error
if isinstance(error,commands.MissingPermissions):
AttributeError: 'Command' object has no attribute 'MissingPermissions'
when I test the other possible error I get the same message with the corresponding errors instead of MissingPermissions\
EDIT: The reason this happened is because I created a command called "commands"
which basically showed the commands and what they do. python thought that I was trying to import "commands" from there. All I had to do was rename the command so python was not confused between the two. thanks for the help everyone!

Related

Hey Every Time I wanna run my bot it tells me this error

Hey Every Time I wanna run my bot it tells me this error but it runs anyway. I wanna know how can I fix it?
ERROR:
Unhandled exception in internal background task 'ch_pr'.Traceback (most recent call last): File "C:\Users\Pekah\AppData\Local\Programs\Python\Python39-32\lib\site-packages\discord\ext\tasks\__init__.py", line 178, in _loop await self.coro(*args, **kwargs) File "c:\Users\Pekah\OneDrive\Documents\Projects\rare-bot\bot.py", line 32, in ch_pr await client.change_presence(activity=discord.Game(name=f'Rare Bot version: {version} 🔧')) File "C:\Users\Pekah\AppData\Local\Programs\Python\Python39-32\lib\site-packages\discord\client.py", line 1112, in change_presence await self.ws.change_presence(activity=activity, status=status_str)AttributeError: 'NoneType' object has no attribute 'change_presence'
CODE:
#tasks.loop(seconds=5)
async def ch_pr():
while True:
await client.change_presence(activity=discord.Game(name=f'Rare Bot version: {version} 🔧'))
await asyncio.sleep(5)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening ,name='-help 🌌'))
await asyncio.sleep(5)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening ,name=f'{len(client.guilds)} servers 👾'))
await asyncio.sleep(5)
await client.change_presence(activity=discord.Game(name='discord.py 📁'))
await asyncio.sleep(5)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening ,name=f'{len(client.users)} users 👥'))
await asyncio.sleep(5)
ch_pr.start()
You can use try/except to handle exceptions and carry on as normal. i.e.
while True:
try:
a_function_that_might_error()
except AttributeError:
pass #do nothing
However, this will catch all AttributeErrors. You'd be better off adjusting your code to ensure that the error doesn't happen, or by getting it to throw a more specific custom error that you can catch.

What is the Solution to MissingRequiredArgument Error in discord.py Module?

I want to create a bot that will display server information such as server name, server owner's name, total number of members, etc. whenever called upon. I read up on the module's document many times and tried many things. Regardless of all attempts, I have been getting this error whenever I invoke the bot by command. I am using Python 3.9.2 and discord.py module of version 1.6.0, by the way. How can I solve the issue? Thanks in advance!
My Code:
#bot.command()
async def server(ctx, guild: discord.Guild):
await ctx.send(guild.name)
await ctx.send(guild.owner)
await ctx.send(guild.owner_id)
await ctx.send(guild.members)
await ctx.send(guild.member_count)
Error:
Ignoring exception in command server:
Traceback (most recent call last):
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 856, in invoke
await self.prepare(ctx)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 790, in prepare
await self._parse_arguments(ctx)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 706, in _parse_arguments
kwargs[name] = await self.transform(ctx, param)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 542, in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: guild is a required argument that is missing.
Do you need the guild parameter, why don't you just do:
#bot.command()
async def server(ctx):
await ctx.send(ctx.guild.name)
await ctx.send(ctx.guild.owner)
await ctx.send(ctx.guild.owner_id)
await ctx.send(ctx.guild.members)
await ctx.send(ctx.guild.member_count)
You have requested 2 arguments , ctx and guild : discord.Guild when you run the command , you have to use the command in the format !server <guild id/name> , since you didn't mention the <guild id/name> the error was triggered , either you can remove the argument as mentioned in answer given by #PythonProgrammer or you can mention the guild you want to request info of

helpme command, cannot find author's voice channel discord.py

There are bots that you can do !h and the bot types in chat (authors name, need help, #staff, and in which voice channel is the person who need the help)
How could I find the command author's voice channel?
thats what i tried:
(I am trying it on the on_message event for now)
#client.event
async def on_message(message):
print(f'member is in {message.author.voice_channel}')
But when I'm running it and typing something in the chat I receive this error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Ido Shaked\Desktop\citybot\venv\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "C:/Users/Ido Shaked/Desktop/citybot/citycode.py", line 18, in on_message
print(f'member is in {message.author.voice_channel}')
AttributeError: 'Member' object has no attribute 'voice_channel'
The error is telling you exactly what's wrong - a Member does not have a voice_channel attribute. You have to get it via message.author.voice.channel

How to get User object from id discord.py

I have a bot that sends dm pings to a user whos Id is given as a parameter in a command.
This is the command:.spam ID #_OF_PINGS
I run into the following error:
Ignoring exception in command spam:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "bot.py", line 16, in spam
await ctx.send(f'Started pinging {user.name} {num} times.')
AttributeError: 'NoneType' object has no attribute 'name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/app/.heroku/python/lib/python3.6/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: AttributeError: 'NoneType' object has no attribute 'name'
Here is my code:
from discord.ext import commands
token = 'MyBotTokenHere'
prefix = '.'
client = commands.Bot(command_prefix=prefix)
client.remove_command("help")
#client.event
async def on_ready():
print('Ready')
#client.command()
async def spam(ctx, id1: int, num: int):
user = client.get_user(id1)
await ctx.send(f'Started pinging {user.name} {num} times.')
for i in range(num):
await user.send(f'<#{str(id1)}>')
await ctx.send(f'Finished {num} pings for {user.name}')
client.run(token)
It was working fine yesterday, but today, it broke down for some reason.
How do I fix it?
P.S. I hosted it on Heroku
You can use a converter for this:
#client.command()
async def spam(ctx, user: discord.User, num: int):
await ctx.send(f'Started pinging {user.name} {num} times.')
for i in range(num):
await user.send(f'<#{str(id1)}>')
await ctx.send(f'Finished {num} pings for {user.name}')
This way, Discord will automatically try to get the discord.User instance that corresponds to the id that you pass as an argument, and you can also #mention someone if you want to & it'll still work.
Also, starting from Discord 1.5.0, you now need to pass in intents when initializing a bot, which you clearly aren't doing yet. Update your Discord, and use the following to initialize your bot:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=prefix, intents=intents)
More info on the how's and why's of intents in the relevant API documentation. You'll also want to enable members on your bot's Privileged Intents page, which is explained in the linked API docs as well.

(discord.py) İ want to make a roll call bot for a discord server

I want to make a discord bot. Here is what i want: When i say '!yoklama 11.10.2020.txt'(or something) bot sends me a list of participants of which voice channel i am in.[not 1 channel(which i am in)] But i dont know how can i do.
Then i was some research and i find similar things about my request.
But they did not work properly.
Here is my found codes:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.command()
async def attendance(ctx, filename):
channel = client.get_channel(755129236397752441) # Replace with voice channel ID
with open(filename, 'w') as file:
for member in channel.members:
file.write(member.name + '\n')
client.run('mytoken')
This codes are working(when i run they dont give me error) but when i say !attendence test.txt bot
does not say anything and my python shell say to me:
Ignoring exception in command attendance:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(args, **kwargs)
File "C:\bot2\bot2.py", line 10, in attendance
for member in channel.members:
AttributeError: 'NoneType' object has no attribute 'members'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 859, in invoke
await injected(ctx.args, **ctx.kwargs)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\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: AttributeError:
'NoneType' object has no attribute 'members
You see guys i dont understand anything please help me.
I found these question but it does not work for me:
Discord Python bot creating a file of active member in a vocal channel
#client.command()
async def test(ctx):
guild = ctx.guild
channel = discord.utils.get(guild.voice_channels, name='General')
for member in channel.members:
print('test')
What I would do instead of having to pass in the file in the command, is just in the function writing:
with open("file.txt", "w") as f:

Categories

Resources