Discord.py | Cooldown for events - python

The task of my code:
When the moderator issues or removes roles from any server participant, the bot reads the logs, and then sends a message to the specified channel about which role was changed and to which, and so on.
My problem:
When a moderator adds or removes several roles at once, the bot sends a message with information for each role at once. But I want there to be a delay when sending the event. This clogs up the chat logs and is annoying.
For example:
I delete 5 roles at once, the bot has a delay of sending a message of 30 seconds. And in this message, he adds all 5 roles, not one at a time.
CODE:
#Bot.event
async def on_member_update(before, after):
if before.roles != after.roles:
channel = Bot.get_channel(827986763606786099)
emb = discord.Embed(description = f'**Updating user roles - {before.mention}**', colour = discord.Color.red())
emb.add_field(name = '**Roles before**', value = ", ".join([r.mention for r in before.roles]))
emb.add_field(name = '**Roles after**', value = ", ".join([r.mention for r in after.roles]))
async for event in before.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
if getattr(event.target, "id", None) != before.id:
continue
emb.add_field(name="Changed roles", value = ", ".join([getattr(r, "mention", r.id) for r in event.before.roles or event.after.roles]))
emb.add_field(name="Moderator", value = event.user)
break
await channel.send(embed = emb)
https://i.stack.imgur.com/v3h1v.jpg

Before running be sure to import asyncio import asyncio
cooldown = []
#bot.event()
async def on_member_update(before, after):
if before.roles != after.roles:
global cooldown
if before in cooldown:
return
cooldown.append(before)
await asyncio.sleep(10) #here you can change how long the cooldown should be
cooldown.remove(before)
channel = bot.get_channel(688344722082627686)
emb = discord.Embed(description=f'**Updating user roles - {before.mention}**', colour=discord.Color.red())
emb.add_field(name='**Roles before**', value=", ".join([r.mention for r in before.roles]))
emb.add_field(name='**Roles after**', value=", ".join([r.mention for r in after.roles]))
changed_roles = []
for role in before.roles:
if role in after.roles:
pass
else:
changed_roles.append(role)
for role in after.roles:
if role in before.roles:
pass
else:
if role in changed_roles:
pass
else:
changed_roles.append(role)
text = ""
for role in changed_roles:
text = text + role.mention
emb.add_field(name="Changed roles", value=text)
async for event in before.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
if getattr(event.target, "id", None) != before.id:
continue
emb.add_field(name="Moderator", value=event.user)
break
await channel.send(embed=emb)
Had to change getting the changed roles a bit, since I wasn't able to get how many audit logs I should have fetch in.
So what happens is:
user is getting added to cooldown list, bot waits for 10 seconds so that the moderator can finish removing/adding the roles, after that bot gathers them all, removes the user from cooldown and sends the embed.

Here is the fully working code:
#Bot.event
async def on_member_update(before, after):
if before.roles != after.roles:
global cooldown
if before in cooldown:
return
cooldown.append(before)
await asyncio.sleep(5) # here you can change how long the cooldown should be
cooldown.remove(before)
channel = Bot.get_channel(ID log channel)
emb = discord.Embed(description=f'**Updating user roles - {before.mention}**', colour=discord.Color.orange())
emb.add_field(name='Roles before', value=", ".join([r.mention for r in before.roles][1:]), inline=False)
emb.add_field(name='Roles after', value=", ".join([r.mention for r in after.roles][1:]), inline=False)
changed_roles = []
for role in before.roles:
if role in after.roles:
pass
else:
changed_roles.append(role)
for role in after.roles:
if role in before.roles:
pass
else:
if role in changed_roles:
pass
else:
changed_roles.append(role)
text = ""
blacklist=[797920206407598098,817750571330961410,797916381621256202] # list roles, that should not be read
for role in changed_roles:
if role.id in blacklist:
return
text = text + role.mention
emb.add_field(name="Changed roles", value=text, inline=False)
async for event in before.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
if getattr(event.target, "id", None) != before.id:
continue
emb.add_field(name="Moderator", value=f"{event.user} \n**ID**: {event.user.id}")
break
await channel.send(embed=emb)
Thank you very much for helping #NimVrod

Related

How to add role by reacting in DM to a bot message

Im trying to make a bot that send me a message with a reaction button, when I click it, give me a role in the server. Im trying to use on_raw_reaction_add event but I cant reach a solution to make it, Im always getting errors at getting guild roles and this stuff.
In this case, guild is none, I dont know what Im doing wrong.
My code:
#client.command()
async def test(ctx):
global member
global message__id
global channel_id
channel_id = (ctx.channel.id)
member = ctx.message.author
embed = discord.Embed(title="Verify your account", color=0x03fc14)
embed.add_field(name=f"Verification!", value=('React to this message to get verified!'), inline=False)
embed.set_footer(text=ctx.author, icon_url=ctx.author.avatar_url)
mesg = await member.send(embed=embed)
await mesg.add_reaction("✅")
message__id = mesg.id
print("EXECUTED")
#client.event
async def on_raw_reaction_add(payload):
print("reacted")
print("messageid accepted")
guild = client.get_guild(payload.guild_id)
if guild is not None:
print("messageid accepted")
reactor = payload.guild.get_member(payload.member.id)
role = discord.utils.get(guild.roles, name="Member")
if payload.emoji.name == '✅':
print("emoji accepted")
await reactor.add_roles(role)
EDIT:
I changed my on_raw_reaction_add event:
#client.event
async def on_raw_reaction_add(payload):
print("reacted")
print("messageid accepted")
guild = client.get_guild(payload.guild_id)
if guild is not None:
print("guild not none ")
reactor = payload.guild.get_member(payload.member.id)
role = discord.utils.get(guild.roles, name="Verified")
if payload.emoji.name == '✅':
print("emoji accepted")
await reactor.add_roles(role)
else:
print("guild none")
And this is what happen when I try the method:
First three words are the prints of the bot reacting his own message, the "EXECUTED" is the print of test method and the last three messages. are when I react the bot message
In this case, I suggest using a wait_for rather than on_raw_reaction_add.
Example, as seen in the docs:
#client.command(aliases=["t"])
async def test(ctx):
embed = discord.Embed(title="Verify your account", color=0x03fc14)
embed.add_field(name=f"Verification!", value=('React to this message to get verified!'), inline=False)
embed.set_footer(text=ctx.author, icon_url=ctx.author.avatar_url)
# sends message to command author
mesg = await ctx.author.send(embed=embed)
await mesg.add_reaction("✅")
#check if the reactor is the user and if that reaction is the check mark
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) == '✅'
try:
#wait for the user to react according to the checks
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
# 60 seconds without reaction
await ctx.send('Timed out')
else:
await ctx.send('✅')
docs- https://discordpy.readthedocs.io/en/stable/api.html?highlight=wait_for#discord.Client.wait_for
async def test(ctx):
global member
global message__id
global channel_id
channel_id = (ctx.channel.id)
member = ctx.message.author
embed = discord.Embed(title="Verify your account", color=0x03fc14)
embed.add_field(name=f"Verification!", value=('React to this message to get verified!'), inline=False)
embed.set_footer(text=ctx.author, icon_url=ctx.author.avatar_url)
mesg = await member.send(embed=embed)
await mesg.add_reaction("✅")
message__id = mesg.id
print("EXECUTED")
role = get(ctx.guild.roles, id=role id here)
done = False
while done == False:
reaction, user = await client.wait_for(“reaction_add”, check=check)
if user == client.user:
continue
await member.add_roles(role)
done = True
I am pretty sure something like this would work :D
When you do guild is not None, it will never fire because you are reacting in a DM channel context - where the guild is None. To access the guild information from the DM, you'll need to save it somewhere in the message (or save it yourself internally). Then, you have the guild object and you're able to add roles or do things to it.
#client.command()
async def testrole(ctx):
member = ctx.author
# ...
embed = discord.Embed(title="Verify your account", color=0x03fc14)
embed.add_field(name=f"Verification!", value=('React to this message to get verified!'), inline=False)
embed.set_footer(text=ctx.author, icon_url=ctx.author.avatar_url)
mesg = await member.send(str(ctx.guild.id), embed=embed) # we need the guild information somewhere
await mesg.add_reaction("✅")
#client.event
async def on_reaction_add(reaction, user): # there's really no need to use raw event here
if user.id == client.user.id:
return
if reaction.message.guild is None:
if reaction.emoji == '\u2705':
# we have to get the guild that we saved in the other function
# there is no other way to know the guild, since we're reacting in the DM
# that's why it was saved inside of the message that was sent to the user
guild_id = int(reaction.message.content)
guild = client.get_guild(guild_id)
member = guild.get_member(user.id)
await member.add_roles(...) # change this with your roles

Delete channel on reaction | discord.py

I am creating ticket tool for my server. So whenever a user will react to the message a new channel will be created, but I want the bot to send a message in that new channel saying "React below to delete the ticket.". But can I do that without storing msg_id and other data in a global variable?
Code:
#bot.event
async def on_raw_reaction_add(payload):
if payload.member.id != bot.user.id and str(payload.emoji)== u"\U0001F3AB":
msg_id, channel_id, category_id= bot.ticket_configs[payload.guild_id]
if payload.message_id == msg_id:
guild= bot.get_guild(payload.guild_id)
for category in guild.categories:
if category.id == category_id:
break
guild = await bot.fetch_guild(460654239094669332)
channels = guild.text_channels
channel = guild.get_channel(842807548968042536)
duplicate = False
topic = f"<#!{payload.member.id}>'s ticket"
for channel in channels:
if topic == channels.topic:
duplicate = True
break
if duplicate:
return
else:
ticket_num= 1 if len(category.channels) == 0 else int(category.channels[-1].name.split("-")[1]) + 1
ticket_channel= await category.create_text_channel(f"Ticket-{ticket_num}", topic= topic, permission_synced= True)
await ticket_channel.set_permissions(payload.member, read_messages= True, send_messages= True)
print(msg_id)
embed= discord.Embed(title= "New Ticket!!!", description= f"{payload.member.mention} Thank You! for creating this ticket staff will contact you soon. Type **-close** to close the ticket.")
message= await ticket_channel.send(embed=embed)
await message.add_reaction("❌")
Help will be really appreciated
You can use bot.wait_for with a simple check
message = await ctx.send("React below to delete the ticket")
def check(reaction, user):
return reaction.message == message and str(reaction) == "✅" # you can use another emoji, or don't use it at all
try:
await bot.wait_for("reaction_add", check=check, timeout=60.0) # timeout is optional
except asyncio.TimeoutError:
...
else:
await channel.delete() # where `channel` is the channel to delete

Discord.py: How to Ping User Through a User ID Variable

I have a queue system bot, BUT I need to store the author name/id. So I have 2 commands. One when the number of queues is 0 and 1. I can easily use message.author.mention but how would I ping the first id (the one where queue = 0) in the same message.
Code:
import discord
import os
from discord.utils import get
from discord.ext import commands
#run the Bot and a message to make sure it ran
client = discord.Client()
global queue
queue = 0
global author
author = " "
global author2
author2 = " "
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
await client.change_presence(activity=discord.Game('Minceraft'))
#client.event
async def on_message(message): #Defines Message
#If You Queue, And Role Requirement to Do So
if message.content.startswith('-queue duos'):
global queue
if queue == 0:
role = discord.utils.get(message.guild.roles, id=830557149863608367)
if role in message.author.roles:
print('you have queued')
global author
author = message.author.id
print(author)
#Adds Role Duos
role = discord.utils.get(message.guild.roles, id=830219965549510656)
await message.author.add_roles(role)
#Removes Role NoQueue
role4 = discord.utils.get(message.guild.roles, id=830557149863608367)
await message.author.remove_roles(role4)
#Gives Message
await message.channel.send('you have joined the duos queue sit tight!')
queue += 1
await message.channel.send(queue)
elif queue == 1:
role = discord.utils.get(message.guild.roles, id=830557149863608367)
if role in message.author.roles:
print('you have queued')
global author2
author2 = message.author.id
print(author2)
#Adds Role Duos
role = discord.utils.get(message.guild.roles, id=830219965549510656)
await message.author.add_roles(role)
#Removes Role NoQueue
role4 = discord.utils.get(message.guild.roles, id=830557149863608367)
await message.author.remove_roles(role4)
#Gives Message
await message.channel.send('you have joined the duos queue sit tight!')
queue += 1
#Work On Pings Here
await message.channel.send(message.author.mention)
await message.channel.send(author)
queue = 0
#Is They Already Queued and Have Duos Role
else:
await message.channel.send('You Already Queued!')
#if the message is unqueue duos command
if message.content.startswith('-unqueue duos'):
#Role Requirement
role = discord.utils.get(message.guild.roles, id=830219965549510656)
if role in message.author.roles:
print('you have unqueued')
#Remove Duos Role
role2 = discord.utils.get(message.guild.roles, id=830219965549510656)
await message.author.remove_roles(role2)
#Adds NoQueue Role
role3 = discord.utils.get(message.guild.roles, id=830557149863608367)
await message.author.add_roles(role3)
await message.channel.send('You Have Unqueued Duos!')
queue -= 1
#Already Not In a Queue
else:
await message.channel.send('You Are Not In a Queue!')
#Runs Token From The .env File
client.run(os.getenv('TOKEN'))
TL;DR: I want to ping someone using a global string because I can't use message.author.mention with 2 people at once or in the same message.channel.send command.
<#USERID> is how you mention someone. Just get the id and change USERID for that. What you could do is:
await message.channel.send(f"<#{USERID}>")
because f strings format things for you. That code would mention someone.

Discord.py - Checking if a user has a role based on the channel

so right now I'm trying to create a command which basically creates overrides that blocks a specific user from viewing the channel. The only thing I am having trouble with is the part where the bot verifies if the user "owns" the channel. There are a few channels which are player owned and they have a role that matches up with the channel's name.
So as an example:
#space-invaders
#Space Invaders OP
The issue in the code is that while trying to convert the roles into string, it fails to do so. So I need an alternative to this and I have no clue what else I could do.
#commands.command()
#commands.has_role("Realm OP")
async def block(self, ctx, user: discord.User):
#channel = await ctx.author.create_dm()
channel = ctx.message.channel
author = ctx.message.author
mentions = [role.mention for role in ctx.message.author.roles if role.mentionable]
channel2 = str(channel.name)
channel = channel2.split('-')
if len(channel2) == 2: # #real-emoji
realm, emoji = channel
else: # #realm-name-emoji
realm, emoji = channel[0], channel[-1]
realmName = realm.replace("-" , " ")
realmName1 = realmName.lower()
rolelist = []
authorRoles = discord.Role.name(author.roles) # Issue here
for role in authorRoles:
rolen = role.lower()
rolelist.append(rolen.mention)
if realmName1 in rolelist:
await ctx.send("true")
else:
await ctx.send("false")
Any suggestions would greatly help!
You would check if the role is in the author roles.
First: Change the channel name into the format of the role space-invaders->Space Invaders OP. Which means replace, title and OP at the end
Secondly: Get the role from the guild using discord.utils.get and check if the author has it.
#commands.command()
#commands.has_role("Realm OP")
async def block(ctx, user: discord.User):
check_name = f'{ctx.channel.name.replace("-", " ").title()} OP'
check_role = discord.utils.get(ctx.guild.roles, name= check_name)
if check_role not in ctx.author.roles:
return await ctx.send('You do not own this channel')
# code here.

How to delete a voice channel the user is in?

Im making a command where a user can create and delete a private voice channel. For the part where i want to add or delete a channel it wants me to add a variable. I need it to delete the voice channel the user is in. Than you in advance
Code:
client.command()
async def room(ctx, activity=None, *, member : discord.Member = None):
if activity == "STREAMING".lower():
guild = ctx.guild
user = ctx.author
await guild.create_voice_channel(f"Streaming Room - {user}")
await ctx.send("Created streaming room!")
elif activity == "RECORDING".lower():
guild = ctx.guild
user = ctx.author
channel = await guild.create_voice_channel(f"Recording Room - {user}")
everyone = ctx.message.author.guild.default_role
disallow = discord.PermissionOverwrite()
disallow.read_messages = False
disallow.send_messages = False
await channel.set_permissions(everyone, overwrite=disallow)
await ctx.send("Created recording room!")
elif activity == 'ADD'.lower():
await ctx.send(f"{member.mention} has been added to your room")
allow = discord.PermissionOverwrite()
allow.connect = True
allow.speak = True
await channel.set_permissions(member, overwrite=allow)
else:
await ctx.send('Please specify what activity you would like to be doing [streaming, recording]')
General command for the task:
#client.command()
async def delvc(ctx, member: discord.Member):
vc = member.voice.channel
if vc:
await vc.delete()
await ctx.send(f"{member.name}'s voice channel - `{vc.name}` - was deleted!")
else:
await ctx.send(f"{member.name} isn't in a voice channel at the moment.")
References:
Member.voice
discord.VoiceChannel
VoiceChannel.delete()

Categories