-
-
Save lexisother/0e804cfa3da1e9349bd111928b260dcf to your computer and use it in GitHub Desktop.
Discord emote archiving tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
AUTH_TOKEN = ENV["DISCORD_TOKEN"] | |
OUTPUT_DIR = "./emoji-archive" | |
SERVER_LIST = [ | |
# List of server IDs here | |
] | |
require 'net/http' | |
require 'json' | |
require 'fileutils' | |
API_BASE = "https://discord.com/api/v8" | |
API_GET_GUILD = "#{API_BASE}/guilds/{id}" | |
CDN_BASE = "https://cdn.discordapp.com" | |
CDN_EMOJI_URL = "#{CDN_BASE}/emojis/{id}.{ext}" | |
def http_request(method, url, params, header = {}, json: false) | |
uri = URI url.gsub(/{(.+?)}/) { params[$1.to_sym] } | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.start do | |
response = case method | |
when :get then http.request_get(uri.request_uri, header) | |
else fail "bad request type" | |
end | |
content = response.body | |
content = JSON.parse(content, symbolize_names: true) if json | |
return content | |
end | |
end | |
emoji_count = 0 | |
SERVER_LIST.each do |server| | |
dirname = File.dirname(OUTPUT_DIR) | |
unless File.directory?(dirname) | |
FileUtils.mkdir_p(dirname) | |
end | |
guild = http_request(:get, API_GET_GUILD, { id: server }, { 'Authorization' => AUTH_TOKEN }, json: true) | |
puts %(Processing "#{guild[:name]}", emojis: #{guild[:emojis].length}) | |
emoji_count += guild[:emojis].length | |
guild[:emojis].each do |emoji| | |
fext = emoji[:animated] ? "gif" : "png" | |
fname = "#{emoji[:name]}-#{emoji[:id][-8..-1]}" | |
guildname = guild[:name] | |
guilddir = File.join(OUTPUT_DIR, guildname) | |
unless File.directory?(guilddir) | |
puts %(Creating directory "#{guilddir}" for guild "#{guildname}") | |
FileUtils.mkdir_p(guilddir) | |
end | |
outpath = "#{OUTPUT_DIR}/#{guildname}/#{fname}.#{fext}" | |
next if File.exists?(outpath) | |
imgdata = http_request(:get, CDN_EMOJI_URL, { id: emoji[:id], ext: fext }) | |
bytes = File.open(outpath, "wb") { _1.write(imgdata) } | |
puts %(-- Downloaded :#{emoji[:name]}: (#{bytes} bytes)) | |
end | |
sleep(0.2) | |
end | |
puts %(Total emojis: #{emoji_count}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment