Created
March 2, 2021 19:02
-
-
Save Mijyuoon/05a0f9723ed524ad352974189b57e702 to your computer and use it in GitHub Desktop.
Discord emote archiving tool
This file contains hidden or 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' | |
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| | |
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]}" | |
outpath = "#{OUTPUT_DIR}/#{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