10.4.2. Demo: Alt-text From Discord#
Choose Social Media Platform: Reddit | Discord | Bluesky | No Coding
Let’s search for images and look up some alt-text on images posted to Discord
First we need to do our normal Discord login steps (and optional fake discord step)
Discord Setup#
# 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.
%run ../../fake_apis/fake_discord.ipynb
# 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
Load posts from a discord channel#
We’ll load a set of posts from a discord channel (if you are doing this on real discord, you’ll have to put the correct channel_id
in the code). Hopefully the posts will have some images.
# 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_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 shoould work, start running your bot
client.run(discord_token)
display alt-text of any images we find in those posts#
To see alt-text in posts, we check the “attachments” for embedded information (which might be images). If it does, then we display info about the post, and loop through the attachements (which may be images), display the alt-text description and a link to the attachment.
for post in recent_posts:
if(len(post.attachments) > 0):
print("Post by " + post.author.display_name + ":")
print(" ---- ")
print(" - Content: " + post.content)
for attachment in post.attachments:
print(" ---- ")
print(" - attachment content type: " + str(attachment.content_type))
print(" - alt text (description): " + str(attachment.description))
print(" - img link (proxy_url): " + str(attachment.proxy_url))
print("---------------------------------------")
print()
Post by fake_user:
----
- Content: Breaking news: A lovely cat took a nice long nap today!
----
- attachment content type: image/png
- alt text (description): a peaceful cat
- img link (proxy_url): fake_cat1.jpg
----
- attachment content type: image/png
- alt text (description): another view of a peaceful cat, sleeping on a couch in a sunbeam
- img link (proxy_url): fake_cat2.jpg
---------------------------------------
Post by pretend_user:
----
- Content: Breaking news: Someone said a really mean thing on the internet today!
----
- attachment content type: image/png
- alt text (description): None
- img link (proxy_url): img1.jpg
---------------------------------------
Post by not_real_user:
----
- Content: Breaking news: All the horrors of the universe revealed at last!
----
- attachment content type: image/png
- alt text (description): eldritch horror
- img link (proxy_url): img2.jpg
---------------------------------------
What alt-texts do you find helpful?#
In the above output, you are in a position where you can read the alt-text of the image, but you can’t see the image (unless you open up the actual posts, which if you are looking at the fake_discord output, there are no actual posts).
Without seeing the images you can hopefully see what makes alt-text useful or not. Posts without alt-text will be hard to make sense of, and some alt text on photos might tell you information about the photo, but not the information you need.