-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
129 lines (107 loc) · 3.58 KB
/
main.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
127
128
129
-----------------
---- Globals ----
-----------------
luaPath = "lua/"
libPath = luaPath .. "lib/"
modulePath = luaPath .. "modules/"
mainChannel = discord.getChannelByID("165560868426219520") -- stammbot-dev-channel @ Stammgruppe Afterbirth
------------------------
---- Initialization ----
------------------------
function loadDependencyManager()
local func, errorStr = loadfile(libPath.."depends.lua")
if(func == nil) then
mainChannel.sendMessage("[INIT][ERROR] An error occured while loading dependency manager:\n"..errorStr)
else
func()
print("[LUA] Dependency manager loaded!")
end
end
function loadLibs() -- Load Libraries
local lsStr = os.capture("ls "..libPath)
for file in string.gmatch(lsStr, "(%a+).lua") do
depends.onLib(file)
end
end
function loadModules() -- Load Modules
local lsStr = os.capture("ls "..modulePath)
for file in string.gmatch(lsStr, "(%a+).lua") do
depends.onModule(file)
end
end
function reconnect() -- Try to reconnect to Discord
hook.remove("autoreconnect") -- Prevents multiple reconnect attempts at the same time
if(not discord.isReady()) then
discord.login()
setTimer(5000, reconnect) -- Retry in 5sec
end
end
loadDependencyManager() -- Load dependency manager
loadLibs() -- Load essential libraries
loadModules() -- Load additional modules
mainChannel.sendMessage("[INFO] Initialized!")
-----------------------------
---- Vital Chat Commands ----
-----------------------------
command.add("update", function(msg, args)
if(core.isAdmin(msg)) then
os.execute("git -C ".. luaPath .." reset --hard")
local text = os.capture("git -C ".. luaPath .." pull")
if (text ~= "Already up-to-date.") then
local beginPos, endPos, fromVersion, toVersion = string.find(text, "(%w+)%.%.(%w+)") -- Get version hashes
text = string.sub(text, endPos+15) -- Remove version hashes from string
text = string.gsub(text, "([%+%-]+)%s", "%1\n") -- Format file changes
mainChannel.sendMessage("[Update] Updating from <".. fromVersion .."> to <".. toVersion .. ">\n"..text)
mainChannel.sendMessage("[Update] Please reload the bot to apply the changes.") -- Safety delay to give the update process some time (needs admin credentials)
else
mainChannel.sendMessage("[Update] Already up-to-date.")
end
end
end)
command.add("reload", function(msg, args)
if(core.isAdmin(msg)) then
func, errorStr = loadfile(luaPath .."main.lua")
if(func == nil) then
mainChannel.sendMessage("[ERROR] An error occured while running the script:\n"..errorStr)
else
func()
end
end
end)
----------------
---- Events ----
----------------
function onDiscordDisconnectedEvent(reason)
hook.call("onDiscordDisconnected", reason)
end
function onMessageReceivedEvent(msg)
hook.call("onMessageReceived", msg)
end
function onAudioUnqueuedEvent(event)
hook.call("onAudioUnqueued", event)
end
function onAudioPlayEvent(event)
hook.call("onAudioPlay", event)
end
function onAudioQueuedEvent(event)
hook.call("onAudioQueued", event)
end
function onAudioStopEvent(event)
hook.call("onAudioStop", event)
end
function onUserVoiceChannelLeaveEvent(event)
hook.call("onUserVoiceChannelLeave", event)
end
function onUserVoiceChannelJoinEvent(event)
hook.call("onUserVoiceChannelJoin", event)
end
function onJavaErrorEvent(error)
hook.call("onJavaError", error)
end
function onPortData(data)
mainChannel.sendMessage(data)
end
hook.add("onDiscordDisconnected", "autoreconnect", function(reason)
print("[LUA] API Disconnected: "..reason.."\n[LUA] Trying to reconnect...") -- Print the reason why Discord4J lost connection
setTimer(5000, reconnect)
end)