Alkemee
Alkemee

Reputation: 69

Printing embed content from an extrenal url in Discord.py

Evening,

I am having some Twitter links sent to my a Discord channel and I want to read the full message.embeds of the URL.

Example

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

Output:

<discord.embeds.Embed object at 0x1083f22d0>

Here is an example of the URL that would come through: https://twitter.com/FortniteStatus/status/1255881010400632832

I would like to print out all fields of the link so I can extrapolate the body of the message in the link. For example, I can find the contents of the image***strong text*** like this.

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

Here is the output

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)

From this picture, I would like to get the body of the embed, not the image.

I thought I could achieve this by printing out the embed, but it looks like I got a memory reference instead? Thoughts?

enter image description here

Upvotes: 1

Views: 1388

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 61063

You can convert the Embed to a dictionary with its to_dict method, then print that:

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")

For your example, this would print

{'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'}

Upvotes: 2

Related Questions