I want to allow some people that i have added to a list to be able to use certain commands this is what i got so far please help me
#client.command()
async def premium(ctx):
premium = set()
premium.add(user_id)
if user_id in premium:
embed=discord.Embed(title="Just a Test Command", description="Trying to make a premium command", color=0xff0000)
embed.set_thumbnail(url="https://images4.alphacoders.com/980/thumb-350-980708.png")
embed.set_footer(text="Made by Cortax#1667")
await ctx.channel.send(embed=embed)
else:
await ctx.channel.send("You are not a premium user")```
It don't work because u forget about user_id
Let's use:
user_id = ctx.user.id
Or exchange to:
premium.add(ctx.user.id)
Related
I've already seen the variant where you allow a certain role to use a command.
However, I'm trying to achieve the exact opposite: How to disallow a certain role from using a command.
I have searched around stack overflow and found no answer, nor did I found an answer on the official discord.py documentation.
Any sort of help is appreciated.
author.roles returns a list of discord.Role so just check if the role you specify is contained in that list, and if so, exit the command early.
Using Role Id (Preferred)
#bot.command()
async def command_without_specific_role(ctx):
if role_id in [role.id for role in ctx.author.roles]:
return
...
Using Role Name
#bot.command()
async def command_without_specific_role(ctx):
if role_name in [role.name for role in ctx.author.roles]:
return
...
Check inside command
#bot.command()
async def hello(ctx: commands.Context):
blacklisted_role = ctx.guild.get_role(ID)
if not any(role == blacklisted_role for role in ctx.author.roles):
await ctx.send("world!")
Own Decorator
A more elegant way is to create your own decorator. It's more like the opposite of has_any_role.
from discord.ext import commands
def has_not_any_role(*roles):
async def extended_check(ctx):
return not any(role.id in roles for role in ctx.author.roles)
return commands.check(extended_check)
#bot.command()
#has_not_any_role(492212595072434186)
async def hello(ctx: commands.Context):
await ctx.send("world!")
If extended_check returns True the message will be sent. If it return False it throws a discord.ext.commands.errors.CheckFailure error which then can be catched in on_command_error.
References:
Member.roles
commands.check
I'm writing a discord bot for my discord-server and the people on the server change their nicknames like their underwear. Can i get the user id from their changing nicknames?
I want to code a command like this:
!command #mention
I need the user id of the mention. Thanks in advance!
Hope this helps :)
#client.command()
async def id(ctx, member: discord.Member = None):
if not member:
await ctx.send('Please mention a user')
else:
await ctx.send(member.id)
I'm trying to get it so a specific role can react to the message to be able to ban the user, I have made the ability to be able to react to the message, and the author of the command can ban the user, but I want to make it so another role can do it as well
Here is what I have currently
def check(rctn, user):
return user.id == ctx.author.id and str(rctn) == '<:tick:837398197931868190>'
reaction, user = await bot.wait_for('reaction_add', check=check)
You can use #commands.has_permissions
For example you could make it:
#bot.command()
#commands.has_permissions(ban_members=True)
# rest of your code
This way only roles with the permissions to ban_members can use the command
I don't have much experience in discord.py, but I'm making a bot. Right now I added the welcoming message:
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(embed=emb)
but I want to mention the user who joined as this picture
May you help me doing this? Thanks!
Use member.mention
This returns a string which allows you to mention the member.
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.mention} for joining!")
await channel.send(embed=emb)
You should however keep in mind that because of caching, the mention will most likely look something like this <#!123456789>.
You need the code to look like this:
#client.event
async def on_member_join(member):
if member.guild.id !=Guild.id:
return
channel = client.get_channel(channel_id) # replace id with the welcome channel's id
await channel.send(f"{member.mention} has arrived!, check out our announcments channel for server and bot announcements!")
await member.send(f"Thank you for joining {member.guild.name}!")
This line right here is you only want people that join YOUR server to send a msg, if you don't have any server with your bot in it, will say the msg still
if member.guild.id !=Guild.id:
return
{member.mention} is what mentions the user if you are using {} in the sending part you need the f function
Next code will make the code call the {}
await channel.send(f"{member.mention}")
This calls the guild name:
{member.guild.name}
I am not sure about client.get_channel, but you sure can try this:
#client.event
async def on_member_join(member):
guild = client.get_guild(serverID)
channel = guild.get_channel(channelID)
emb = discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(member.mention, embed=emb)
I'm trying to make a kick command using Python and the discord.py library, though I cannot figure out how to allow people to only be able to kick members lower than their role, so Moderators wouldn't be able to kick Admins.
enter image description here
You can compare the Member object with a function called top_role
An example code would be:
#client.command()
#commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
if member.top_role >= ctx.author.top_role: # Check if the role is below the authors role
await ctx.send("You can only kick users with a lower role!")
return
else:
await member.kick(reason=reason)
await ctx.send(f"{member.id} got kicked with the reason: {reason}.")
Add all the moderators to a list and regular users to another list.
Like this;
string[] moderator = all moderators.
string[] regularUsers = all regular users.