This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.lua
126 lines (105 loc) · 4.57 KB
/
bot.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
local discordia = require("discordia")
local client = discordia.Client()
local json = require('json')
local fs = require('fs')
local prefix = "m?"
discordia.extensions() -- load all helpful extensions
client:on("ready", function() -- bot is ready
print("Logged in as " .. client.user.username)
client:setGame("breadsticks")
end)
client:on("messageCreate", function(message)
if message.author.bot then return end
local content = message.content
local args = content:split(" ") -- split all arguments into a table
if args[1] == prefix.."src_code" then
message:reply("This bots source code is hosted on github which is found here: https://github.com/Breadsticks-Speedruning-Community/Discord-Bot !")
elseif args[1] == prefix.."wr" then
message:delete()
local http = require('coro-http')
if args[2] == "any" then
local res, body = http.request("GET", "https://www.speedrun.com/api/v1/leaderboards/yd4ke3k6/category/z279vo0d")
if res.code > 299 then
print('Failed to fetch speedrun.com: ' .. res.reason)
else
print('Successfully fetched speedrun.com´s api!')
local json = require('json')
local default = json.decode(body).data.runs[1].run
local video = tostring(default.videos.links[1].uri)
local time = tostring(default.times.primary_t)
local name = ""
local pic = ""
local usernamelink = default.players[1].uri
local resd, bodyd = http.request("GET", usernamelink)
if resd.code > 299 then
print("failed")
else
name = tostring(json.decode(bodyd).data.names.international)
pic = tostring(json.decode(bodyd).data.assets.image.uri)
end
local finalstring = "The current any% world record is a "..SecondsToClock( time).." set by "..name
message:reply(finalstring)
client:setGame("Any% wr by "..name)
message:reply(video)
end
elseif args[2] == "room1"then
finalstring, video = getroom(1)
message:reply(finalstring)
message:reply(video)
elseif args[2] == "room2"then
finalstring, video = getroom(2)
message:reply(finalstring)
message:reply(video)
elseif args[2] == "room3"then
finalstring, video = getroom(3)
message:reply(finalstring)
message:reply(video)
else
message:reply("**YOU HAVE TO INPUT A VALID PARAMETER**")
end
elseif args[1] == prefix.."help"then
message:reply("The commands available are: \n "..prefix.."wr {any, room1, room2, room3} \n "..prefix.."src_code")
elseif args[1] == prefix.."prefix"then
local author = message.guild:getMember(message.author.id)
if author:hasPermission("administrator") then
-- TO DO
end
end
end)
function SecondsToClock(time)
local seconds = time
local formated = string.format("%.2i:%.2i:%.2i", -- minutes:seconds:milliseconds
(seconds / 60) % 60, -- minutes
seconds % 60, -- seconds
(seconds - math.floor(seconds)) * 100 -- milliseconds
)
return formated
end
function getroom(what_room)
local http = require('coro-http')
local res, body = http.request("GET", "https://www.speedrun.com/api/v1/categories/wdm7m6od/records")
if res.code > 299 then
print('Failed to fetch speedrun.com: ' .. res.reason)
else
print('Successfully fetched speedrun.com´s api!')
local json = require('json')
local default = json.decode(body).data[what_room].runs[1].run
local video = tostring(default.videos.links[1].uri)
local time = tostring(default.times.primary_t)
local name = ""
local pic = ""
local usernamelink = default.players[1].uri
local resd, bodyd = http.request("GET", usernamelink)
if resd.code > 299 then
print("failed")
else
name = tostring(json.decode(bodyd).data.names.international)
pic = tostring(json.decode(bodyd).data.assets.image.uri)
end
local finalstring = "The current room"..what_room.."% world record is a "..SecondsToClock( time).." set by "..name
return finalstring, video
end
end
local content = fs.readFileSync('token.json')
local data = json.decode(content)
client:run('Bot '..data.token)