This repository was archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatBot.lua
More file actions
193 lines (141 loc) · 4.76 KB
/
Copy pathChatBot.lua
File metadata and controls
193 lines (141 loc) · 4.76 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
-- //AUTO-GENERATED-CODE//
local APLibPath = settings.get('APLibPath')
assert( -- check if setup was done before, if not return with an error
type(APLibPath) == 'string',
'Couldn\'t open APLib through path: '..tostring(
APLibPath
)..'; probably you haven\'t completed Lib setup via \'LIBFILE setup\' or the setup failed'
)
assert( -- check if Lib is still there, if not return with an error
fs.exists(APLibPath),
'Couldn\'t open APLib through path: '..tostring(
APLibPath
)..'; remember that if you move the Lib\'s folder you must set it up again via \'LIBFILE setup\''
)
os.loadAPI(APLibPath) -- load Lib with CraftOS's built-in feature
APLibPath = fs.getName(APLibPath)
if APLibPath:sub(#APLibPath - 3) == '.lua' then APLibPath = APLibPath:sub(1, #APLibPath - 4); end
local APLib = _ENV[APLibPath]
APLib.APLibPath = APLibPath
APLibPath = nil
-- //MAIN-PROGRAM//
-- PARAMS
-- BASIC
local chat
local defaultPrefix = '!'
local botName = 'bot'
-- ADVANCED
local chatEvent = 'chat'
local prefix = APLib.OSSettings.get('ChatBotPrefix')
-- SEEKING FOR CHATBOX
for key, value in pairs(peripheral.getNames()) do
if peripheral.getType(value) == 'chatBox' then
chat = peripheral.wrap(value)
break
end
end
assert(chat, "Couldn't find any chatBox.")
-- SEEKING FOR PREFIX
if not prefix then
prefix = defaultPrefix
APLib.OSSettings.set('ChatBotPrefix', defaultPrefix)
end
-- MEMO
local meConsole = APLib.Memo.new(2, 4, 50, 16, {nil, nil, colors.gray})
meConsole:editable(true)
meConsole:limits(false)
-- FUNCTIONS
local function reply(user, str)
chat.say(user..': '..str)
end
local function print(string)
meConsole:setCursorPos(1, #meConsole.lines)
meConsole:print(string)
meConsole:setCursorPos(1, #meConsole.lines - 1)
meConsole:draw()
end
-- HEADER
local hTitle = APLib.Header.new(2, {'ChatBot powered by APLib '..APLib.ver})
-- LABEL
local lCredits = APLib.Label.new(2, 18, {'& PeripheralsPlusOne'})
-- BUTTON
local bQuit = APLib.Button.new(45, 18, 50, 18, {'Quit', nil, nil, colors.green, colors.red})
-- CALLBACKS
-- BUTTON CALLBACKS
bQuit:setCallback(
APLib.event.button.onPress,
function (self, event)
if self.state then
self:draw()
sleep(0.5)
self.state = false
self:draw()
APLib.stopLoop()
APLib.resetLoopSettings()
end
end
)
-- LOOP CALLBACKS
APLib.setLoopCallback(
APLib.event.loop.onInit,
function ()
print("Current prefix is '"..prefix.."'")
print()
end
)
APLib.setLoopCallback(
APLib.event.loop.onEvent,
function (event)
if event[1] == chatEvent then
local user, message = event[2], event[3]
sleep(0.5)
message = message:lower()
if (message:sub(1, #botName) == botName) or (message:sub(1, #prefix) == prefix) then
if message:sub(1, #botName) == botName then
message = message:sub(#botName + 1)
else
message = message:sub(#prefix + 1)
end
if message:sub(1, 1) == ' ' then message = message:sub(2); end
local words = APLib.stringSplit(message, ' ')
local command = table.remove(words, 1)
print('Command sent: '..command)
print('Command sent by '..user)
local args = words
print('Args used:')
print(textutils.serialize(args))
print()
if command == 'prefix' then
if args[1] then
APLib.OSSettings.set('ChatBotPrefix', args[1])
prefix = APLib.OSSettings.get('ChatBotPrefix')
reply(user, "Prefix was succesfully changed to '"..prefix.."'")
else
reply(user, "Current prefix is '"..prefix.."'")
end
elseif command == 'reboot' then
reply(user, 'Rebooting in 3 secs!')
sleep(3)
os.reboot()
elseif command == 'shutdown' then
reply(user, 'Shutting down in 3 secs!')
sleep(3)
os.shutdown()
else
reply(user, "'"..command.."' isn't a valid command!")
end
end
elseif event[1] == 'mouse_scroll' then
meConsole:setCursorPos(1, meConsole.cursor.pos.line + event[2])
end
end
)
-- PROGRAM
APLib.drawOnLoopEvent()
APLib.addLoopGroup('main', {hTitle, meConsole, lCredits, bQuit})
APLib.setLoopGroup('main')
APLib.loop()
APLib.bClear()
-- //AUTO-GENERATED-CODE//
os.unloadAPI(APLib.APLibPath)
-- //--//