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

时间:2020-10-31 05:20:46

标签: python command discord discord.py discord.py-rewrite

我当前正在尝试构建一个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 个答案:

答案 0 :(得分:0)

message中留出async def mute(ctx, message, member: discord.Member = None)并将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错误。