Discord.py 在频道命令中取消固定消息?

Discord.py Unpin messages in a channel command?

到目前为止我得到的是:

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

我的问题在 pins = await channel.pins() - 我不知道如何访问频道中固定的消息。如果有人可以提供帮助,将不胜感激。

您将 ctx.channel 返回为字符串。这就是您无法访问引脚的原因。如果将行 channel = str(ctx.channel) 更改为 channel = ctx.channel,您的问题将得到解决。

此外,您应该将参数 amount=None 更改为 amount=0

问题出在这一行:

channel = str(ctx.channel)

这一行returns通道对象的字符串值。 您在以下代码中使用它:

pins = await channel.pins() # i.e. get string.pins()

因为字符串没有 pins() 函数。它会引发错误,或者无法正常运行。


为了解决这个问题,您应该将 ctx.channel 对象分配给通道变量。这样我们就不会使用 str().

将其转换为字符串

现在我们有了一个通道对象,我们可以正确使用pins()函数,做你想做的事。

@Bot.command()
async def unpin(ctx, amount = None):
    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.")
    else:
        pins = await channel.pins()
        for message in pins:
                await message.unpin()
                x+=1
        x1 = str(x)
        await ctx.send(f"Unpinned {x} messages from #{channel}")