【问题标题】:Discord bot retrieve pins from all channelsDiscord bot 从所有渠道检索引脚
【发布时间】:2019-03-02 23:55:55
【问题描述】:

我希望我的 Bot 从该服务器中的所有 6 个通道发布 pin,但是,我的 bot 仅从调用命令的当前通道获取 pin。我想知道是否有办法解决这个问题。不和谐版本 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)

以下代码从存在此机器人的所有服务器中检索用户详细信息。我想知道是否应该在第一个代码 sn-p 中使用公会而不是频道?结果这给了我一个错误。

 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"```py\ntotal: {guild.member_count} \nonline: {online}  \nidle: {idle}  \noffline: {offline}```")

【问题讨论】:

  • 我之前没有使用过1.0.0a,但查看文档似乎guild 确实有一个channels 可迭代的,它可以满足您的需求(参见here )。您可以尝试查看它是否适用于message.guild.channels,然后在循环中的每个通道上调用pins()。不过要小心,因为channels 似乎还包括各种渠道,例如语音渠道而不仅仅是文本渠道,因此您可能还需要为TextChannel 添加类型检查。
  • 感谢 Xay 成功了 :)

标签: python discord.py


【解决方案1】:

感谢 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)

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 2019-10-31
    • 1970-01-01
    • 2021-06-30
    • 2021-04-11
    • 2020-11-11
    • 1970-01-01
    • 2020-10-02
    • 2021-08-08
    相关资源
    最近更新 更多