Discord.py(重写)静音命令总是返回相同的响应

Posted

技术标签:

【中文标题】Discord.py(重写)静音命令总是返回相同的响应【英文标题】:Discord.py (rewrite) mute command always returns the same response 【发布时间】:2021-02-13 12:33:55 【问题描述】:

我目前正在尝试构建一个 discord.py 静音命令,该命令发送一个嵌入询问用户是否确定要静音,然后在添加反应时执行静音并编辑嵌入。我在尝试这样做时遇到了一些错误。

@bot.command()
@commands.has_permissions(manage_guild=True)
async def mute(ctx, message, member: discord.Member = None):
  
  guild = ctx.guild
  user = member

  if member == None:
    await ctx.send(f'**ctx.message.author,** please mention somebody to mute.')
    return 

  if member == ctx.message.author:
    await ctx.send(f'**ctx.message.author,** you cannot mute yourself, silly.')
    return 
  
  for role in guild.roles:
    if role.name == "Muted":
      if role in user.roles:
                await ctx.send("**** is already muted.".format(user))
                return

  embedcheck=discord.Embed(title="Mute", colour=0xFFD166, description=f'Are you sure you want to mute **user?**')
  
  embeddone=discord.Embed(title="Muted", colour=0x06D6A0,description=f'The mute has been done. **user** cannot talk in any channels anymore.')

  embedfail=discord.Embed(title="Not Muted",colour=0xEF476F,description=f'The mute did not carry out as I did not receive a reaction in time.')

  msg = await ctx.send(embed=embedcheck)
  await message.add_reaction('✅','❌')

  try:
      def check(rctn, user):
        return user.id == ctx.author.id and str(rctn) == '✅'
      reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
  except asyncio.TimeoutError:
      await msg.edit(embed=embedfail)
  else:
      for role in guild.roles:
        if role.name == "Muted":
            await member.add_roles(role)
            await msg.edit(embed=embeddone)

当我运行命令时,我总是得到相同的输出,“...请提及某人静音,”即使我提到了某人。当我不提及任何人时,我会收到错误

    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: message is a required argument that is missing.

【问题讨论】:

【参考方案1】:

async def mute(ctx, message, member: discord.Member = None) 中省略message 并将await message.add_reaction(...) 更改为await msg.add_reaction(...)

@bot.command()
@commands.has_permissions(manage_guild=True)
async def mute(ctx, member: discord.Member = None):
  
  guild = ctx.guild
  user = member

  if member == None:
    await ctx.send(f'**ctx.message.author,** please mention somebody to mute.')
    return 

  if member == ctx.message.author:
    await ctx.send(f'**ctx.message.author,** you cannot mute yourself, silly.')
    return 
  
  for role in guild.roles:
    if role.name == "Muted":
      if role in user.roles:
                await ctx.send("**** is already muted.".format(user))
                return

  embedcheck=discord.Embed(title="Mute", colour=0xFFD166, description=f'Are you sure you want to mute **user?**')
  
  embeddone=discord.Embed(title="Muted", colour=0x06D6A0,description=f'The mute has been done. **user** cannot talk in any channels anymore.')

  embedfail=discord.Embed(title="Not Muted",colour=0xEF476F,description=f'The mute did not carry out as I did not receive a reaction in time.')

  msg = await ctx.send(embed=embedcheck)
  await msg.add_reaction('✅')
  await msg.add_reaction('❌')

  try:
      def check(rctn, user):
        return user.id == ctx.author.id and str(rctn) == '✅'
      reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
  except asyncio.TimeoutError:
      await msg.edit(embed=embedfail)
  else:
      for role in guild.roles:
        if role.name == "Muted":
            await member.add_roles(role)
            await msg.edit(embed=embeddone)

您提到了一个用户去了message 而不是member。不提及任何人将 message 留空并向您发送错误,因为它是必需的参数。

我没有检查它是否会静音,但这应该可以解决您的 MissingRequiredArgument 错误。

【讨论】:

以上是关于Discord.py(重写)静音命令总是返回相同的响应的主要内容,如果未能解决你的问题,请参考以下文章

我需要帮助在 discord py 中创建 discord py temp 静音命令

从用户中删除角色,然后将它们添加回 Discord.py(使用 Repl.it)

在 discord.py 中获取成员的角色

禁止命令 discord.py 的问题(重写分支)

在 discord.py 中显示设置前缀的命令(重写)

Discord.py 重写,设置后台任务,后台任务不加载