-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.lua
132 lines (111 loc) · 3.56 KB
/
command.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
--- User-defined commands.
--- Add your own commands here!
--- @classmod command
local command = require "request"
--- Public commands
--- @section public
--- Get help on available commands.
function command:help()
return "oh no whats wrong"
end
--- Get IP address for a host.
function command:ip(host) -- Host domain to check.
return self:exec("host " .. self:quote(host))
end
--- Get temperature.
function command:temp(code) -- US zip or airport code.
local m = self:fetch("https://forecast.weather.gov/zipcity.php?inputstring=" .. code):match "%d+°[FC]"
return m and ("it's " .. m:gsub("°", "°")) or "try a US zip or airport code"
end
--- Define a word.
function command:define(word) -- Word to define.
return self:fetch("https://www.dictionary.com/browse/" .. word):match "definition, (.-) See more."
end
--- IMDB lookup.
function command:imdb(query)
local list = self:fetch("https://www.imdb.com/find?q=" .. query)
local link = list and list:match "href=\"(/title/[^/]+/)%?"
local page = link and self:fetch("https://www.imdb.com/" .. link)
return page and page:match 'meta name[%p%s]+description[%p%s]+content[%p%s]+([^"]+)'
end
--- Discogs lookup.
function command:discogs(query)
local list = self:fetch("https://www.discogs.com/search/?q=" .. query)
local link = list and list:match '<a href="([^"]+)" class="search_result_title"'
local page = link and self:fetch("https://www.discogs.com/" .. link)
return page and page:match '<div class="readmore" id="profile">%s+([^\n]+)'
end
--- Roll some dice.
function command:roll(dice) -- Number of dice and sides. For example 2d6 to roll two six-sided dice.
local d, s = dice:match "^(%d+)d(%d+)$"
d, s = tonumber(d), tonumber(s)
if not d or d < 1 or d > 10 or s < 2 or s > 100 then
return "try !roll 2d6"
end
if d < 2 then return reply("rolled " .. math.random(s)) end
local rolls, total = {}, 0
for i = 1, d do
rolls[i] = math.random(s)
total = total + rolls[i]
end
return ("rolled %i = (%s)"):format(total, table.concat(rolls, " + "))
end
--- Choose something randomly.
function command:choose(choices) -- A set of space-delimited choices.
local t = {}
for s in choices:gmatch "%S+" do t[#t + 1] = s end
return "choose " .. t[math.random(#t)]
end
local eightballResponses = {
"it is certain",
"it is decidedly so",
"without a doubt",
"yes, definitely",
"you may rely on it",
"as I see it, yes",
"most likely",
"outlook good",
"yes",
"signs point to yes",
"reply hazy try again",
"ask again later",
"better not tell you now",
"cannot predict now",
"concentrate and ask again",
"don't count on it",
"my reply is no",
"my sources say no",
"outlook not so good",
"very doubtful",
}
--- Magic 8-ball.
function command:eightball()
return eightballResponses[math.random(#eightballResponses)]
end
--- Admin commands
--- @section admin
--- Send a raw IRC command. Calls `bot:send`.
function command:raw(command) -- Raw IRC command to send, without terminating line breaks.
if self.bot.owner ~= self.nick then return end
self.bot:send(command)
end
--- Reload the bot. Calls `bot:reload`.
function command:reload()
if self.bot.owner ~= self.nick then return end
self.bot:reload()
return "done"
end
--- Kill the bot.
function command:die()
if self.bot.owner ~= self.nick then return end
self:reply "bye"
os.exit()
end
--- Evaluate Lua code.
function command:eval(code) -- Lua code to evaluate.
if self.bot.owner ~= self.nick then return end
return (_G.load or _G.loadstring)(
"self, command, bot = ...; " .. code)(
self, command, self.bot)
end
return command