I’m building a Discord bot in Python and need help with a user management feature. Instead of banning troublesome users, my server uses a special role called ‘jester’ that limits them to posting only in a designated channel called ‘theater’. I want to create a system that can track these restricted users even when they leave and rejoin the server.
My plan is to store user IDs in a JSON file and automatically assign the restriction role when those users are detected. However, I’m struggling to get the implementation working properly. Has anyone built something similar?
import json
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.command()
async def restrict_user(ctx, member: discord.Member):
# Load existing restricted users
with open('restricted.json', 'r') as file:
restricted_list = json.load(file)
# Add user to restriction list
restricted_list.append(member.id)
# Save updated list
with open('restricted.json', 'w') as file:
json.dump(restricted_list, file)
# Apply jester role
jester_role = discord.utils.get(ctx.guild.roles, name='jester')
await member.add_roles(jester_role)
The logic seems right but it’s not functioning as expected. What am I missing?
your json file might be formatted wrong - needs to be an empty array [], not an object. you’re also not checking for duplicates, so users can get added multiple times. use if member.id not in restricted_list: before appending to fix that. the role matching is case sensitive too, so ‘jester’ has to match exactly.
Your code looks solid, but you’re missing error handling and the member join event listener. The main issue is probably that your restricted.json file doesn’t exist initially, or you don’t have the on_member_join event to catch returning users.
I had a similar setup for moderating repeat offenders. You need to wrap your file operations in try-except blocks to handle missing files, and add an event listener that checks new members against your restriction list.
Also consider using a set instead of a list for the restricted IDs - prevents duplicates and makes lookups way faster. The other thing that got me was making sure the bot has proper permissions to assign roles and that the bot’s role sits higher in the hierarchy than the jester role. Discord’s role system can be really finicky about that.
Code looks fine - you’re probably dealing with file permissions or corrupted JSON. Hit this exact issue building a similar system last year. Usually it’s the JSON file getting locked by another process or going bad after a crash. What saved me was adding backups and atomic writes. Don’t overwrite directly - write to a temp file first, then rename it. Also check if your bot actually has manage roles permission and verify the jester role exists before assigning it. Throw in some debug logging to see what values you’re pulling from the JSON - that’ll show you the problem fast. One more thing: make sure your bot has proper file system permissions where the JSON lives. Some hosting platforms lock this down by default.