-
Notifications
You must be signed in to change notification settings - Fork 0
/
egobot.lua
136 lines (111 loc) · 3.07 KB
/
egobot.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
130
131
132
133
134
135
136
math.randomseed(os.time())
local discordia = require('discordia')
local client = discordia.Client{
cacheAllMembers = true,
logLevel = discordia.enums.logLevel.error
}
local db = require('./libs/db')
local ts = require('./libs/tablesave')
local getdirs = require('./libs/getdirs')
const = require('libs/const')
const.client = client
const.startTime = os.date('!%Y-%m-%dT%H:%M:%S')
const.hostname = io.popen('hostname'):read()
const.version = io.popen('git show-ref --head --abbrev --hash'):read()
const.getfiles = getfiles
const.db = db
const.require = require
const.pp = require('pretty-print')
const.enums = discordia.enums
local log = require('./libs/log')
const.log = log
local data = db("./data/db")
const.data = data
if not data:exists('config') then -- Default config
data.config = {
prefix = '.'
}
end
--[[
Command line argument parsing and use
]]
local parser = require('./libs/argparse').new({
token = {
long = 'token',
short = 't',
type = 'token',
text = 'One of your discord account\'s tokens'
},
help = {
long = 'help',
short = 'h',
text = 'Displays this help message and exit, nothing is saved'
},
cfgToken = {
short = 'T',
text = 'Sets the token to use if none is provided (default: none)',
type = 'token'
},
cfgPrefix = {
short = 'P',
text = 'Sets the prefix to use if none is provided (default: .)',
type = 'any'
},
configOnly = {
short = 'E',
text = 'Exit after saving (new) config'
}
})
local argv = parser:parse(args, 2)
for i in pairs(args) do args[i] = nil end
if argv.help then
print(parser:help())
os.exit(1)
end
if argv.cfgToken then
data.config.token = argv.cfgToken
log('Set new token', log.Info)
end
if argv.cfgPrefix then
data.config.prefix = argv.cfgPrefix
log('Set new prefix to '..argv.cfgPrefix, log.Info)
end
data:save('config')
if argv.configOnly then
os.exit(1)
end
if not (argv.token or data.config.token) then
print('You must provide a token with the option -t or set it for later use with -T.\nSee --help.')
os.exit(2)
end
--[[
Webui stuff
]]
--local ui = require('./libs/webui')
--[[
Module loading
]]
local modules = require('./libs/modules')(client, {}, data) -- unused config for now, might get removed
const.modules = modules
log('Getting ready...', log.Info)
modules:load(getdirs('./mods'))
log('Loaded '..#modules..' module(s).',log.Info)
--[[
Client events
]]
client:on('ready', function()
if client.user.bot then
log('This is a selfbot, you should be using a discord user account token.', log.Error)
client:stop()
return
end
log('Egobot-rw ready for action o7', log.Info)
log('Logged in as '..client.user.username..'#'..client.user.discriminator..' ('..client.user.id..')', log.Info)
end)
client:on('messageCreate', function(message)
if client.user.id ~= message.author.id then return end
if message.content:sub(1, #data.config.prefix) == data.config.prefix then
modules:exec(message.content:sub(#data.config.prefix + 1), {message = message})
end
end)
client:run(argv.token or data.config.token)