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.
Related
I try to use guild.fetch_members(limit=None) in my class:
In an earlier version it worked but won't anymore...
Like in the introduction in the doc described I created a class for my bot now I am facing the problem that Intents.members aren't enabled.
Intents = discord.Intents.default()
Intents.members = True
Intents.presences = False
class OverlordBot(discord.Client):
async def on_ready(self):
print(platform.system())
print(Intents.members)
# loop for every guild
for guild in self.guilds:
self.__amountGuilds += 1
# loop for every channel in every guild
for channels in guild.channels:
self.__amountChannels += 1
# loop for every user in every server
members = await guild.fetch_members(limit=None).flatten()
for member in members:
self.__memberList.append(member.id)
And here is the error-stack:
Linux
True
Ignoring exception in on_ready
Traceback (most recent call last):
File "/home/anon/.local/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/home/anon/Documents/git/overlordbot/Overlord_vrs_4/main/Main.py", line 133, in on_ready
members = await guild.fetch_members(limit=None).flatten()
File "/home/anon/.local/lib/python3.9/site-packages/discord/guild.py", line 1313, in fetch_members
raise ClientException('Intents.members must be enabled to use this.')
discord.errors.ClientException: Intents.members must be enabled to use this.
Funnily this error only occurs on Linux-Systems. It works perfectly fine on my Windows-machine and since I want to run this bot on my RaspberryPi I must resolve this problem.
Thanks for any help...
I suggest to remove the class and use the code as docs say. If you don't want, try to copy-paste the intents line into the class() too.
Hello everyone who tried to help me here.
I finally found what the problem was.
at the end of file when instantiating the client I needed to give the intents as a parameter.
client = OverlordBot(intents=intents)
client.run()
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)
#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')
Earlier, I asked a question which was fixing some broken code. But the code specified changed the role of the author.
async def ruleBreak(ctx, arg):
member = await ctx.message.author
role = discord.utils.get(member.guild.roles, name="RuleBreakers")
await discord.Member.add_roles(member, role)
However, I want to set a specified user to get the role, not the author. I've thought about removing the send part on line 2.
Please help.
Thanks in advance!
Your second line is saying that the member who should get the role is the member who invokes the command. For this you can use add_roles() which takes the role you want to give as an arguments. You can specify a recipient and role like so !addrole #someperson #thisrole
#client.command()
async def addrole(ctx, user: discord.Member, role: discord.Role):
await user.add_roles(role)
await ctx.send(f"I gave {user.name} the role {role.name}")
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.