当静音用户离开服务器并重新加入时,如何保持静音角色?

2024-09-29 02:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用discord.py机器人,发现了一个问题。当静音用户离开服务器并重新加入时,该用户的静音角色将消失

这是我的静音/取消静音命令:

@bot.command()
@commands.has_permissions(manage_messages=True)
async def mute(ctx, member:discord.Member, *, reason=None):
      guild = ctx.guild
      mutedRole = discord.utils.get(guild.roles, name="Muted")    
      await member.add_roles(mutedRole, reason=reason)

      embed=discord.Embed(title="Muted.")
      embed.add_field(name="Muted.", value=(f"Muted {member.mention} for reason {reason}"))                     
      await ctx.send(embed=embed)
      await member.send(f"You were muted in the server {guild.name} for {reason}")
      list_of_muted_members.append(member)
      print(list_of_muted_members)

@bot.command(description="Unmutes a specified user.")
@commands.has_permissions(manage_messages=True)
async def unmute(ctx, member: discord.Member, reason=None):
      mutedRole = discord.utils.get(ctx.guild.roles, name="Muted")

      embed=discord.Embed(title="You have been unmuted.")
      embed.add_field(name="Unmuted.", value=(f"Unmuted {member.mention} Reason: {reason}"))
      await ctx.send(embed=embed)
      await member.remove_roles(mutedRole)
      await member.send(f"You were unmuted in the server {ctx.guild.name}")

Tags: nameyousendadd静音embedawaitmember
3条回答

一种方法是将当前处于静音状态的用户存储在一个.json文件中,然后当用户重新加入服务器时,您可以引用该文件

{
   "USERID": true
}

然后你可以:

with open('JSONFILE.json', 'r') as f:
  jsonfile = json.load(f)
  try:
     status = jsonfile[str(user.id)]
     if status:
        # add role to member
  except:
     # user wasn't found in json
     return

您可以尝试创建一个简单的数据库来保存静音用户的用户ID,然后在用户重新加入组时查询该数据库

只需列出一个包含所有静音成员的列表,每次可能的新成员加入都会通过该列表,如果该成员在该列表中,只需再次添加静音角色

相关问题 更多 >