要取消固定频道命令中的消息吗?

时间:2020-10-02 07:40:16

标签: discord.py discord.py-rewrite

到目前为止,我所得到的是这样的:

@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()-我不知道如何访问频道中的固定消息。如果有人可以提供帮助,将不胜感激。

2 个答案:

答案 0 :(得分:0)

您将ctx.channel返回到字符串中。这就是为什么您无法访问图钉。如果将行channel = str(ctx.channel)更改为channel = ctx.channel,您的问题将得到解决。

而且,您还应该将参数amount=None更改为amount=0

答案 1 :(得分:0)

问题是这一行:

channel = str(ctx.channel)

此行返回通道对象的字符串值。 您可以在以下代码中使用它:

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

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


要解决此问题,应将ctx.channel对象分配给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}")