Skip to content

Instantly share code, notes, and snippets.

@eslindsey
Last active June 28, 2017 08:00
Show Gist options
  • Save eslindsey/43560a77612e8e8dc703c93dafe371d9 to your computer and use it in GitHub Desktop.
Save eslindsey/43560a77612e8e8dc703c93dafe371d9 to your computer and use it in GitHub Desktop.
Set avatar for Discord bot
#!/bin/bash
# This script allows you to update the avatar for a Discord bot you created
# (regardless of whether your framework includes that feature or not). Discord
# uses the App Icon when you first create a bot, but after that it requires an
# API call if you want to change it. This script performs that API call.
if [ $# -lt 2 ]; then
echo
echo "Usage: $0 bot-token filename"
echo
echo "To get your bot token, got to the My Apps section of Discord, pull up your bot, look under APP BOT USER for the Token line. You may need to click a link to get the token to display."
echo
echo "Per the Discord API, filename should be a JPEG, PNG, or GIF image."
echo
exit 1
fi
if ! [ -r $2 ]; then
echo "$2 is not readable"
exit 2
fi
TYPE=$(file -b --mime-type $2)
DATA=$(base64 -w 0 $2)
# Run the cURL command and output to STDOUT, but grab the HTTP code in a variable
# Thanks https://superuser.com/a/862395/740441
exec 3>&1
HTTP_STATUS=$(curl -isw '%{http_code}' -o >(cat >&3) \
--request PATCH \
--header "Authorization: Bot $1" \
--header 'User-Agent: AvatarScript (https://gist.github.com/eslindsey/43560a77612e8e8dc703c93dafe371d9, 0.1)' \
--header 'Content-Type: application/json' \
--data "{\"avatar\":\"data:$TYPE;base64,$DATA\"}" \
'https://discordapp.com/api/users/@me')
echo
echo
if [ "$HTTP_STATUS" -ne 200 ]; then
echo "ERROR: There was a problem setting the avatar."
echo "The server's response is displayed above, which should tell you why the request failed."
exit 3
fi
echo "SUCCESS: Avatar set successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment