how i can set kick command with a role use just that Moderator role will can use
my kick command :
#client.command(pass_context = True)
async def kick(ctx, userName: discord.User):
"""Kick A User from server"""
await client.kick(userName)
await client.say("__**Successfully User Has Been Kicked!**__")
You can use the commands.has_permissions decorator to ensure the caller has a specific permission.
#client.command(...)
#commands.has_permissions(kick_members=True)
async def kick(ctx, ...):
pass
Just a word of warning though, according to the function docstring, it checks for the user having any required permission instead of all.
It is also recommended to add the bot_has_permissions check too to make sure it can actually kick users too.
Related
I want to make i discord bot that can move a member to a specified channel without having to mention them.
import discord
from discord.ext import commands
token = '<token>'
bot = commands.Bot(command_prefix='#')
#bot.command()
async def m(ctx, member : discord.Member, channel : discord.VoiceChannel):
await member.move_to(channel)
#bot.event
async def on_ready():
print('Ready')
bot.run(token)
The command would be: #m #user General
This works great but i need the command to be easier to type by not having to mention a user and simply just moving the author of the message. How do I do that?
You can use ctx.author and make the member argument optional.
Now we just move around the member argument to be last (because a channel is required), and set the default value to None to make it optional.
#bot.command()
#commands.guild_only()
async def m(ctx, channel: discord.VoiceChannel, member: discord.Member = None):
member_to_move = member or ctx.author # if `member` is None, it will be the author instead.
await member_to_move.move_to(channel)
Edit: added #commands.guild_only decorator. This is to ensure that the command can only be used within a guild (and will raise an error if invoked, lets say, in DMs).
You can use ctx.author to get the author of the message:
#bot.command()
#commands.guild_only()
async def m(ctx, channel : discord.VoiceChannel):
await ctx.author.move_to(channel)
So, you can use the command like this: #m General.
Also I added #commands.guild_only() check to be sure that the command is invoked in the guild channel.
So, I wanna make a command foo which says bar when executed. I want the user to have either the fooBar role or the administrator permission in the guild using an if statement. I have tried this:
class FooBar:
#commands.command()
#commands.has_permission(administrator=True)
async def foo(self,ctx):
await ctx.send("bar")
But, thats only for the permission, how would I check for a role too. I tried using an error handler but I again need to see if the user has the role using an if statement. What I tried was:
class FooBar:
#commands.command()
#commands.has_permission(administrator=True)
async def foo(self,ctx):
await ctx.send("bar")
#foo.error()
async def fooErr(self,error,ctx):
if isinstance(error, commands.MissingPermission) and (code to check if the user has a role):
print("bar")
But, I get stuck at the point of checking if the user has a role again. Anything I can do for this or is it just not possible?
For anyone else having the same problem, thanks to Lukasz this answer worked:
class FooBar:
#commands.command()
#commands.has_permission(administrator=True)
#commands.has_role("fooBar")
async def foo(self,ctx):
await ctx.send("bar")
So, I want to write a function for all subcommands for my python bot but I'm not sure how they work in discord.py. For instance, I have this mute code here.
#bot.command()
async def mute(ctx,member : discord.Member, *, reason=None):
with open("role.json", "r") as f:
roles = json.load(f)
muted_role = discord.utils.get(ctx.guild.roles, id=roles[f"{ctx.guild.id}"]["role_id"])
embed = discord.Embed(color=discord.Color(0xFF0000), title=f"{member} has been muted.")
#sent to DM
embedC=discord.Embed(title=f"{member} has been muted.",color=discord.Color(0xff0000))
embedC.add_field(name=f"Member",value=f"{member}",inline=False)
embedC.add_field(name=f"Moderator",value=f"{ctx.author}",inline=False)
embedC.add_field(name=f"Reason",value=f"{reason}",inline=False)
embedC.set_footer(text=f"Mute was successfully invoked. Expiration is indefinite.")
await member.add_roles(muted_role)
await ctx.send(embed=embed)
await member.send(embed=embedC)
In the code above is my mute command. I know this is a bit irrelevant to my question but I want to make a custom subcommand for bot. You can remove built in command help by doing bot bot.remove_command("help") but how do I remove custom help subcommand? Now this leads to the second part of the question, which is also the main question. How do I write a function for custom subcommand so I won't have to write and repeat the same process over for every subcommand?
I tried doing def subcommand(command, error) where the command is the command needed for help and error is the error message I want to put. So in case my mute command fails, it will ask user to use my subcommand and my subcommand will provide them with information on how this command works. Thank you!
There's an built-in subcommand support:
#bot.group()
async def cmd(ctx):
if ctx.invoked_subcommand is None:
await ctx.send("Command can't be invoked without a subcommand")
#cmd.command()
async def subcommand1(ctx, *args):
#...
#cmd.command()
async def subcommand2(ctx, *args):
#...
To invoke:
{prefix}cmd subcommand1 arg1 arg2
Docs
I want to make a simple profile command. When there is no mention bot shows your profile, when there is mention, bot shows profile of mentioned user.
#!профиль - профиль
#client.command(passContent=True, aliases=['profile'])
#commands.has_role("🍿║Участники")
async def профиль(ctx, member):
colour=member.colour
if member==None:
профиль_сообщение=discord.Embed(
title=f'Профиль {member.name}',
colour=colour
)
await ctx.send(embed=профиль_сообщение)
else:
return
When I am using command WITHOUT mention I get this:
discord.ext.commands.errors.MissingRequiredArgument: member is a required argument that is missing.
If you want to show your profile when the member parameter's empty, you can give a value to it. For example you can do async def профиль(ctx, member: discord.Member=None):. That means, if user doesn't fill the member parameter, it'll be None. Then in the code, you can add:
#client.command(passContent=True, aliases=['profile'])
#commands.has_role("🍿║Участники")
async def профиль(ctx, member):
colour=member.colour
if member==None:
member = ctx.author
профиль_сообщение=discord.Embed(
title=f'Профиль {member.name}',
colour=colour
)
await ctx.send(embed=профиль_сообщение)
So if the member is None, then it'll be the ctx.author.
How do I make a bot in Discord.py that will assign roles present in a role.json file, while using the same command to both remove and add the same role. For example, ?role <rolename> will both add and remove a role, depending on if the user has the role assigned. I'm a bit confused on how to achieve this.
My current bot uses ?roleadd <rolename> ?roleremove <rolename>.
I'm not sure where your role.json file comes into play, but here's how I would implement such a command
#bot.command(name="role")
async def _role(ctx, role: discord.Role):
if role in ctx.author.roles:
await ctx.author.remove_roles(role)
else:
await ctx.author.add_roles(role)
This uses the Role converter to automatically resolve the role object from its name, id, or mention.
This code basically just checks that if the command raiser is the owner of the server or not and then assigns the specified role to him.
#bot.command()
#commands.is_owner()
async def role(ctx, role:discord.Role):
"""Add a role to someone"""
user = ctx.message.mentions[0]
await user.add_roles(role)
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
Let me break the code down:
#bot.command()
#commands.is_owner()
These are just plain function decorators. They are pretty much self-explanatory. But still let me tell.
#bot.command() just defines that it is a command.
#commands.is_owner() checks that the person who has raised that command is the owner.
async def role(ctx, role:discord.Role): > This line defines the function.
user = ctx.message.mentions[0] #I don't know about this line.
await user.add_roles(role) #This line adds the roles to the user.
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
#It just sends a kind of notification that the role has been assigned