I'm trying to remove a role from a user with a command but I'm not quite sure how the command works.
Here is the code:
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
await bot.remove_roles(user, 'member')
await bot.say("{} has been muted from chat".format(user.name))
It looks like remove_roles needs a Role object, not just the name of the role. We can use discord.utils.get to get the role
from discord.utils import get
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role = get(ctx.message.server.roles, name='member')
await bot.remove_roles(user, role)
await bot.say("{} has been muted from chat".format(user.name))
I don't know what happens if you try to remove a role that the target member doesn't have, so you might want to throw in some checks. This might also fail if you try to remove roles from an server admin, so you might want to check for that too.
To remove a role, you'll need to use the remove_roles() method. It works like this:
member.remove_roles(role)
The "role" is a role object, not the (role's) name, so it should be:
role_get = get(member.guild.roles, id=role_id)
await member.remove_roles(role_get)
With your code:
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role_get = get(member.guild.roles, id=role_id) #Returns a role object.
await member.remove_roles(role_get) #Remove the role (object) from the user.
await bot.say("{} has been muted from chat".format(user.name))
The remove_roles() function takes two parameters, the first of which is the user from which the role and the role itself are removed. It is possible not to write user: discord.Member in the function parameters, leave only ctx. You also need the role you want to delete. This can be done by:
role = get (ctx.author.guild.roles, name = "the name of the role you want to remove")
#also include 'from discord.utils import get'
And then to remove the obtained role (object), call the function:
await ctx.author.remove_roles (role)
So, the final code should look something like this:
from discord.utils import get
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx):
role = get (ctx.author.guild.roles, name = "the name of the role you want to remove")
await ctx.author..remove_roles(role)
await ctx.send("{} has been muted from chat".format(user.name))
Try that:
import discord
from discord.ext import commands
#bot.command()
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role = discord.utils.get(ctx.guild, name='member')
await user.remove_roles(role)
await ctx.send(f'{user.name} has been muted from chat')
Related
For example, a person mentions the role of #role1 and the bot gives him this role. How to do it?
For the rewrite branch:
role = discord.utils.get(ctx.guild.roles, name="role to add name")
user = ctx.message.author
await user.add_roles(role)
For the async branch:
user = ctx.message.author
role = discord.utils.get(user.server.roles, name="role to add name")
await client.add_roles(user, role)
I think this is a good solution. Just make it so it requires a role, or a role mention.
We basically make a command where it requires two arguments: a role, and a mentioned member. You can configure this to make it so only administrators can run this command. If the Role doesn't exist, it will send a "This is not a correct role" message. Let me know if you face any issues!
from discord.ext import commands
from discord.utils import get
import discord
intents = discord.Intents.all()
client = commands.Bot(command_prefix = '!', intents=intents, case_intensitive=True)
#client.event
async def on_ready():
print('Bot is online!')
#client.command()
async def addrole(ctx, user: discord.Member, role: discord.Role):
try:
await user.add_roles(role)
await ctx.send(f'{user.mention} was given the role: {role.name}')
except commands.BadArgument:
await ctx.send('That is not a correct role!')
client.run('TOKEN')
I wanna do a ban appeal system on my server. I wanna remove every single role of someone and give them the [Banned] role. I cannot just do something like I did for the member role for all of them since there are a lot of roles, even some custom ones that are made and deleted every day.
member_role = get(user.guild.roles, name="γβ
γΒ· πΌπππππ")
await user.remove_roles(member_role, reason=None, atomic=True)
I tried this: discord.py trying to remove all roles from a user but it didn't work. Also tried this:
for role in user.roles:
if role.name == '[Banned]':
pass
else:
await user.remove_roles(role)
but couldn't get it to work. (I have no experience in python or discord.py)
So. How can I remove every role from a user instead of only the member_role ?
#bot.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member, *, reason=None):
await asyncio.sleep(1)
banned_role = get(user.guild.roles, name="[Banned]")
await user.add_roles(banned_role, reason=None, atomic=True)
member_role = get(user.guild.roles, name="γβ
γΒ· πΌπππππ")
await user.remove_roles(member_role, reason=None, atomic=True)
banemb = discord.Embed(title="Ban", description=f"{user.mention} a fost banat/a. ", colour=discord.Colour.dark_red())
banemb.add_field(name="Motiv:", value=reason, inline=False)
await ctx.send(embed=banemb)
You can't get the list of roles that way.
To remove all the roles of the user you have to apply an edit to it and provide an empty list of roles.
It also changes the order of your code as you first have to remove and then re-assign a role to the user.
Have a look at the following code:
#bot.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member, *, reason=None):
await asyncio.sleep(1)
banned_role = discord.utils.get(user.guild.roles, name="[Banned]")
await user.edit(roles=[]) # Remove all roles
await user.add_roles(banned_role, reason=None) # Assign the new role
banemb = discord.Embed(title="Ban", description=f"{user.mention} a fost banat/a. ",
colour=discord.Colour.dark_red())
banemb.add_field(name="Motiv:", value=reason, inline=False)
await ctx.send(embed=banemb)
I have changed/removed a few things from the code. Of course you have to add them again, according to your wishes.
I Want to check if the message author has admin role to use this command or not
in my case it always gives me False and i know the code is not right
#client.command(pass_content=True)
async def new(ctx, member:discord.Member, nick):
role = author.get_role(name="Admin")
if role in author.roles:
await member.edit(nick=nick)
await ctx.send(f"Nickname changed")
You can use #commands.has_role(role ID or name)
For a role ID:
#bot.command()
#commands.has_role(123123)
async def test(ctx):
await ctx.send('It works!')
For a role name:
#bot.command()
#commands.has_role("role_name")
async def test(ctx):
await ctx.send('It works!')
I highly recommend using a role id because role names aren't unique.
You can use #commands.has_role("rolename") the in your #client.command
like this
#client.command(pass_content=True)
#commands.has_role("mod")
async def name(parameters):
#your code
Here is the code I am working with:
import discord
import random
import asyncio
from discord.ext import commands
from discord.ext.commands import bot
from discord.utils import get
def IsTeam(role):
Value = False
AllTeams = [] # Add all teams in here.
if role in AllTeams:
Value = True
return Value
#bot.command()
async def sign(ctx, member: discord.Member=None, *, arg=None):
if not member:
await ctx.send("Please provide a username.")
elif arg == None:
await ctx.send("Please provide a team.")
else:
try:
Role = discord.utils.get(ctx.guild.roles, name=arg)
if Role == None:
await ctx.send("This is an invalid role.")
else:
pass
except:
await ctx.send("Unexpected Error Occured.")
return
if Role >= ctx.author.top_role:
await ctx.send("You cannot add a role that is higher then your current role.")
else:
if IsTeam(Role):
if Role in ctx.author.roles:
await member.add_roles(Role)
await ctx.send(f'{member.mention} has been signed to `{role}`')
I wanted to know if I can make the
#bot.command()
async def sign(ctx, member: discord.Member=None, *, arg=None):
command into an event on_message such as like:
#bot.event()
async def on_message(ctx, member: discord.Member=None, *, arg=None):
while doing the same things the command does.
sorry if I am not explaining this clearly.
As I understand, you're trying to give a role to a user when someone mentions him/her with a specific prefix. In this case, your prefix is a emoji+ sign. You can use the on_message event for this. But in the on_message event, you can only use the message parameter. And also, you need to get the emoji id by typing \:TeamEmoji: in your discord chat. It will return something like <:TeamEmoji:123456789>. So you can basically do:
#bot.event
async def on_message(message):
if message.content.startswith('<:TeamEmoji:1234567890> sign'):
role = discord.utils.get(message.guild.roles, name='role name')
member = message.mentions[0]
await member.add_roles(role)
await message.channel.send('Successfully signed to <:TEAM:123456789>')
I was trying to make a bot that is able to add a role to a user with a user mention. I also want to pass the role in the command.
So the syntax should be:
!addrole ROLENAME #user
I tried this:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get
def read_token():
with open("token.txt", "r") as f:
lines = f.readlines()
return lines[0].strip()
token = read_token()
bot = commands.Bot(command_prefix='!')
#bot.command(pass_context=True)
async def addrole(ctx, arg1, member: discord.Member):
role = get(member.guild.roles, name={arg1})
await member.add_roles(role)
bot.run(token)
but it doesn't work. I receive the error:
AttributeError: 'NoneType' object has no attribute 'id'
You're already using a converter for member, use one for role too:
from discord import Member, Role
#bot.command()
async def addrole(ctx, member: Member, *, role: Role):
await member.add_roles(role)
The , *, allows you to supply role names that have multiple words.