Skip to content

Instantly share code, notes, and snippets.

@marcogravbrot
Created February 14, 2017 07:33
Show Gist options
  • Save marcogravbrot/afd2ab7c7df7424fe060fe2cc4b2a821 to your computer and use it in GitHub Desktop.
Save marcogravbrot/afd2ab7c7df7424fe060fe2cc4b2a821 to your computer and use it in GitHub Desktop.
local discordia = require("discordia")
local client = discordia.Client()
local http = require("coro-http")
local json = require("json")
local timer = require("timer")
local linking = {}
local link_checking = false
function getName(id, callback)
if not (id and callback) then
return
end
local head, res = http.request("GET", "https://scriptfodder.com/api/users/search/" .. id, {{"User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"}})
res = json.decode(res)
if res then
local player = res["users"][1]
local name = player["name"]
local group = player["usergroup"]
if name then
if group then
callback(name, group)
return
end
callback(name)
return
end
end
callback(false)
end
function checkChange()
for k, user in pairs(linking) do
if user.id and user.member and user.unique then
getName(user.id, function(name, group)
if not (name and group) then
return
end
group = tonumber(group)
if name:match(user.unique) then
if (group == 1) then
user.member:sendMessage({content = "Synced with ScriptFodder account! You have now been given the Content Creator status!"})
-- set role here
else
user.member:sendMessage({content = "Synced with ScriptFodder account, you are not a developer on ScriptFodder though so you´re status here has not changed."})
end
linking[k] = nil
end
end)
end
end
end
function linkCheck()
if table.count(linking) < 1 then
return
end
--[[
link_checking = true
coroutine.wrap(function()
local check
check = timer.setInterval(2000, function()
print("checking")
print(check)
if table.count(linking) < 1 then
link_checking = false
print("clear", check)
timer.clearInterval(check)
return
end
loadstring("checkChange()")()
end)
end)()]]
end
function linkUser(member, id)
getName(id, function(name)
if not name then
print("Failed to link")
return
end
local unique = math.random(1, 10000000000000)
table.insert(linking, {id = id, member = member, unique = unique})
linkCheck()
member:sendMessage({content = "Put this number anywhere in your ScriptFodder user name in order to link your profile to it. You can either run the .link command again or wait for the automatic check to be made. ```".. unique .."```Visit https://scriptfodder.com/dashboard/settings/profile to change user name."})
end)
end
client:on("ready", function()
print("ScriptFodder bot initialized")
end)
client:on("messageCreate", function(message)
local msg = message.content
if msg:match("^%.link") then
for k, v in pairs(linking) do
if v.member == message.author then
checkChange()
return
end
end
local id = msg:match("%d+")
if id then
linkUser(message.author, id)
end
end
end)
client:run("token")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment