I'm pretty new to python and discord bots in general. I am trying to make it so when a user runs !addrole (role) they will get that role. I need it to work for multiple users and multiple roles. It would be nice if users could also give other users that role.
from discord.ext import commands
import os
import random
import discord
from discord.utils import get
my_secret = os.environ['TOKEN']
GUILD = os.getenv('DISCORD_GUILD')
bot = commands.Bot(command_prefix='!') #prefix
#bot.command(pass_context=True)
#commands.has_role("admin")
async def addrole(ctx, *, rolewanted):
member = ctx.message.author
role = rolewanted
await bot.add_roles(member, role)
bot.run(my_secret)
Make sure to read the documentation, you're code is pretty outdated. You can type hint member and role to discord objects and use the add_roles method to do what you need
#bot.command()
#commands.has_role('admin')
async def addrole(ctx, member: discord.Member, role: discord.Role):
await member.add_roles(role)
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')
Code:
import os
import discord
import random
from discord.ext import commands
GUILD = os.getenv('DISCORD_GUILD')
client2 = commands.Bot(command_prefix = '')
#client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} has connected to '
f'{guild.name}'
)
#client.event
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
bot = commands.Bot(command_prefix='$')
#client2.command(pass_context = True)
async def mute(ctx, member: discord.Member):
if ctx.message.author.server_permissions.administrator or ctx.message.author.id == '194151340090327041':
role = discord.utils.get(member.server.roles, name='Muted')
await bot.add_roles(member, role)
embed=discord.Embed(title="User Muted!", description="**{0}** was muted by **{1}**!".format(member, ctx.message.author), color=0xff00f6)
await bot.say(embed=embed)
else:
embed=discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff00f6)
await bot.say(embed=embed)
client.run(os.getenv('TOKEN'))
I am trying to create a bot that mutes people. This is my 1st week in learning the discord API. I copied the mute function from a website so I didn't code everything myself. I'm having some trouble with the command. The error is:
NameError: name 'commands' is not defined
But I have seen other people do this and not get an error so I have no idea what the problem is.
Thanks a lot!
As mentioned before, you are going around with definitions that don't exist/that you shouldn't use.
First: Decide between client or bot and use the original handling, not somehow client2 etc.
Second: To use the commands, import from discord.ext import commands.
Third: Since you are now using client the commands have to be adapted. Also you don't use client.say or bot.say anymore, but ctx.send.
Fourth: You can't use multiple on_ready events, combine them or just use one.
Fifth: Please have a look at the mute command as yours contained many errors and requested things in the wrong way. You can take a look at other StackOverflow questions and answers as just copy and pasting another answer is not really useful...
Have a look at the full/final code:
from discord.utils import get # New import
import discord
from discord.ext import commands # New import
client = commands.Bot(command_prefix = 'YourPrefixHere') # Changes
TOKEN = "YourTokenHere" # Changes
#client.event
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
#client.command()
async def mute(ctx, member: discord.Member):
if ctx.message.author.guild_permissions.administrator or ctx.message.author.id == 'Your_ID': # Changes
role = discord.utils.get(ctx.guild.roles, name='Muted') # Changes
await member.add_roles(role)
embed = discord.Embed(title="User Muted!",
description="**{}** was muted by **{}**!".format(member, ctx.message.author),
color=0xff00f6)
await ctx.send(embed=embed)
else:
embed=discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff00f6)
await ctx.send(embed=embed)
client.run(TOKEN) # Changes
I am trying to make a discord bot that finds users with a specific role so it can do something with this group of users. I have written code to list all of the members in a server but I don't think this will help.
import discord
#some code
client = discord.Client()
#some code
client.run("my token")
You can use the following, use it like this $get_members ROLENAME
ref for commands
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
#bot.event
async def on_ready():
print("Logged in as")
print(bot.user.name)
print("------")
#bot.command()
async def get_members(ctx,role_name):
role = discord.utils.find(
lambda r: r.name == role_name, ctx.guild.roles)
for user in ctx.guild.members:
if role in user.roles:
await ctx.send(f"{user.mention} has the role {role.mention}")
bot.run("TOKEN")
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.
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')