使用tempmute命令时BOT取消成员静音 [英] Bot unmuting member when use tempmute command

查看:38
本文介绍了使用tempmute命令时BOT取消成员静音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了tempmute命令、mute命令和unmute命令。我的问题是,当我用mute命令将一个成员静音,并试图让他静音时,机器人正在取消对该成员的静音。 以下是我的完整代码:

@bot.command()
async def tempmute(ctx, member:discord.Member, czas:int, *, reason = "Brak powodu"):
  if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
    muted = discord.utils.get(ctx.guild.roles, name="Muted")
    if muted in member.roles:
      await ctx.send("Nie możesz wyciszyć kogoś, kto już jest wyciszony!")
    else:
      await member.add_roles(muted)
      await ctx.send(f"Wyciszono użytkownika {member.mention} na {czas} minut(-ę/-y) z powodu: {reason}")
      await asyncio.sleep(czas*60)
    if muted in member.roles:
      await member.remove_roles(muted)
      await ctx.send(f"Odciszono użytkownika {member.mention}, ponieważ jego czas wyciszenia ({czas} minut(-a/-y)) minął")
    else:
      pass
  else:
      await ctx.send("Nie posiadasz permisji!")

@tempmute.error
async def tempmute_error(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
      await ctx.send("Poprawne użycie (przykład):
,tempmute <@839076078651441204> 1 (czas w minutach) powód")
    else:
      await ctx.send("Nie posiadasz permisji!")

@bot.command()
async def unmute(ctx, *, member:discord.Member):
  if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
    muted = discord.utils.get(ctx.guild.roles, name="Muted")
    if muted in member.roles:
      await member.remove_roles(muted)
      await ctx.send(f"Odciszono {member.mention}")
    else:
      await ctx.send(f"{member.mention} nie jest wyciszony/a!")

@unmute.error
async def unmute_error(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
      await ctx.send("Poprawne użycie (przykład):
,unmute <@839076078651441204>")
    else:
      await ctx.send("Nie posiadasz permisji!")

@bot.command()
async def mute(ctx, member:discord.Member, *, reason="Brak powodu"):
  if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
    muted = discord.utils.get(ctx.guild.roles, name="Muted")
    if muted in member.roles:
      await ctx.send(f"{member.mention} jest już wyciszony/a!")
    else:
      await member.add_roles(muted)
      await ctx.send(f"Wyciszono {member.mention} z powodu: {reason}")

@mute.error
async def mute_error(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
      await ctx.send("Poprawne użycie (przykład):
,mute <@839076078651441204> powód")
    else:
      await ctx.send("Nie posiadasz permisji!")

Works just like in this picture,有人能帮我吗?

推荐答案

我真的认为您尝试编写命令的方式不可行。

我将逐一解释为该命令编写代码的过程。

TEMPMUTE命令。

第一步:定义功能和权限

我们将使用一些使用权限限制来定义该函数。具有权限的人员指定的权限。

在使用此功能之前,您的代码中需要包含以下内容:

from discord.ext import commands

(如果有则忽略)

操作方法如下:

@bot.command()
@commands.has_permissions(manage_roles=True)
async def tempmute(ctx, member: discord.Member, time, *, reason=None):

您可以根据需要添加更多权限,并使用True或False来允许或拒绝。

第二步:创建角色查找条件。如果该角色不存在,则创建一个禁用角色,如果存在,则使用它。

因此,我正在为角色是否存在创建一个条件。它将检查该角色是否存在。如果它存在,我们将简单地使用它,但如果它不存在,我们将创建一个具有特定权限的文件。

操作方法如下:

if discord.utils.get(ctx.guild.roles, name="Muted"):
    mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
else:
    perms = discord.Permissions(send_messages=False, add_reactions=False, connect=False, speak=False)
    await bot.create_role(name="Muted", permissions=perms)
    mute_role = discord.utils.get(ctx.guild.roles, name="Muted") 

第3步:检查某人是否静音。

现在,我正在为成员是否静音创建一个条件。我们将仔细检查该成员的角色并对其进行检查。

操作方法如下:

if mute_roles in member.roles:
    await ctx.channel.send(f"{member.mention} is already muted!")
else:
    # code here (more steps will explain how)

第4步:添加条件并将成员设置为静音。

现在,我们将为此命令添加限制权限。所有人都不能静音的人,以及所有人都可以成为静音的人。

首先,我们添加第一个条件,即管理员不能静音。 以下是操作方法:

if member.guild_permissions.administrator:    
    isadminembed=discord.Embed(title="Tempmute", description=f"Hi {ctx.author.mention}, you can't mute {member.mention} as they are a Server Administrator.", color=discord.Colour.red())
    isadminembed.set_author(name="Bot")
    await ctx.channel.send(embed=isadminembed)
现在我们将添加else条件,即除管理员外的所有其他成员都可以静音。 以下是操作方法:

else:
    time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800, "M": 2419200, "y": 29030400}
    mute_time = int(time[:-1]) * time_conversion[time[-1]]

    await member.add_roles(mute_role)
    mutedembed=discord.Embed(title="Tempmute", description=f"The member, {member.mention} has been muted by the moderator {ctx.author.mention}. 
 
Time: {mute_time} seconds 
Reason: {reason}", color=discord.Colour.random())
    mutedembed.set_author(name="Bot")
    await ctx.channel.send(embed=mutedembed)

    await asyncio.sleep(mute_time)
    await member.remove_roles(mute_role)
    await ctx.channel.send(f"{member.mention} has been unmuted!")

我在这里添加了时间转换。时间输入必须为:

1s1秒、1m1分钟等,years也可以使用。


临时静音命令已编译

这是作为命令一起编译的所有代码。

@bot.command()
@commands.has_permissions(manage_roles=True)
async def tempmute(ctx, member: discord.Member, time, *, reason=None):
    if discord.utils.get(ctx.guild.roles, name="Muted"):
        mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
    else:
        perms = discord.Permissions(send_messages=False, add_reactions=False, connect=False, speak=False)
        await bot.create_role(name="Muted", permissions=perms)
        mute_role = discord.utils.get(ctx.guild.roles, name="Muted")

    if mute_roles in member.roles:
        await ctx.channel.send(f"{member.mention} is already muted!")
    
    else:
        if member.guild_permissions.administrator:    
            isadminembed=discord.Embed(title="Tempmute", description=f"Hi {ctx.author.mention}, you can't mute {member.mention} as they are a Server Administrator.", color=discord.Colour.red())
            isadminembed.set_author(name="Bot")
            await ctx.channel.send(embed=isadminembed)
        
        else:
            time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800, "M": 2419200, "y": 29030400}
            mute_time = int(time[:-1]) * time_conversion[time[-1]]

            await member.add_roles(mute_role)
            mutedembed=discord.Embed(title="Tempmute", description=f"The member, {member.mention} has been muted by the moderator {ctx.author.mention}. 
 
Time: {mute_time} seconds 
Reason: {reason}", color=discord.Colour.random())
            mutedembed.set_author(name="Bot")
            await ctx.channel.send(embed=mutedembed)

            await asyncio.sleep(mute_time)
            await member.remove_roles(mute_role)
            await ctx.channel.send(f"{member.mention} has been unmuted!")

TEMPMUTE命令错误处理

第1步:将函数定义为事件。

我们将脚本中的函数定义为tempmute命令的错误,并将其声明为错误。

操作方法如下:

@bot.error
async def tempmute_error(ctx, error):

第2步:添加错误实例。

我们现在将为错误添加一个条件。我们的错误将是:MissingRequiredArgument错误,因为该命令可能缺少必要的参数。

因此isinstance将检查错误,然后执行操作。

操作方法如下:

if isinstance(error, discord.ext.commands.MissingRequiredArgument):
    tempmuteerrorembed=discord.Embed(title=f"Missing Argument! {error}", description=f"Hello {ctx.author.mention}! You have not entered the needed argument. 
 Either you forgot to **mention the member** or you forgot to **enter the time of the mute** you want. 
 
 Please check this again and add the necessary argument in the command. 
 
This is the syntax: 
```!tempmute <mention member> <time: 1s, 2h, 4y etc..> <reason (optional)>```", color=discord.Colour.red())
    tempmuteerrorembed.set_author(name="Bot")
    await ctx.send(embed=tempmuteerrorembed)

这将适用于MissingRequiredArguement,并显示其中的语法错误,以便命令的正确用法。


TEMPMUTE命令错误处理编译

以下是TEMPMUTE命令错误处理的编译代码。

@bot.error
async def tempmute_error(ctx, error):
    if isinstance(error, discord.ext.commands.MissingRequiredArgument):
        tempmuteerrorembed=discord.Embed(title=f"Missing Argument! {error}", description=f"Hello {ctx.author.mention}! You have not entered the needed argument. 
 Either you forgot to **mention the member** or you forgot to **enter the time of the mute** you want. 
 
 Please check this again and add the necessary argument in the command. 
 
This is the syntax: 
```!tempmute <mention member> <time: 1s, 2h, 4y etc..> <reason (optional)>```", color=discord.Colour.red())
        tempmuteerrorembed.set_author(name="Bot")
        await ctx.send(embed=tempmuteerrorembed)

命令中的文本不是您编写的内容,因为这是我为我的bot创建的命令。您可以自己相应地更改它们。

这将为您工作,我希望您能理解它。请在评论中询问任何问题。:)

谢谢!:d

这篇关于使用tempmute命令时BOT取消成员静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆