Discord.py Reaction role command - discord.py

I want my bot to make a reaction role when I say p!reactrole [emoji] [role] [message], but it isn't working. No errors or anything.
#client.command()
async def reactrole(ctx, emoji, role: discord.Role, *, message):
embedVar = discord.Embed(description=message)
msg = await ctx.channel.send(embed=embedVar)
await msg.add_reaction(emoji)
with open('reactrole.json') as json_file:
data = json.load(json_file)
new_react_role = {
'role_name':role.name,
'role_id':role.id,
'emoji':emoji,
'message_id':msg.id
}
data.append(new_react_role)
with open('reactrole.json', 'w') as j:
json.dump(data,j,indent=4)

The code looks good but you don't have gateway intents enabled. If your bot is verified, you'll have to email discord at dis.gd/contact. If your bot is not verified, you can enable them in the developer portal under the bot tab. Once you enable the intent, you'll have to also enable them in your code.
intents = discord.Intents.<name of intent or 'all' if you want all intents>()
client = commands.Bot(..., intents=intents)

Related

on_member_join function not working in discord.py

I'm working on a bot and the on_member_join function isn't working when a user joins the server. My function is in a cog and is the only function that won't work,
def __init__(self, client):
self.client = client
#commands.Cog.listener()
async def on_ready(self):
#commands.Cog.listener()
async def on_member_join(self, member):
print('working')
I am assuming the indentation is just wrong while pasting here and not in the actual code.
If you haven't done this already, the new version of discord.py has something called intents. First you have to go to the developer portal and activate it. Then you have to add the intents to your code.
This will enable all the Intents:
intents = discord.Intents.all()
client = commands.Bot(command_prefix = '-', intents=intents)

Using CTX in on_reaction_add event -- Discord.py

async def on_reaction_add(reaction, user):
emoji = reaction.emoji
if emoji == "💌":
await user.channel.send("HI")
I got problem here with user.
I want to use here with ctx like ctx.channel.send.
but also it occured error how to use ctx in here?
Instead of using the on_reaction_add event, it's better in this case to use a wait_for command event. This would mean the event can only be triggered once and only when the command was invoked. However with your current event, this allows anyone to react to a message with that emoji and the bot would respond.
By using client.wait_for("reaction_add"), this would allow you to control when a user can react to the emoji. You can also add checks, this means only the user would be able to use the reactions on the message the bot sends. Other parameters can be passed, but it's up to you how you want to style it.
In the example below shows, the user can invoke the command, then is asked to react. The bot already adds these reactions, so the user would only need to react. The wait_for attribute would wait for the user to either react with the specified emojis and your command would send a message.
#client.command()
async def react(ctx):
message = await ctx.send('React to this message!')
mail = '💌'
post = '📮'
await message.add_reaction(mail)
await message.add_reaction(post)
def check(reaction, user):
return user == ctx.author and str(
reaction.emoji) in [mail, post]
member = ctx.author
while True:
try:
reaction, user = await client.wait_for("reaction_add", timeout=10.0, check=check)
if str(reaction.emoji) == mail:
await ctx.send('Hi you just received')
if str(reaction.emoji) == post:
await ctx.send('Hi you just posted...')
You need to use reaction.message.channel.send
async def on_reaction_add(reaction, user):
emoji = reaction.emoji
if str(emoji) == "💌": await reaction.message.channel.send("HI")

discord bot python auto-role(help)

import discord
from discord.ext import commands
print(discord.__version__)
client = commands.Bot(command_prefix = '!')
#client.event
async def on_ready():
print('bot is online')
#client.event
async def on_member_join(member):
role = get(member.guild.roles, name="Members")
await member.add_roles(role)
print(f"{member} was given {role}")
client.run(removed)
what am I doing wrong here? I am trying to make an auto-join role when somebody joins it gives him a role
In version 1.5 intents were introduced which directly effect events like on__member_join. You would need to enable it in the Discord Developer Portal under bot in your application. Once that is enabled you would want to add this:
intents = discord.Intents.default()
intents.members = True

How to make a discord.py reaction role code

I'm new to discord bot making, and I'd like to have the bot add a reaction to my message that, once pressed, allows the user reacting to pick up a role. What would be the code to allow me to do this? Thank you.
This is a very well written and formatted question! In order for the bot to detect that the reaction is on a specific message, you can do many ways. The easiest way would be to be by ID, so I'm just going to do this with a simple command.
messageIDs = []
#client.event
async def on_raw_reaction_add(payload):
global messageIDs
for messageID in messageIDs:
if messageID == payload.message_id:
user = payload.member
role = "roleName"
await user.add_roles(discord.utils.get(user.guild.roles, name = role))
#client.command()
async def addMessage(ctx, messageID):
global messageIDs
emoji = "👍"
channel = ctx.message.channel
try:
msg = await channel.fetch_message(messageID)
except:
await ctx.send("Invalid Message ID!")
return
await msg.add_reaction(emoji)
messageIDs.append(messageID)

Turning invite code to url discord rewrite

I set up a command on my bot to pull active invites from servers but cant seem to turn the results into a url.
Ive tried to turn {invites} to {invites.url} and it did'nt seem to work
#commands.is_owner()
#commands.command()
async def fi(self, ctx, id: int):
server = self.bot.get_guild(id)
invites = await server.invites()
fetching = await ctx.send("Fetching Invites...")
await fetching.edit(content=f"All Active Invites Codes: \n {invites}")
await asyncio.sleep(8)
await fetching.delete()
# Fetches invite codes
Guild.invites returns a list of Invites, so you need to get the url attribute of each Invite individually.
invites = await server.invites()
invites = [invite.url for invite in invites]
invites_message = "\n".join(invites)
await fetching.edit(content=f"All Active Invites Codes: \n{invites_message}")

Resources