-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua-twitch.lua
182 lines (158 loc) · 4.66 KB
/
lua-twitch.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
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
local socket = require("socket")
--[[
@TODO:
* make a subscriber thing
]]
local TwitchBot = {
DEFAULT_HOST = 'irc.chat.twitch.tv',
DEFAULT_PORT = 6667,
SECURE_PORT = 443,
COMMAND_RATE = 20/30, -- we're allowed to send 20 times every 30 seconds, or once every 20/30 second
commands = {},
handlers = {}
}
TwitchBot.__index = TwitchBot
setmetatable(TwitchBot, {
__call = function (class, ...)
return class.new(...)
end,
})
---
-- Create a new TwitchBot.
-- @name TwitchBot.new
-- @param ... Whatever
-- @return A new instance of TwitchBot
function TwitchBot.new(nickname, password, channel, host, port)
local self = setmetatable({}, TwitchBot)
-- set properties on self here
assert(nickname and type(nickname) == 'string', "nickname must be set and a string")
self.nickname = string.lower(nickname)
assert(password and type(password) == 'string' and password ~= "", "password must be set and a non-empty string")
self.password = password
assert(channel and type(channel) == 'string', "channel must be set and a string")
self.channel = string.lower(channel)
self.waitTime = 0
self.queue = {}
self.messages = {}
self.connected = false
self.host = host or TwitchBot.DEFAULT_HOST
self.port = port or TwitchBot.DEFAULT_PORT
self.debug = false
self:connect()
return self
end
function TwitchBot:connect()
local conerr
if self.connection == nil then
if self.port == TwitchBot.SECURE_PORT then
-- be secure
assert(false, "ssl not implemented")
else
self.connection, conerr = socket.tcp()
end
assert(self.connection ~= nil, "socket failed: " .. tostring(conerr))
self.connection:settimeout(0)
end
self.connection:connect(self.host, self.port)
end
function TwitchBot:disconnect()
if self.connection ~= nil then
self.connection:close()
end
self.connection = nil
end
---
-- Update the command queue and send any messages that weren't released yet.
function TwitchBot:update(dt)
self:read()
if self.waitTime > 0 then
self.waitTime = self.waitTime - dt
if self.waitTime < 0 then
self.waitTime = 0
end
end
if self.waitTime <= 0 and #self.queue > 0 then
if self.connection ~= nil then
local snd = self.queue[1]
if self.debug then
print('dump[OUT]', snd)
end
local sent, senderr, lastbytesent = self.connection:send(snd..'\r\n')
if sent ~= nil then
table.remove(self.queue, 1)
self.waitTime = TwitchBot.COMMAND_RATE
return true
end
assert(sent ~= nil, "Send failed:" .. tostring(senderr) .. "; last byte sent:" .. tostring(lastbytesent))
return false
end
end
end
function TwitchBot:send(data)
table.insert(self.queue, data)
return false
end
function TwitchBot:sendMessage(message)
self:send("PRIVMSG " .. self.channel .. " :" .. message)
end
function TwitchBot.commands.login(self)
if not self.connected then
self:connect()
socket.sleep(1)
assert(self.password and type(self.password) == 'string' and self.password ~= "", "password must be set and a non-empty string")
self:send("PASS "..self.password)
self:send("NICK "..self.nickname)
--self:send("CAP REQ :twitch.tv/membership")
--self:send("CAP REQ :twitch.tv/tags")
self.connected = true
end
end
TwitchBot.handlers["376"] = function(self, rawcmd, tags, prefix, cmd, param)
self:send("JOIN "..self.channel)
end
TwitchBot.handlers["PRIVMSG"] = function(self, rawcmd, tags, prefix, cmd, param)
if param ~= nil then
param = string.sub(param,2)
local param1, param2 = string.match(param,"^([^:]+) :(.*)$")
local username, userhost = string.match(prefix,"^([^!]+)!(.*)$")
table.insert(self.messages, {
username = username,
message = param2,
tags = tags,
})
end
end
function TwitchBot:read()
local buffer, err
local tags, prefix, cmd, param
err = nil
if self.connection ~= nil then
buffer, err = self.connection:receive("*l")
if err ~= nil or err == "timeout" then
TwitchBot.commands.login(self)
end
if buffer ~= nil then
if self.debug then
print('dump[IN]', buffer, err)
end
if string.sub(buffer,1,4) == "PING" then
self:send(string.gsub(buffer,"PING","PONG",1))
else
tags, prefix, cmd, param = string.match(buffer, "^([^:]*:?)([^ ]+) ([^ ]+)(.*)$")
if TwitchBot.handlers[cmd] and type(TwitchBot.handlers[cmd]) == "function" then
TwitchBot.handlers[cmd](self, buffer, tags, prefix, cmd, param)
end
end
end
else
assert(false, "No connection.")
end
return buffer, err
end
function TwitchBot:readMessage()
if #self.messages <= 0 then
return nil
end
return table.remove(self.messages, 1)
end
return TwitchBot