I made a discord jail command and it doesn't work the bot is online but it doesn't work for some reason "Other commands work"
@client.command()
@commands.has_permissions(administrator=True)
async def jail(ctx, member: discord.Member, *, reason=None):
color = 0xFFB6C1
jailed_role = discord.utils.get(ctx.guild.roles, name="jailed")
if not jailed_role:
embed = discord.Embed(title="Failed!", description="Jail role not found, creating one.", color=color)
await ctx.send(embed=embed)
try:
jailed_role = await ctx.guild.create_role(name="jailed")
embed = discord.Embed(title="Jail role created", color=color)
await ctx.send(embed=embed)
except discord.Forbidden:
embed = discord.Embed(title="Failed!", description="Bot does not have permission to create the 'jailed' role.", color=color)
await ctx.send(embed=embed)
return
for channel in ctx.guild.channels:
await jailed_role.set_permissions(channel, view_channel=False)
if jailed_role not in member.roles:
for role in member.roles:
if role != jailed_role:
try:
await member.remove_roles(role)
except discord.Forbidden:
embed = discord.Embed(title="Failed!", description=f"Bot does not have permission to remove the role {role.name} from {member.mention}.", color=color)
await ctx.send(embed=embed)
return
try:
await member.add_roles(jailed_role, reason=reason)
embed = discord.Embed(title="Jailed!", description=f"{member.mention} has been jailed!", color=color)
embed.add_field(name="Reason:", value=reason)
await ctx.send(embed=embed)
except discord.Forbidden:
embed = discord.Embed(title="Failed!", description=f"Bot does not have permission to assign the 'jailed' role to {member.mention}.", color=color)
await ctx.send(embed=embed)
else:
embed = discord.Embed(title="Failed!", description=f"{member.mention} is already jailed.", color=color)
await ctx.send(embed=embed)
I tried alot of stuff but nothing seems to work, it would be great if someone would be able to fix it.
I think this is what you are looking for.. only issue I see with your code is that the jailed permissions are set everytime you give the role to someone. Theoretically you only need to do it once - when you create the role.
Also note that I've added an optimization to avoid the removing of the "@everyone" role since that role can not be removed and will cause an error.
@bot.command(pass_context = True)
@commands.has_permissions(administrator=True)
async def jail(ctx, member: discord.Member, *, reason=None):
color = 0xFFB6C1
jailed_role = discord.utils.get(ctx.guild.roles, name="jailed")
if not jailed_role:
embed = discord.Embed(title="Failed!", description="Jail role not found, creating one.", color=color)
await ctx.send(embed=embed)
try:
jailed_role = await ctx.guild.create_role(name="jailed")
embed = discord.Embed(title="Jail role created", color=color)
await ctx.send(embed=embed)
except discord.Forbidden:
embed = discord.Embed(title="Failed!", description="Bot does not have permission to create the 'jailed' role.", color=color)
await ctx.send(embed=embed)
return
overwrite = discord.PermissionOverwrite()
overwrite.update(read_message_history = False, read_messages = False)
for channel in ctx.guild.channels:
await channel.set_permissions(jailed_role, overwrite=overwrite) # set the permissions for each channel
if jailed_role not in member.roles:
for role in member.roles:
if role != jailed_role:
try:
if role.name == "@everyone": # will skipp @everyone since this role cant be removed
continue
await member.remove_roles(role)
except discord.Forbidden:
embed = discord.Embed(title="Failed!", description=f"Bot does not have permission to remove the role {role.name} from {member.mention}.", color=color)
await ctx.send(embed=embed)
return
try:
await member.add_roles(jailed_role, reason=reason)
embed = discord.Embed(title="Jailed!", description=f"{member.mention} has been jailed!", color=color)
embed.add_field(name="Reason:", value=reason)
await ctx.send(embed=embed)
except discord.Forbidden:
embed = discord.Embed(title="Failed!", description=f"Bot does not have permission to assign the 'jailed' role to {member.mention}.", color=color)
await ctx.send(embed=embed)
else:
embed = discord.Embed(title="Failed!", description=f"{member.mention} is already jailed.", color=color)
await ctx.send(embed=embed)