4.5.3. Demo: Data from a Discord Post#

Choose Social Media Platform: Reddit | Discord | Bluesky | No Coding

Let’s see what the data actually looks like from a Discord Post!

First we need to do our normal Discord login steps (and optional fake discord step)

Log into Discord (or fake Discord)#

Load “discord” and nest_asyncio libraries#

# Load some code called "discord" that will help us work with Discord
import discord

# Load another library that helps the bot work in Jupyter Noteboook
import nest_asyncio
nest_asyncio.apply()

(Optional) Step 1b: Make a fake Discord connection with the fake_discord library#

For testing purposes, we’ve added this line of code, which loads a fake version of Discord, so it wont actually connect to Discord. If you want to try to actually connect to Discord, don’t run this line of code.

%run ../../fake_apis/fake_discord.ipynb
Fake discord is replacing the discord.py library. Fake discord doesn't need real passwords, and prevents you from accessing real discord

Step 2: Set up your Discord connection#

To use this on your real Discord account, copy your discord token into the code below, replacing our fake passwords.

Also, since we are going to be reading posts, we need to indicate that our bot wants to read message contents, so we have additional lines of code to mark that.

Note: you’ll have to re-run this step if you want to re-run your bot

# Set up your Discord connection
# TODO: put the discord token for your bot below
discord_token = "m#5@_fake_discord_token_$%Ds"

# set up Discord client with permissions to read message_contents
intents = discord.Intents.default()
intents.message_content = True 

Find a Discord Post#

Below is code to find a recent Discord post from a discord channel with the id 123456789 (if you want to load a real channel, you’ll have to get the channel id)

Don’t worry if you don’t understand this part yet. We are just doing this, so we can get to the point of seeing what discord data looks like.

Note: If you run this on real Discord, we can’t gurantee anything about how offensive what you might find is.

# set up discord connection
client = discord.Client(intents=intents)

# TODO: put the discord channel id number below for the channel you want to use
channel_id = 123456789

# Provide instructions for what your discord bot should do once it has logged in
@client.event
async def on_ready():
    global recent_post # Save the recent_post variable outside our running bot
    
    # Load the discord channel you want to post to
    channel = client.get_channel(channel_id)

    # Get the latest post in the channel history
    post_history = channel.history(limit=1)
    recent_post = await anext(post_history)

    # Tell your bot to stop running
    await client.close()
    
# Now that we've defined how the bot should work, start running your bot
client.run(discord_token)
Fake discord is pretending to set up a client connection
Fake discord bot is fake logging in and starting to run
Fake discord bot is shutting down

Look at data in Discord post#

Now we will look at some of the data that came back!

Again, don’t worry too much about the code, we want to look at the data and data types.

post content:#

display("The data type of the message content is: " + type(recent_post.content).__name__)
display("The post content is: " + recent_post.content)
'The data type of the message content is: str'
'The post content is: Scientists have cloned dangerous dinosaurs!'

As you can see above, the text of a post is a string (str) data type.

post id#

display("The data type of the message id is: " + type(recent_post.id).__name__)
display("The message id is: " + str(recent_post.id))
'The data type of the message id is: int'
'The message id is: 12345'

The post content id is a whole number (int) that looks like random number. This is how the post is referred to inside Discords’ computers. So if someone is commenting on a post, Discord just puts uses content ID they were commenting on to see where to display it.

submission author display name#

display("The data type of the author display name is: " + type(recent_post.author.display_name).__name__)
display("The author display name is: " + str(recent_post.author.display_name))
'The data type of the author display name is: str'
'The author display name is: fake_user'

The author name is an string (str). Note recent_post.author has other information about the author as well as the name.

message author id#

display("The data type of the author id is: " + type(recent_post.author.id).__name__)
display("The author id is: " + str(recent_post.author.id))
'The data type of the author id is: int'
'The author id is: 4589435'

The author name is a number (int).

post created at#

display("The data type of the post created_at is: " + type(recent_post.created_at).__name__)
display("The created_at at is: " + str(recent_post.created_at))
'The data type of the post created_at is: datetime'
'The created_at at is: 2024-07-25 01:23:04'

The created at time for the submission is a datetime, which may be in Coordinated Universal Time zone.

message reactions#

display("The data type of the reactions to the message is: " + type(recent_post.reactions).__name__)
display("The reactions to the message are: " + str(recent_post.reactions))
'The data type of the reactions to the message is: list'
"The reactions to the message are: [FakeReaction(emoji='👍', me=False, count=2), FakeReaction(emoji='🥳', me=True, count=1)]"

The reactions to a message are saved as list. Each item is the list has the emoji reaction, if the current user did this reaction, and the count of how many users gave that emoji reaction.

message is pinned?#

display("The data type of the message pinned: " + type(recent_post.pinned).__name__)
display("Is this message pinned?: " + str(recent_post.pinned))
'The data type of the message pinned: bool'
'Is this message pinned?: False'

The status of whether or not a messaged is pinned is a true/false value (bool).

Still more!#

In addition to the data we looked at above, there are even more options for Discord messages. The documentation is formatted in a not very friendly way, but you can see more info about messages here: