【问题标题】:Printing embed content from an extrenal url in Discord.py从 Discord.py 中的外部 url 打印嵌入内容
【发布时间】:2020-08-27 16:51:28
【问题描述】:

晚上,

我有一些 Twitter 链接发送到我的 Discord 频道,我想阅读该 URL 的完整 message.embeds。

例子

@bot.event
async def on_message(message):
     if message.author == bot.user:
          return
print(f'{message.embeds[0]})

输出:

<discord.embeds.Embed object at 0x1083f22d0>

下面是一个 URL 示例:https://twitter.com/FortniteStatus/status/1255881010400632832

我想打印出链接的所有字段,以便推断链接中消息的正文。比如我可以像这样找到图片***强文本***的内容。

@bot.event
async def on_message(message):
     if message.author == bot.user:
          return
print(f'{message.embeds[0].image})

这是输出

EmbedProxy(width=1920, url='https://pbs.twimg.com/media/EW3ItMcXsAAtJqs.jpg:large', proxy_url='https://images-ext-1.discordapp.net/external/07ehAt_tC9hRC9peWhXgLLK6HkBlvoWuc-_jjVb-3Js/https/pbs.twimg.com/media/EW3ItMcXsAAtJqs.jpg%3Alarge', height=1080)

从这张图片中,我想得到嵌入的主体,而不是图像。

我认为我可以通过打印嵌入来实现这一点,但看起来我得到了内存引用?想法?

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    您可以使用其to_dict 方法将Embed 转换为字典,然后打印:

    from pprint import pprint
    from discord.ext import commands
    
    bot = commands.Bot("!")
    
    @bot.event
    async def on_message(message):
        if message.embeds:
            pprint(message.embeds[0].to_dict())
    
    bot.run("token")
    

    对于您的示例,这将打印

    {'author': {'icon_url': 'https://pbs.twimg.com/profile_images/1182784466240135170/tTYzHFoe_bigger.jpg',
                'name': 'Fortnite Status (@FortniteStatus)',
                'proxy_icon_url': 'https://images-ext-1.discordapp.net/external/wF3vK-DmpMjN_SM_GIU9fRgqjVrtdyzw8xMIT883ScQ/https/pbs.twimg.com/profile_images/1182784466240135170/tTYzHFoe_bigger.jpg',
                'url': 'https://twitter.com/FortniteStatus'},
     'color': 1942002,
     'description': 'Hey, everyone!\n'
                    '\n'
                    'We\'re aware that the "Throw Henchmen overboard at The Yacht" '
                    'Location Domination Challenge may not be tracking progress '
                    'properly and are working to resolve this.\n'
                    '\n'
                    "We'll update you all when this is resolved.",
     'fields': [{'inline': True, 'name': 'Retweets', 'value': '258'},
                {'inline': True, 'name': 'Likes', 'value': '4733'}],
     'footer': {'icon_url': 'https://abs.twimg.com/icons/apple-touch-icon-192x192.png',
                'proxy_icon_url': 'https://images-ext-1.discordapp.net/external/bXJWV2Y_F3XSra_kEqIYXAAsI3m1meckfLhYuWzxIfI/https/abs.twimg.com/icons/apple-touch-icon-192x192.png',
                'text': 'Twitter'},
     'image': {'height': 1080,
               'proxy_url': 'https://images-ext-1.discordapp.net/external/07ehAt_tC9hRC9peWhXgLLK6HkBlvoWuc-_jjVb-3Js/https/pbs.twimg.com/media/EW3ItMcXsAAtJqs.jpg%3Alarge',
               'url': 'https://pbs.twimg.com/media/EW3ItMcXsAAtJqs.jpg:large',
               'width': 1920},
     'type': 'rich',
     'url': 'https://twitter.com/FortniteStatus/status/1255881010400632832'}
    

    【讨论】:

    • 完美,有效!跟进问题:) 尝试打印“description”AttributeError: 'dict' object has no attribute 'description' Code: message.embeds[0].description 时出现此错误
    • 您的嵌入可能没有描述。如果您按照说明打印出来,您会看到一个吗?
    • 我让它工作了——可能是某处的某种空白。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 2010-11-08
    • 2017-05-30
    • 2021-04-09
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多