【问题标题】:Lock command - Discord.py锁定命令 - Discord.py
【发布时间】:2021-12-28 14:42:21
【问题描述】:

这是我的锁定命令

    @commands.command()
    async def lock(self, ctx, channel: discord.TextChannel = None, reason: str = None):
        channel = ctx.channel or channel

        await channel.set_permissions(ctx.guild.default_role, send_messages=False, add_reactions=False)
        await ctx.send(f"{success} | **{ctx.author.name}**, successfully locked <#{channel.id}>",
                       ephemeral=True)

        embed = discord.Embed(
            title="Channel locked",
            description=f"This channel was locked by {ctx.author.mention} ????",
            color=embed_color
        )
        embed.add_field(name="Reason", value=reason.capitalize())
        embed.add_field(name=":warning: | Important", value="**Users with Administrator perms won't be affected**",
                        inline=False)
        embed.timestamp = datetime.datetime.utcnow()
        await channel.send(embed=embed)

它可以工作,但如果频道是私人频道(所以@everyone 看不到频道),即使在代码中(它覆盖权限)我没有 view_channel=True,机器人也会将其设置为公共频道。有什么想法可以解决吗?

【问题讨论】:

  • 与您的问题无关,但您可以使用channel.mention 而不是&lt;#{channel.id}&gt;
  • 感谢改进!
  • 也许当你做set_permissions它默认view_channel=True,所以尝试将它显式设置为False?不幸的是,如果是这种情况,我在文档中找不到参考。
  • 但是如果一个频道在被屏蔽之前对所有人都是可见的(所以@everyone 可以看到它),如果我输入view_channel=False 然后@everyone 不再看到它。这就是问题所在。
  • 你可以试试 PermissionOverwrites - discordpy.readthedocs.io/en/stable/…

标签: discord discord.py


【解决方案1】:

您也可以在此处尝试此代码,而不是提及频道或使用频道 ID,您只需使用该命令即可锁定您键入 lock in 的频道

代码:

    @commands.command(aliases=["l"])
    @commands.cooldown(1, 5, commands.BucketType.user)
    @commands.has_permissions(manage_channels=True)
    async def lock(client, ctx, channel: discord.TextChannel = None, reason = None
):
          """Lock the current channel """
          await ctx.message.delete()
          embed = discord.Embed(
            title="Channel locked",
            description=f"This channel was locked by {ctx.author.mention} ?",
            color=embed_color
        )
          embed.add_field(name="Reason", value=reason.capitalize())
          embed.add_field(name=":warning: | Important", value="**Users with Administrator perms won't be affected**",
                        inline=False)
        embed.timestamp = datetime.datetime.utcnow()
        await channel.send(embed=embed)
          overwrite = ctx.channel.overwrites_for(ctx.guild.default_role)
          overwrite.send_messages = False
          await ctx.channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)

您需要在锁定之前发送消息,这样当频道被锁定时,机器人仍然可以输入锁定的信息,因为如果它在发送之前锁定,那么机器人必须具有管理员权限

我也给了命令权限,你可以很明显地摆脱它,与冷却线一样,我只是向你展示了添加到审核命令的选项

【讨论】:

  • 如果你使用的是cogs,为什么函数中没有self参数?
  • self 仅用于@commands.Cogs.listener 代码,您只需要在参数中调用 client
  • 检查this。不过我可能是错的.. 我没有使用 cogs 的经验,但我已经看到在 cogs 中即使是命令也有一个 self 参数。
  • 只要能用,我就从来没有用过它们
  • 机器人有管理员权限,所以这不是一个真正的问题。我的“真正”问题是当它覆盖权限时,特别是当频道是私有的时,机器人会将其公开。
猜你喜欢
  • 2021-11-10
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 2021-03-26
相关资源
最近更新 更多