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}")
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)
i need a ban command that bans a member and than dms the banned member to inform them about it and when it happened.
I have a few problems:
What if the user's dms are closed?
It is not dming the banned member.
Also includes a perm ban like `!Ban
If anyone can help me out that will be great and i will greatly appricate it.
Thanks!
Like you want, this is example of simple ban member for discord.py
#client.command()
async def ban(ctx, member:discord.Member, *, reason=None)
await member.ban
await member.send(f"You've been banned by {ctx.author}\nReason = {reason}")
If you looking for .cogs version
#commands.command()
async def ban(self, ctx, member:discord.Member, *, reason = None)
await member.ban
await member.send(f"You've been banned by {ctx.author}\nReason = {reason}") ##this send message by DMs for banned member to tell him/her why getting banned
If you using discord-py-slash-command you can create new question
for the 1:
try:
await user.send("msg")
except discord.errors.Forbidden:
code here
I don't know if this can help you
So I'm working on a discord bot using discord.py and I'm trying to create a bot for the moderation team in a server, the bot will swap the 'Moderator' role with a 'Leave of absence' role for when they're not active, however the code I have come up with has a slight loopholing problem that I just can't figure out, the code for the commands is this
...
#client.command()
#commands.has_role('Moderator')
async def sl(ctx, member: discord.Member = None):
if not member:
member = ctx.author
loa = ctx.guild.get_role(848032714715561985)
mod = ctx.guild.get_role(848032880709074944)
await member.add_roles(loa)
await member.remove_roles(mod)
await ctx.send("I have filed your Leave, take care, we look forward to your return!")
#client.command()
async def sr(ctx, member: discord.Member = None):
if not member:
member = ctx.author
mod = ctx.guild.get_role(848032880709074944)
loa = ctx.guild.get_role(848032714715561985)
await member.add_roles(mod)
await member.remove_roles(loa)
await ctx.send("Welcome back!")
...
as you can see anyone could use the second command to just give themselves a moderator role, I can't set the second command to be moderator only use as the moderator will no longer have said role from using the first command, I'm racking my brain to think of a work around i.e. logging the command users id to a whitelist and having only those whitelisted id's be able to use the second command, I've done many googlesearches for this but have come back with no results, any suggestions would be appreciated, please forgive that this question is a bit lengthy and I'm still very new to coding in general so any help at all, even if you don't fully understand what I'm blabbering on about would be very appreciated, thank you.
Check for the loa role in the command (ex):
mod = None
for role in ctx.author.roles:
if role.id == 848032714715561985: mod = True
if mod:
#your code here
So from your question, I'm guessing that you would like to code basically a "storage" file and make sure the person on the leave of absence was previously a moderator.
What you could do is create a csv file, for example records.csv (in the same folder as your main .py file of course), and every time someone calls the sl command, the program will record the user that used it.
import csv
#client.command()
#commands.has_role('Moderator')
async def sl(ctx, member: discord.Member = None):
if not member:
member = ctx.author
loa = ctx.guild.get_role(848032714715561985)
mod = ctx.guild.get_role(848032880709074944)
await member.add_roles(loa)
await member.remove_roles(mod)
file = open("records.csv", "w")
file.writelines(str(ctx.author.id))
file.close()
await ctx.send("I have filed your Leave, take care, we look forward to your return!")
#client.command()
async def sr(ctx, member: discord.Member = None):
if not member:
member = ctx.author
mod = ctx.guild.get_role(848032880709074944)
loa = ctx.guild.get_role(848032714715561985)
found = False
with open('records.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == str(ctx.author.id):
found = True
break
if found = False:
await member.add_roles(mod)
await member.remove_roles(loa)
await ctx.send("Welcome back!")
else:
await ctx.send("We do not have history of you having a moderator role.")
Are you running the program through an online environment like Repl.it? If so, this may not be the best way to approach this problem since people would have access to your this records.csv file (which you may not care about but just in case). If you are running the program through your desktop file directories, then there should be no privacy concerns.
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
I'm trying to do a bot that picks up all roles from user, send a message that mention all roles in a channel, so the bot need remove all roles and add a 'Prisoner' role and send a reason for the prison. How do I do this?
I'm trying to do this command for 3 days, but nobody could help me.
You can try this for deleting all roles and adding prisoner role:
#client.command()
async def prison(ctx, member: discord.Member):
member_roles = []
for role in member.roles:
member_roles.append(role)
await member.remove_roles(role)
member_roles = ', '.join(member_roles)
await ctx.send(f'{member.mention} is in prison. His {member_roles} roles are deleted.')
prisoner = discord.utils.get(ctx.guild.roles, name='Prisoner')
await member.add_roles(prisoner)
After you create the Prisoner role, change the permissions of this role for all the channels except for Prison channel in Discord.
Note: There could be syntax problems in my code because I'm on mobile right now. If any problem raises, just comment.