title: Add Muted Users in a database? When they rejoin they get the role again
body: <pre><code>@commands.command()
      @commands.has_permissions(manage_roles=True)
      async def mute(self, ctx, member:discord.Member, *, time: TimeConverter = None):
          role = discord.utils.get(ctx.guild.roles, name=&quot;Muted by ez&quot;)
          if not role:
              role = await ctx.guild.create_role(name=&quot;Muted by ez&quot;)
  
              for channel in ctx.guild.channels:
                  await channel.set_permissions(role, speak=False, send_messages=False, read_message_history=True,
                                                read_messages=False)
              await member.add_roles(role)
              await asyncio.sleep(time)
              await member.remove_roles(role)
              await ctx.send(f&quot;Muted {member.mention} for {time}s&quot;)
          else:
              await member.add_roles(role)
              await ctx.send(f&quot;Muted {member.mention} for {time}s&quot;)
              await asyncio.sleep(time)
              await member.remove_roles(role)
              await ctx.send(f'Unmuted {member.mention}')
  
  
      @commands.command()
      @commands.has_permissions(manage_roles=True)
      async def unmute(self, ctx, member: discord.Member):
          role = discord.utils.get(ctx.guild.roles, name=&quot;Muted by ez&quot;)
          await member.remove_roles(role)
          await ctx.send(f'Unmuted {member.mention}')
  </code></pre>
  <p>this my code so far its perfectly working, but i am so long in tht and ask myself how to add users in json database so when they rejoin they still have the role.</p>
  <p><strong>Update better code</strong>
  i got now all ur chanches but not these with more data:
  i want to add guild id, mute duration and author id</p>
  <p>so i tried to store tht all but i got many errors so i hope u can help me again
  this my code so far</p>
  <pre><code>import re
  
  import asyncio
  import discord
  from discord.ext import commands
  import cogs._json
  time_regex = re.compile(&quot;(?:(\d{1,5})(h|s|m|d))+?&quot;)
  time_dict = {&quot;h&quot;: 3600, &quot;s&quot;: 1, &quot;m&quot;: 60, &quot;d&quot;: 86400}
  
  
  
  class TimeConverter(commands.Converter):
      async def convert(self, ctx, argument):
          args = argument.lower()
          matches = re.findall(time_regex, args)
          time = 0
          for key, value in matches:
              try:
                  time += time_dict[value] * float(key)
              except KeyError:
                  raise commands.BadArgument(
                      f&quot;{value} is an invalid time key! h|m|s|d are valid arguments&quot;
                  )
              except ValueError:
                  raise commands.BadArgument(f&quot;{key} is not a number!&quot;)
          return round(time)
  
  
  class Moderation(commands.Cog):
      def __init__(self, bot):
          self.bot = bot
  
      @commands.command(
          name='mute',
          description=&quot;Mutes a given user for x time!&quot;,
          ussage='&lt;user&gt; [time]'
      )
      @commands.has_permissions(manage_roles=True)
      async def mute(self, ctx, member: discord.Member, *, time: TimeConverter= None):
          role = discord.utils.get(ctx.guild.roles, name=&quot;Muted by ez&quot;)
          if not role:
              role = await ctx.guild.create_role(name=&quot;Muted by ez&quot;, colour=0x171717)
  
              for channel in ctx.guild.channels:
                  await channel.set_permissions(role, speak=False, send_messages=False, read_message_history=True,
                                                read_messages=False, add_reactions=False)
              pass
  
          try:
  
                  data = cogs._json.read_json(&quot;muted_users&quot;)
                  if member.id in data['muted_users']:
                      remsg = await ctx.send(&quot;reloading the mute&quot;)
                      await member.remove_roles(role)
                      data = cogs._json.read_json(&quot;muted_users&quot;)
                      data[&quot;muted_users&quot;].remove(member.id)
                      cogs._json.write_json(data, &quot;muted_users&quot;)
                      await asyncio.sleep(2)
                      await remsg.edit(content=f&quot;Unmuted `{member}`&quot;)
                      data = cogs._json.read_json(&quot;muted_users&quot;)
                      data[&quot;muted_users&quot;].append(member.id)
                      cogs._json.write_json(data, &quot;muted_users&quot;)
                      await member.add_roles(role)
                      if not time:
                          await asyncio.sleep(2)
                          await remsg.edit(content=f&quot;Muted `{member}`&quot;)
                      else:
                          await asyncio.sleep(2)
                          await remsg.edit(content=f&quot;Muted `{member}` for `{time}s`&quot;)
  
                          await asyncio.sleep(time)
                          await member.remove_roles(role)
                          data = cogs._json.read_json(&quot;muted_users&quot;)
                          data[&quot;muted_users&quot;].remove(member.id)
                          cogs._json.write_json(data, &quot;muted_users&quot;)
                          await ctx.send(content=f&quot;Unmuted `{member}`&quot;)
  
  
                      return
          except KeyError:
              pass
          data = cogs._json.read_json(&quot;muted_users&quot;)
          data[&quot;muted_users&quot;].append(member.id)
          cogs._json.write_json(data, &quot;muted_users&quot;)
          await member.add_roles(role)
  
          if not time:
              await ctx.send(f&quot;Muted `{member}`&quot;)
          else:
              await ctx.send(f&quot;Muted `{member}`for `{time}s`&quot;)
              await asyncio.sleep(time)
              print(time)
              if role in member.roles:
                  await member.remove_roles(role)
                  data = cogs._json.read_json(&quot;muted_users&quot;)
                  data[&quot;muted_users&quot;].remove(member.id)
                  cogs._json.write_json(data, &quot;muted_users&quot;)
                  await ctx.send(f&quot;Unmuted `{member}`&quot;)
              else:
                  data = cogs._json.read_json(&quot;muted_users&quot;)
                  data[&quot;muted_users&quot;].remove(member.id)
                  cogs._json.write_json(data, &quot;muted_users&quot;)
  
      @commands.command(
          name='unmute',
          description=&quot;Unmuted a member!&quot;,
          usage='&lt;user&gt;'
      )
      @commands.has_permissions(manage_roles=True)
      async def unmute(self, ctx, member: discord.Member):
          role = discord.utils.get(ctx.guild.roles, name=&quot;Muted by ez&quot;)
          if not role:
              role = await ctx.guild.create_role(name=&quot;Muted by ez&quot;, color=0x171717)
  
              for channel in ctx.guild.channels:
                  await channel.set_permissions(role, speak=False, send_messages=False, read_message_history=True,
                                                read_messages=False, add_reactions=False)
              return
  
          if role not in member.roles:
              await ctx.send(&quot;This member is not muted.&quot;)
              data = cogs._json.read_json(&quot;muted_users&quot;)
              data[&quot;muted_users&quot;].remove(member.id)
              cogs._json.write_json(data, &quot;muted_users&quot;)
  
  
  
  
  
          await member.remove_roles(role)
          data = cogs._json.read_json(&quot;muted_users&quot;)
          data[&quot;muted_users&quot;].remove(member.id)
          cogs._json.write_json(data, &quot;muted_users&quot;)
          await ctx.send(f&quot;Unmuted `{member}`&quot;)
  def setup(bot):
      bot.add_cog(Moderation(bot))
  </code></pre>
  <p>that is the cog</p>
  <pre><code>import json
  from pathlib import Path
  
  def get_path():
      &quot;&quot;&quot;
      A function to get the current path to bot.py
  
      Returns:
       - cwd (string) : Path to bot.py directory
      &quot;&quot;&quot;
      cwd = Path(__file__).parents[1]
      cwd = str(cwd)
      return cwd
  
  def read_json(filename):
      &quot;&quot;&quot;
      A function to read a json file and return the data.
  
      Params:
       - filename (string) : The name of the file to open
  
      Returns:
       - data (dict) : A dict of the data in the file
      &quot;&quot;&quot;
      cwd = get_path()
      with open(cwd+'/bot_config/'+filename+'.json', 'r') as file:
          data = json.load(file)
      return data
  
  def write_json(data, filename):
      &quot;&quot;&quot;
      A function used to write data to a json file
  
      Params:
       - data (dict) : The data to write to the file
       - filename (string) : The name of the file to write to
      &quot;&quot;&quot;
      cwd = get_path()
      with open(cwd+'/bot_config/'+filename+'.json', 'w') as file:
          json.dump(data, file, indent=4)
  </code></pre>
  <p>my &quot;converting file&quot;</p>
  <pre><code>{
      &quot;muted_users&quot;: []
  }
  </code></pre>
  <p>my json store file</p>
  <pre><code>@commands.Cog.listener()
      async def on_member_join(self, member):
          print(member)
          await member.send(&quot;hello&quot;)
          role = discord.utils.get(member.guild.roles, name=&quot;Muted by ez&quot;)
          data = cogs._json.read_json(&quot;muted_users&quot;)
          if member.id in data['muted_users']:
              await member.add_roles(role)
  </code></pre>
  <p>and my on_member_join module.
  This code is fully working but i want to make it better, bcs like in the on member join module is could be tht one member so the same kid is muted on one server and not on the other but gets the mute on both. thats why i need the guild id.
  and how can i remove all tht data when i unmute the member again?
  i didnt understand tht so can u help me again?</p>