【问题标题】:Lock command discord.py锁定命令 discord.py
【发布时间】:2021-11-10 12:04:53
【问题描述】:

我发出了一个锁定命令,但是当fl.lock #general test 用于#general 频道时,它不会锁定通用频道,但当fl.lock #general test 用于#staff-chat 频道时,它会锁定#general 频道

因此,当 lock 命令中提到该频道并且在同一频道中使用该频道时,我的机器人不会锁定频道

代码

  @commands.command(case_insensitive = True)
  @commands.has_any_role(885434191783788564, 856071314740740096, 856061783722426378, 856465667296985108)
  async def lock(self, ctx, channel: discord.TextChannel, *, reason=None): 
    channel = channel or ctx.channel
    overwrite = channel.overwrites_for(ctx.guild.default_role)
    if overwrite.send_messages == True:
      overwrite.send_messages = False
      await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
      em_reason = discord.Embed(title="**Channel locked**", description=f":lock: {reason}",color=discord.Color.red())
      embed = discord.Embed(description = f"<a:fl_check:874522235879186483> Locked channel <#{channel.id}>",color=discord.Color.green())
      await ctx.send(embed=em_reason)
      await ctx.send(embed=embed)
    elif overwrite.send_messages == False:
      em = discord.Embed(description="<a:fl_no:874522273984442420> That channel is already locked.", color=discord.Color.red())
      await ctx.send(embed=em)```

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    我认为channel = channel or ctx.channel 是这里的问题。

    您应该直接检查channel 参数是否为None,而不是使用ctx.channel

    @commands.command(case_insensitive = True)
      @commands.has_any_role(885434191783788564, 856071314740740096, 856061783722426378, 856465667296985108)
      async def lock(self, ctx, channel: discord.TextChannel=None, *, reason=None): 
        if channel is None:
           channel = ctx.channel
        overwrite = channel.overwrites_for(ctx.guild.default_role)
        if overwrite.send_messages == True:
          overwrite.send_messages = False
          await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
          em_reason = discord.Embed(title="**Channel locked**", description=f":lock: {reason}",color=discord.Color.red())
          embed = discord.Embed(description = f"<a:fl_check:874522235879186483> Locked channel <#{channel.id}>",color=discord.Color.green())
          await ctx.send(embed=em_reason)
          await ctx.send(embed=embed)
        elif overwrite.send_messages == False:
          em = discord.Embed(description="<a:fl_no:874522273984442420> That channel is already locked.", color=discord.Color.red())
          await ctx.send(embed=em)
    

    【讨论】:

    • 没用控制台也没有任何错误
    猜你喜欢
    • 2021-12-28
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 2021-03-26
    相关资源
    最近更新 更多