Last active
May 25, 2018 06:53
-
-
Save 0x263b/106a7370fdf7fb51105a4ff502f61f7b to your computer and use it in GitHub Desktop.
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 | |
# encoding: utf-8 | |
Encoding.default_external = "UTF-8" | |
Encoding.default_internal = "UTF-8" | |
require "open-uri" | |
require "json" | |
require "csv" | |
GUILD = "" # Server ID | |
CHANNEL = "" # A Channel ID for the server, doesn't matter which | |
TOKEN = "" # My API Token | |
COOKIE = "" # My Cookie | |
REFERER = "https://discordapp.com/channels/#{GUILD}/#{CHANNEL}" | |
@member_list = [] | |
more = true | |
offset = "" | |
while more == true do # Loop through search result pages | |
begin | |
data = open( | |
"https://discordapp.com/api/v6/guilds/#{GUILD}/members?limit=1000#{offset}", | |
"Authorization" => TOKEN, | |
"Cookie" => COOKIE).read | |
data = JSON.parse(data) | |
@member_list << data | |
if data.length == 1000 | |
offset = "&after=#{data.last["user"]["id"]}" | |
else | |
more = false | |
end | |
rescue | |
more = false | |
end | |
end | |
@member_list.flatten!.uniq! | |
# Save the basic member list as backup | |
File.write("#{File.expand_path(File.dirname(__FILE__))}/member_list-#{GUILD}.json", JSON.pretty_generate(@member_list)) | |
# Loop through each member and look up their profile | |
# This gives the mutual server list and linked accounts | |
@member_list.each do |user| | |
begin | |
data = open( | |
"https://discordapp.com/api/v6/guilds/#{GUILD}/messages/search?author_id=#{user["user"]["id"]}&offset=0", | |
"Authorization" => TOKEN, | |
"Cookie" => COOKIE, | |
"Referer" => REFERER).read | |
data = JSON.parse(data) | |
user["post_count"] = data["total_results"] | |
messages = data["messages"].flatten.uniq{ |msg| msg["id"] } | |
messages_ = messages.select do |msg| | |
msg["author"]["id"] == user["user"]["id"] | |
end | |
user["last_message"] = messages_.first | |
sleep 1 # Sleep to avoid Rate Limit | |
rescue OpenURI::HTTPError => err | |
puts "Error: #{user["user"]["id"]} (#{err.message})" | |
end | |
end | |
# Save the expanded list as json | |
File.write("#{File.expand_path(File.dirname(__FILE__))}/member_list_post_count-#{GUILD}.json", JSON.pretty_generate(@member_list)) | |
@export = [] | |
@member_list.each do |user| | |
@export << { | |
:nickname => user["nick"], | |
:username => "#{user["user"]["username"]}##{user["user"]["discriminator"]}", | |
:discord_id => user["user"]["id"], | |
:joined_at => user["joined_at"], | |
:post_count => user["post_count"].nil? ? 0 : user["post_count"], | |
:last_message => user["last_message"].nil? ? nil : user["last_message"]["timestamp"] | |
} | |
end | |
# Create a CSV file for easier reading | |
File.open( | |
"#{File.expand_path(File.dirname(__FILE__))}/member_list_post_count-#{GUILD}.csv", | |
"w+:UTF-16LE:UTF-8") do |file| | |
csv_file = CSV.generate({ | |
write_headers: true, | |
headers: ["Nickname","Username","Discord ID", "Join date", "Number of posts", "Last post date"] | |
}) do |csv| | |
@export.each do |user| | |
csv << user.map { |key, value| value } | |
end | |
end | |
file.write("\xEF\xBB\xBF") #Byte Order Mark | |
file.write(csv_file) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment