I'm currently using this code to assign prefix to nickname if users has specific role:
#client.event
async def on_member_update(before, after):
role = discord.utils.get(before.guild.roles, name="FK")
if after.nick is not None and after.nick.startswith("F |"):
return
if after in role.members:
await after.edit(nick="FK | " + after.display_name, reason=None)
How can i remove (or reset the nickname) if the the role has been removed from the user?
I tried using if after not in role.members but it's not working, so i guess i'm not doing the right thing.
Related
I searched for it, but didn't find anything that could really help. Basically, I would like that when I run .highrank in a discord channel, the bots gives me a list of the people that have this role.
This is the current code I have:
#client.command()
async def highrank(ctx):
role =
await ctx.send(role.members)
I do not know how to make sure that this will give me a list of the people with the high rank role in the server.
Edit: I found this, but I only get the bot name and ID when I do the command, and only if the bot has the role.
#client.command(pass_context=True)
async def members(ctx, *args):
server = ctx.message.guild
role_name = (' '.join(args))
role_id = server.roles[0]
for role in server.roles:
if role_name == role.name:
role_id = role
break
else:
await ctx.send("Role doesn't exist")
return
for member in server.members:
if role_id in member.roles:
await ctx.send(f"{member.display_name} - {member.id}")
#client.command()
async def userrole(ctx, role: discord.Role):
# this will give the length of the users in role in an embed
members_with_role = []
for member in role.members:
members_with_role.append(member.name)
embed = discord.Embed(title=f"**Users in {role.name}: **{len(members_with_role)}", description="\n ".join(member.mention for member in role.members)
await ctx.send(embed=embed)
To get a role by its ID:
ctx.guild.get_role(123456789)
You would replace 123456789 with the role id you get by right-clicking the role (with Developer Mode enabled in Discord).
#client.command()
async def highrank(ctx):
HIGHRANK_ROLE_ID = 123456789 # replace this with your role ID
role = ctx.guild.get_role(HIGHRANK_ROLE_ID)
await ctx.send(role.members)
Alternatively, you can use ctx.guild.roles and locate the role by name using a helper such as discord.utils.find.
#client.command()
async def highrank(ctx):
# replace the role name with your "High Rank" role's name
role = discord.utils.find(lambda m: m.name == 'High Rank', channel.guild.roles)
await ctx.send(role.members)
Basically the bot is supposed to give you the "Cool role" role if your status is "cool" But for some reason, it doesn't and it also doesn't give me any errors. Also, it doesn't seem like the event triggers at all.
# Token, Client
intents = discord.Intents().all() # Turn on intents
client = commands.Bot(command_prefix='.',
intents=intents) # initialize client
# Gives role if you have status
#client.event
async def on_member_update(before, after):
# get differences in before and after activities
differences = set(before.activities) ^ set(after.activities)
# If differences is none, return
if len(differences) == 0:
return
# utils.get the differences and look for the custom activity type with the name
activity = discord.utils.get(differences, name="Cool")
# if activity is None, return
if activity is None:
return
# if activity in before.activities, remove the role
role = after.guild.get_role("Cool role")
if activity in before.activities:
await after.remove_roles(role)
# else add the role
else:
await after.add_roles(role)
member_update event is no longer dispatched when a member's status or activity updates. There is presence_update instead.
You also need the presences intent as well as the members intent enabled.
#client.event
async def on_presence_update(before, after):
pass
Reference
I'm new to discord.py and I'm a intermediate student, I made a program to give a role to a specific user id, but I encounter some problem that I cannot solve it, anyone can help?
#tasks.loop(seconds=5)
async def change_status():
for number in range(len(data_dict["username"]) - 1):
if data_dict["year_start"][number] == year_now and data_dict["month_start"][number] == month_now and data_dict["date_start"][number] == date_now:
userid = int(data_dict["username"][number])
member = Guild.get_member(userid)
role = Guild.get_role(840609566093606953)
member.add_role(role)
Have you set the guild object correctly,
You need the line:
Guild = client.get_guild(ID)
You can find the guild Id by enabling dev mode in user settings and then right clicking on the server name when your server is open
You may have seen my previous question (How do I make a lockdown command?), where I asked about how to lockdown specified channel. But that command only locked down the # everyone role. My server has millions of roles and channels made for other roles, so I want to know how to change ctx.guild.default_role to something where you specify the role and the channel locks down for that role. Current command:
#commands.command()
#commands.has_permissions(manage_channels=True)
async def lockdown(self, ctx):
await ctx.channel.set_permissions(ctx.guild.default_role, send_messages=False)
await ctx.send(ctx.channel.mention + " ***is now in lockdown.***")
You can simply pass the role as an argument
async def lockdown(self, ctx, role: discord.Role): # `RoleConverter` will automatically convert it to a `discord.Role` instance
await ctx.channel.set_permissions(role, send_messages=False)
await ctx.send(ctx.channel.mention + " ***is now in lockdown.***")
You can invoke it by mentioning the role, by putting the role ID or simply by passing the name of the role
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}")