Discord.py onmessage role allocation issue - discord.py

I've been trying to work this out but can't work out what i'm doing wrong, the method worked fine to delete and display the message but when I tried to get it to allocate a role at the same time I broke it
#bot.event
async def on_message(msg):
for badword in file:
if badword in msg.content.lower():
role = discord.utils.get(msg.guild.roles, name="Muted")
await msg.author.add_roles(role)
await msg.delete()
await msg.channel.send(f'{msg.author.mention}! That was a bad word! You have been muted!')
await bot.process_commands(msg)
I presume i'm missing something simple?
This is the error i'm getting:
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 283, in on_message
await msg.author.add_roles(role)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 676, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'
Also the above is making my other bot.commands repeat indefinitely. So now when I use the random quote command it never stops til I shut off the bot:
#wisdom function
#bot.command(help="Wisdom function",
brief="Get a random wise quote")
async def wisdom(ctx):
responses = open('quotes.txt').read().splitlines()
random.seed(a=None)
response = random.choice(responses)
await ctx.send(f'Hey {ctx.message.author.mention} here is your wisdom:')
await ctx.send(response)

await req(guild_id, user_id, role.id, reason=reason)
^^^^^^^
AttributeError: 'NoneType' object has no attribute 'id'
The problem here is discord.utils.get cannot find a role called Muted, so it returns a NoneType. When you are trying to assign the role, you are essentially doing msg.author.add_roles(None). Maybe try making sure that the role exists, or referencing it by ID instead of Name.
Edit: You should also (in your quote function) send the quote in the same message as the first. (By using \n as a line-break)
Edit 2: You should use a with block to readlines from your file, because with what you have now, the file never closes. Also, read() returns a string, you cannot do readlines() off of the string. Instead do readlines() off of the file object.
Edit 3: You are processing commands for every badword in file (why it runs forever)

Refer to #Frederick Reynolds post about your errors in reading the textfile. I can point out a few things as well, however. For your on_message function, you never check if the message is from your own bot itself, which triggers the repetition of the function. To avoid this, add if msg.author == bot.user: return at the beginning of this function. Secondly, role = discord.utils.get(msg.guild.roles, name="Muted") is not able to find the Muted role for some reason, which is why it sets the value of role to None. This is why, when you call await msg.author.add_roles(role), you're basically doing await msg.author.add_roles(None). Lastly, your indentation is wrong, as you are awaiting bot.process_comands(msg) for every badword in file. Here's the fixed code for the function
#bot.event
async def on_message(msg):
if msg.author == bot.user:
return
for badword in file:
if badword in msg.content.lower():
role = discord.utils.get(msg.guild.roles, name="Muted")
await msg.author.add_roles(role)
await msg.delete()
await msg.channel.send(f'{msg.author.mention}! That was a bad word! You have been muted!')
await bot.process_commands(msg)

Related

Command raised an exception: AttributeError: 'str' object has no attribute 'send'

I'm trying to do a custom embed command and i got this error.
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'send'
And this is my code:
#client.command()
#commands.has_permissions(administrator=True)
async def embed(self, ctx,*, msg=None):
if msg == None:
await ctx.send("No Message has been provided. Please write your message within 30secs.")
def check(message):
return message.author == ctx.author and message.channel == ctx.channel
try:
mes=await self.client.wait_for('message',check=check,timeout=30.0)
except asyncio.TimeoutError:
await ctx.send('Timeout! Please be quicker next time.')
else:
msg=mes.content
em=discord.Embed(
description=msg,
timestamp=datetime.utcnow(),
color=discord.Color.random()).set_author(
name=f'{ctx.author.name}#{ctx.author.discriminator}',
icon_url=f'{ctx.author.avatar_url}')
await ctx.send(embed=em)
If u can patch it please say me below :(
You're doing
#client.command()
#commands.has_permissions(administrator=True)
async def embed(self, ctx,*, msg=None):
If you're in a cog, you need to be doing #commands.command. When doing client.command decorator though, you're adding the callback itself (and it isn't called from the class). The actual context is passed to self, and ctx becomes your first argument (a string) while the other argument msg just stays as None.
If you are in a cog, then change it to a normal command.
#commands.command()
#commands.has_permissions(administrator=True)
async def embed(self, ctx,*, msg=None):
#testy.command(name='d')
#commands.has_permissions(administrator=True)
async def embed(ctx, msg=None):
if msg == None:
await ctx.send("No Message has been provided. Please write your message within 30secs.")
def check(message):
return message.author == ctx.author and message.channel == ctx.channel
try:
mes=await testy.wait_for('message',check=check,timeout=30.0)
except asyncio.TimeoutError:
await ctx.send('Timeout! Please be quicker next time.')
else:
msg=mes.content
em=discord.Embed(description=msg, timestamp=datetime.datetime.utcnow(), color=discord.Color.random())
em.set_author(name=ctx.author, icon_url=ctx.author.avatar)
await ctx.send(embed=em)
Using your code as a base, I tweaked a couple of things and verified it worked on my tester bot. You can see the name of my bot is "testy" so you'll want to replace that with your client.
The command is invoked with the command 'd' and then prompts the user for a text string. That string is then displayed in an embed with a timestamp and the user's discord name and avatar.
Hope this leads you in the right direction.
I am not sure what you are passing in for ctx, but it looks like you're passing in a string for ctx based off of the error, in which case there is no send() function for a string object

how do i display a message if user puts a non existing sub command?

So I made a sub command that just sends a message back. I found that if the user types a non existing command it still displays the message from the sub command. It sounds confusing but here's an example.
User: ;id
Bot: This command help you find the name of anyone in the server!
User: ;id Slayer
Bot: Bob Miller
So while testing I found if the user sends some thing like ;id jgfjkag the bot still sends the original message for ;id which is "This command help you find the name of anyone in the server!". How would I make the bot send a specific message if the user trys to use a non existing sub command? Here's the code:
#commands.group()
async def id(self, ctx):
if ctx.invoked_subcommand is None:
await ctx.send("This command help you find the name of anyone in the server! ")
#id.command()
async def Slayer(self, ctx):
await ctx.send("Bob Miller")
#id.command()
async def Skel(self, ctx):
await ctx.send("John Dove")
Check ctx.subcommand_passed first:
#commands.group()
async def id(self, ctx):
if ctx.subcommand_passed is None:
await ctx.send("This command help you find the name of anyone in the server!")
elif ctx.invoked_subcommand is None:
await ctx.send(f"Subcommand '{ctx.subcommand_passed}' does not exist.")
This is my favorite method of doing it:
#commands.group(invoke_without_command=True)
async def group(self, ctx):
await ctx.send_help(ctx.command) # invokes the help command for the group
#group.command()
async def subcommand(self, ctx):
await ctx.send("You passed a valid subcommand!")
The invoke_without_command parameter, when set to true, will only call the command if there are no (valid) subcommands passed. This means that if the code inside if the group() function is called, you can safely assume that they didn't pass a subcommand (or passed an invalid one).
I also like to use ctx.send_help, since that will automatically list subcommands. You could replace this with something like:
await ctx.send("You didn't pass a valid subcommand!")
[EDIT]
More careful reading of your question revealed that the subcommand already has some functionality. This makes it more difficult, but this will work:
#commands.group(invoke_without_command=True, ignore_extra=False)
async def group(self, ctx):
await ctx.send("This is a group command!")
#group.error
async def on_group_error(self, ctx, error):
if isinstance(error, commands.TooManyArguments):
await ctx.send("You passed an invalid subcommand!")
#group.command()
async def subcommand(self, ctx):
await ctx.send("You passed a VALID subcommand!")
Here is what it would look like:
!group
Bot: This is a group command!
!group bla
Bot: You passed an invalid subcommand!
!group subcommand
Bot: You passed a VALID subcommand!
NOTE: The ignore_extra field raises the commands.TooManyArguments error, which then invokes the error handler allowing the bot to send the error message. Unfortunately, the default on_command_error hook will still be called. I would suggest ignoring the commands.TooManyArguments error inside of your main error handler to fix this.

how to make bot assign roles when given command

i am trying to make the bot give the 'mall' role and remove the 'study hall' role. However, it keeps returning the error
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\frost\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "D:\GalaxyConflux Master\gcclient.py", line 89, in on_message
role.id = 798193741403127818
AttributeError: 'str' object has no attribute 'id'
the code that is running is this
#client.event
async def on_message(message):
if message.content.startswith(gccmd.cmd_prfx + 'mall'):
role = 'Mall'
role.id = 798193741403127818
await message.author.add_roles(role.id)
i have tried many different approaches but each result in a similar error.
I had it just go off of the role = 'Mall' part but that did not work either.
How can I convert the role id into an actual id?
Thanks so much!
~Glimmer
here's an example peice that shows you how to add a role and remove one.
#client.command()
async def mall(ctx):
# if prefix is '-' you'd type: -mall
await user.remove_roles(#ROLE_ID)
await user.add_roles(#ROLE_ID)
await ctx.send("Mall role added!")
#client.command()
async def home(ctx):
# if prefix is '-' you'd type: -home
await user.remove_roles(#ROLE_ID)
await user.add_roles(#ROLE_ID)
await ctx.send("Home role added!")
If you want to remove multiple roles, you'd have to just add all of the roles there you want to remove, then add the one's you want.
For example, if you are heading to the mall from your house, it removes every role including the house role and adds the mall one.

I am getting a name error in my coding any one know why?

#bot.command()
#commands.has_guild_permissions(mute_members=True)
async def mute(ctx, member: discord.Member, *, Reason=None):
await Member.add_roles(get_role)
await get_role('751096892867477594')
await ctx.send(f'User {member} Has been muted')
So I am coding a mute command in discord.py and that is the code for it, but when I run the bot and try and mute my test account I get this error:
Ignoring exception in command mute:
Traceback (most recent call last):
File "C:\Users\Robin\AppData\Roaming\Python\Python37\sitepackages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\Robin\Desktop\discord bot in python\bot.py", line 85, in mute
await Member.add_roles(get_role)
NameError: name ‘Member’ is not defined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Robin\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command. invoke (ctx)
File "C:\Users\Robin\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(“ctx.args, “*ctx.kwargs)
File "C:\Users\Robin\AppData\Roaming\Python\Python37\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: NameError: name ‘Member’ is not defined
Member in line 4 is supposed to be lowercase like you passed it in your parameters. Also to get a role use get and role ids are supposed to be int which you passed as a str. The following code will work for you, just change id to your muted role's id.
#bot.command()
#commands.has_guild_permissions(mute_members=True)
async def mute(ctx, member: discord.Member, *, reason=None):
muted_role = discord.utils.get(ctx.guild.roles, id=750678725544247329)
await member.add_roles(muted_role, reason=reason)
await ctx.send(f'User {member} Has been muted')
You have multiple errors:
In Member.add_roles(...), Member has to be in lower case.
You need to get the role before adding it to your user.
The way you use get_role() is wrong → The role id must be an integer, not a string / Writing get_role() wont work since it's a Guild class method.
Your code should look like this:
#bot.command()
#commands.has_guild_permissions(mute_members=True)
async def mute(ctx, member: discord.Member, *, Reason=None):
role = ctx.guild.get_role(751096892867477594)
await member.add_roles(role)
await ctx.send(f'User {member.mention} Has been muted')

Betting, Required arguement missing?

Im starting a new bot, which I hope can eventually start to allow users to bet. Im having trouble finalising this command as it allows the bot to run but when use the ?Coinflip command it says:
Ignoring exception in command coinflip:
Traceback (most recent call last):
File
"C:\Users\sambe\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py",
line 859, in invoke
await ctx.command.invoke(ctx) File "C:\Users\sambe\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py",
line 718, in invoke
await self.prepare(ctx) File "C:\Users\sambe\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py",
line 682, in prepare
await self._parse_arguments(ctx) File "C:\Users\sambe\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py",
line 596, in _parse_arguments
transformed = await self.transform(ctx, param) File "C:\Users\sambe\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py",
line 442, in transform
raise MissingRequiredArgument(param) discord.ext.commands.errors.MissingRequiredArgument: guess is a
required argument that is missing.
This is the command I am using:
#bot.command(pass_context=True)
async def coinflip(ctx, guess: str, amount: float):
guesses = ('heads', 'tails')
guess = guess.lower()
if guess not in guesses:
await bot.say("Invalid guess.")
return
author = ctx.message.author
balance = get_dollars(author)
if balance < amount:
await bot.say("You don't have that much money. Your balance is ${balance:.2f}")
return
result = random.sample(guesses)
if result == guess:
await bot.say("You won!")
add_dollars(author, amount)
else:
await bot.say("You lost!")
remove_dollars(author, amount)
Id like it to give a coinflip command which gives off random numbers.
Although it just ignores the command.
This error means that you aren't passing enough arguments while using this command. You are propably sending ?coinflip instead of ?coinflip heads 12.

Resources