Discord bot从所有通道检索管脚

2024-10-01 22:29:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让我的机器人发布这个服务器上所有6个通道的管脚,但是,我的机器人只从调用命令的当前通道获取管脚。我想知道有没有办法解决这个问题。Discord版本1.0.0A

我现在的代码是:

    if "seepins()" == message.content.lower():
        # retrieve and post all pins again
        allPins = await message.channel.pins()
        for i in allPins:
            # Check if pin is text or a link
            mat = i.attachments
            if len(mat)==0:
                await message.channel.send(i.content)
            else:
                await message.channel.send(mat[0].url)

以下代码从存在此bot的所有服务器检索用户详细信息。我想知道是否应该在第一个代码片段中使用guild而不是channel?结果这给了我一个错误。在

^{pr2}$

Tags: 代码命令服务器sendmessageifchannel机器人
1条回答
网友
1楼 · 发布于 2024-10-01 22:29:20

多亏了谢伊的评论,我才弄明白这一点。 你要做的是使用

message.guild.text_channels

返回所有文本频道的列表。 然后迭代每个通道,并使用

^{pr2}$

它可能不是最有效的代码,但它确实起到了作用:) 最后再次迭代以重新发布该通道中的每个管脚。在

final的代码如下:

    if "getAllPins()" == message.content.lower():
        # get all channels
        allChannels = message.guild.text_channels
        # go through each channel
        for myChannel in allChannels:
            # get pins present in this channel
            myPins = await mychannel.pins()
            # re-post all the pins
            for rePin in myPins:
                mat = rePin.attachments
                if len(mat)==0:
                    await message.channel.send(rePin.content)
                else:
                    await message.channel.send(mat[0].url)

相关问题 更多 >

    热门问题