I have this code which if someone has a role "Server Developer" it is suppose to run the command which is to give someone the role "Jail" for the determent amount n the command but it doesn't work and gives no errors.
import discord
from discord.ext import commands
import ctx
import re
import time
from time import sleep
import asyncio
PREFIX = "$"
bot = commands.Bot(command_prefix=PREFIX, description="Hi")
@commands.has_role("Server Developer")
@bot.command()
async def court(ctx, user_mentioned: discord.Member, time: int):
user_id = message.mentions[0].id
user = message.mentions[0]
role = discord.utils.get(message.guild.roles, name="Jail")
await user_mentioned.add_roles(role)
await ctx.send(
f"sending <@{user_id}> to court!"
)
await asyncio.sleep(time)
await user_mentioned.remove_roles(role)
bot.run(TOKNE_GOES_HERE)
You forgot to add @bot.command()
on top of your command. Add it between @commands.has_role("Server Developer")
and async def court(ctx, user_mentioned: discord.Member, time: int):
Do it like this:
import discord
from discord.ext import commands
import ctx
import re
import time
from time import sleep
import asyncio
PREFIX = "$"
bot = commands.Bot(command_prefix=PREFIX, description="Hi")
@commands.has_role("Server Developer")
@bot.command()
async def court(ctx, user_mentioned: discord.Member, time: int):
user_id = ctx.message.mentions[0].id
user = ctx.message.mentions[0]
role = discord.utils.get(ctx.message.guild.roles, name="Jail")
await user_mentioned.add_roles(role)
await ctx.send(
f"sending <@{user_id}> to court!"
)
await asyncio.sleep(time)
await user_mentioned.remove_roles(role)
bot.run(TOKNE_GOES_HERE)
Using any check decorators like has_role
or is_owner
, doesn't make the function a command, so you would still have to add @commands.command()
or @bot.command()
:
@bot.command()
@commands.has_role("Server Developer")
async def court(ctx, user_mentioned: discord.Member, time: int):
user_id = message.mentions[0].id
user = message.mentions[0]
role = discord.utils.get(message.guild.roles, name="Jail")
await user_mentioned.add_roles(role)
await ctx.send(
f"sending <@{user_id}> to court!"
)
await asyncio.sleep(time)
await user_mentioned.remove_roles(role)
bot.run(TOKNE_GOES_HERE)
you could try adding a check inside the command, something like this:
if not discord.utils.get(ctx.guild.roles, name="Server Developer") in ctx.user.roles:
return
else:
# do something
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.