Hey i am developing a Discord Bot with Python.
Here is the documentation: https://discordpy.readthedocs.io/en/latest/index.html
This program should give a user a role if he or she reacts to a message
I tried to define the ctx in the function itself
#client.event
async def on_raw_reaction_add(payload):
member = discord.utils.get(client.get_all_members())
role = discord.utils.find(lambda r: r.name == "check-in", ctx.guild.roles)
check_in = ["612637944544624690"]
if str(payload.channel_id) in check_in:
await client.add_roles(member, role, reason="check-in")
That is the error without ctx in the function itself:
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\kaitr\Anaconda3\envs\tutorial\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "C:/Users/kaitr/PycharmProjects/Discord Tutorial/bot.py", line 39, in on_raw_reaction_add
role = discord.utils.find(lambda r: r.name == "check-in", ctx.guild.roles)
NameError: name 'ctx' is not defined
That is the error with ctx in the function itself:
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\kaitr\Anaconda3\envs\tutorial\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
TypeError: on_raw_reaction_add() missing 1 required positional argument: 'ctx'
You don't have access to an invocation context because this isn't a command. You can only use the attributes of the payload, a RawReactionActionEvent object. This includes a guild_id attribute that you can use to get the guild:
#client.event
async def on_raw_reaction_add(payload):
if payload.guild_id is None:
return # Reaction is on a private message
guild = client.get_guild(payload.guild_id)
role = discord.utils.get(guild.roles, name="check-in")
member = guild.get_member(payload.user_id)
if str(payload.channel_id) in check_in:
await member.add_roles(role, reason="check-in")
Related
I try
guild = client.get_guild()#input guild-id
async for member in client.fetch_members(limit=150):
print(member.name)
error code:
Ignoring exception in on_ready Traceback (most recent call last):
File "D:\studio\share\crawling\lib\site-packages\discord\client.py",
line 343, in _run_event
await coro(*args, **kwargs) File "D:\studio\onga\main.py", line 20, in on_ready
async for member in client.fetch_members(limit=150): AttributeError: 'Client' object has no attribute 'fetch_members'
There is already a simple way to get the member count the guild.member_count
#bot.command(aliases=["mc"])
async def membercount(ctx):
guild = ctx.guild
await ctx.reply(f"There is {guild.member_count} Members In The {guild.name}")```
Alright I am trying to create a command where the user does (prefix)requestrole discordid roleid. It then sends a request embed to a channel where certain roles can view that channel and they can click on either the checkmark reaction or the 'x' reaction to accept or deny the role request.
#commands.command()
async def requestrole(self, ctx, discordid, roleid):
discordid = int(discordid)
roleid = int(roleid)
userid = ctx.author.id
channel = self.bot.get_channel(FILLWITHCHANNELID)
guild = ctx.guild
user = ctx.author
role = get(guild.roles, id=roleid)
embed = discord.Embed(title="Role Request", description=f"{user} requested {role} for {userid}", color=0x00ff00)
await channel.send(embed=embed)
message = await embed.send(embed=embed)
await message.add_reaction("✅")
await message.add_reaction("❌")
#self.bot.event
async def on_reaction_add(reaction, user):
if reaction.emoji == "✅":
user = reaction.message.author
guild = reaction.message.guild
role = get(guild.roles, id=roleid)
await user.add_roles(role)
await channel.send(f"{user} has been given {role}")
elif reaction.emoji == "❌":
await reaction.message.send("Role request denied")
This is the error I am now receiving
Ignoring exception in command requestrole:
Traceback (most recent call last):
File "C:\Users\name\AppData\Roaming\Python\Python310\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\name\Documents\GitHub\PRP-Bot\cogs\role_cog.py", line 31, in requestrole
message = await embed.send(embed=embed)
AttributeError: 'Embed' object has no attribute 'send'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\name\AppData\Roaming\Python\Python310\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\name\AppData\Roaming\Python\Python310\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\name\AppData\Roaming\Python\Python310\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: 'Embed' object has no attribute 'send'
My question particularly is what is causing the error, and how do I properly code it so a person can basically click on the reaction checkmark to add the role, or click the x to remove the role.
you are trying to send a message to an Embed. That's not how Embeds work. you can send a message to a channel but not to a Embed. Just remove the line message = await embed.send(embed=embed) and you should be fine
I intend, if you send the bot a certain DM that it then gives a role on a certain server.
This is my code what I tried:
#bot.event
async def on_message(message):
if message.author.bot:
return
elif message.content.startswith("++neunundvierzig++"):
role = message.author.guild.get_role(861543456908640298)
guild = bot.get_guild(816021180435529758)
await message.channel.send("Erfolgreich Zerifiziert!")
await member.add_roles(role)
It says in the log:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\aaa12\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "f:\HTML CSS JS Py Projekte\Python\bot_check\bot.py", line 36, in on_message
role = message.author.guild.get_role(861543456908640298)
AttributeError: 'User' object has no attribute 'guild'
I think you defined role wrong. Your code says that message.author.guild doesn't exist, so you should try message.guild.
Like this:
role = message.guild.get_role(861543456908640298)
Aside from that, you don't defined member. Just rename it to await message.author.add_roles(role) instead of await member.add_roles(role)
Sources
Discord Messages
I want to make bot react to every message in specific channel, this is my code:
#bot.event
async def on_message(message):
channel = bot.get_channel(837590298435649537)
if message in channel:
await message.add_reaction("✅")
And this is error:
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 15, in on_message
if message in channel:
TypeError: argument of type 'TextChannel' is not iterable
I don't know what's wrong, please help
You can't use message in channel you would have to use
if message.channel == channel:
await message.add_reaction("✅")
I am currently trying to make a command with discord.py to reset all user nicknames on a server (!!!mreset). However I'm getting a type error which tells me 4 arguments were given.
Code:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!!!mreset'):
serverid = message.server.id
x = message.server.members
for member in x:
await client.change_nickname(serverid, member.name, None)
Error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Saber\PycharmProjects\my_bot\venv\lib\site-packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:/Users/Saber/PycharmProjects/my_bot/my_bot.py", line 32, in on_message
await client.change_nickname(serverid, member.name, None)
TypeError: change_nickname() takes 3 positional arguments but 4 were given
You don't need to pass the serverid, or a Server object to change_nickname. Member objects have a server associated with them, so the command can get the appropriate Server from the Member object. You should also be passing a Member object and not the Member.name attribute.
if message.content.startswith('!!!mreset'):
for member in message.server.members:
await client.change_nickname(member, None)