Discord机器人从所有通道中检索引脚



我希望我的机器人从该服务器中的所有6个频道中发布引脚,但是,我的bot仅从命令的当前通道中获取引脚。我想知道是否有办法解决这个问题。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)

以下代码从所有的机器人中都从所有服务器中检索用户详细信息。我想知道是否应该在第一个代码段中使用公会代替渠道?结果给了我一个错误。

 if "member_status()" == message.content.lower():
        online = 0
        idle = 0
        offline = 0
        print(f"Testing the API with guild.owner: {guild}")
        for i in guild.members:
            if str(i.status) == "online":
                online +=1
            elif str(i.status) == "offline":
                offline +=1
            else:
                idle +=1
        await message.channel.send(f"```pyntotal: {guild.member_count} nonline: {online}  nidle: {idle}  noffline: {offline}```")

我能够凭借Xay的评论来弄清楚这一点。您要做的就是使用

检索所有频道
message.guild.text_channels

返回所有文本通道的列表。然后,您遍历每个通道,并使用

检索每个通道中存在的引脚列表
myPins = await mychannel.pins()

它可能不是最有效的代码,但它可以完成工作:)最后再次迭代以从该通道重新播放每个引脚。

最终的代码看起来像:

    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)

最新更新