6.2.2. Demo & Practice: Author Info#

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

Though Discord probably collects the device/program sources of posts (like we saw from former President Trump), it unfortunately doesn’t let us see them.

Instead we will look at other information about the author of posts on Discord that can perhaps tell us something about their authenticity.

Log into Discord#

These are our normal steps get discord loaded and logged in

# 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) 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.

# (optional)
%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

Load a set of Discord posts#

The code below finds a set of posts from a discord channel. Try loading from real channels with your bot by doing the following:

  • put in your special Discord token

  • skip the fake_discord step above

  • put in a different channel id (that your bot has access to

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

client = discord.Client(intents=discord.Intents.default())

# 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_posts # Save the recent_posts 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=10)
    
    #special code to turn the post_history from discord into a python list
    recent_posts = [post async for post in 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

Display author information for posts#

The code below loads posts from discord, and then does a loop though all the posts, printing out the information about the authors of the submissions, such as:

  • The creation date for the account

  • the author’s avatar

  • if the author is labeled as a bot

  • other “public flags” about the other (e.g., if it’s been labelled as a “spammer”)

for post in recent_posts:
    print("Info for post with content: " + str(post.content))
    print("  author display name: " + str(post.author.display_name))
    print("  author id: " + str(post.author.id))
    print("  author created: " + str(post.author.created_at))
    print("  author avatar: " + str(post.author.avatar))
    print("  author is a bot?: " + str(post.author.bot))
    print("  author public flags: " + str(dict(iter(post.author.public_flags))))
    print()
Info for post with content: Breaking news: A lovely cat took a nice long nap today!
  author display name: fake_user
  author id: 4564563
  author created: 2023-07-25 01:23:04
  author avatar: fake_image.jpg
  author is a bot?: False
  author public flags: {'verified_bot': False, 'spammer': False}

Info for post with content: Breaking news: Someone said a really mean thing on the internet today!
  author display name: pretend_user
  author id: 986545
  author created: 2023-09-25 01:23:04
  author avatar: pretent_image.jpg
  author is a bot?: True
  author public flags: {'verified_bot': True, 'spammer': False}

Info for post with content: Breaking news: Some grandparents made some yummy cookies for all the kids to share!
  author display name: imaginary_user
  author id: 2358658
  author created: 2023-10-25 01:23:04
  author avatar: imaginary_image.jpg
  author is a bot?: False
  author public flags: {'verified_bot': False, 'spammer': True}

Info for post with content: Breaking news: All the horrors of the universe revealed at last!
  author display name: not_real_user
  author id: 73458345
  author created: 2023-12-25 01:23:04
  author avatar: not_real_image.jpg
  author is a bot?: False
  author public flags: {'verified_bot': False, 'spammer': False}