【问题标题】:Discord.py | Unpin command不和谐.py |取消固定命令
【发布时间】:2020-10-04 12:39:23
【问题描述】:

基本上我想做的是做一个有点像这样的命令:

用户:>取消固定 10

机器人取消固定所述金额并返回:#channel 中未固定的金额

到目前为止,我所做的只是:

@Bot.command()
async def unpin(ctx, amount = 0):
    await ctx.message.delete()
    channel = ctx.channel
    x = 0
    amount = int(amount)
    if amount == 0:
        await ctx.send("How many messages do you want to unpin, max is 50.")
    if amount > 50:
        await ctx.send("Maximum amount is 50")
    else:
        pins = await channel.pins()
        while x <= amount:
            for message in pins:
                await message.unpin()
            x+=1
        await ctx.send(f"Unpinned {x} messages from #{channel}")

这里的问题是机器人取消固定每条消息而不是给定数量。我该如何更改它以使其取消固定给定数量?

【问题讨论】:

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


    【解决方案1】:

    让它更pythonic。不要使用while x &lt;= amount: ... x += 1,而是使用 for 循环。另外,不要双循环。第二个循环遍历所有引脚并取消所有引脚。一个更好的系统的例子可能如下:

    ...
        else:
            for i in range(amount):
                pins = await channel.pins()
                await pins[-1].unpin()
            await ctx.send(f"Unpinned {x} messages from #{channel}")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 2022-01-26
      相关资源
      最近更新 更多