summaryrefslogtreecommitdiff
path: root/luamods/notify.lua
blob: 26540e7a21ad6cbf06391503288bd2712d0d80db (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
local function parse_target(value)
    local parts = str_split(value, ':')
    if #parts ~= 2 then
        return nil
    end

    local data = {
        target = parts[1],
        type = str_to_event_type(parts[2])
    }

    if event_type_to_str(data.type) ~= parts[2] then
        data.type = nil
    end

    if data.type == nil then
        return data
    end

    if data.type < 40 then
        local users = {}

        -- kick
        if data.type >= 4 and data.type <= 7 then
            users = kick_get_channels(data.target)
        else
            users = twitch_get_users({ logins = { data.target } })
        end

        if #users == 0 then
            data.target = nil
            return data
        end

        data.target = users[1]
    end

    return data
end

local lines = {
    english = {
        ["no_subcommand"] =
        "{sender.alias_name}: No subcommand provided. Use {channel.prefix}help notify for more information.",
        ["no_message"] = "{sender.alias_name}: No message provided.",
        ["not_parseable"] = "{sender.alias_name}: This value cannot be parsed. (%s)",
        ["unknown_type"] = "{sender.alias_name}: Unknown event type. (%s)",
        ["user_not_found"] = "{sender.alias_name}: User not found. (%s)",
        ["not_found"] = "{sender.alias_name}: Event %s not found.",
        ["namesake"] = "{sender.alias_name}: You have already subscribed to this event.",
        ["list"] =
        "{sender.alias_name}: You can use '{channel.prefix}event list' to find out which events you can subscribe to.",
        ["subs"] = "{sender.alias_name}: Your subscriptions: %s",
        ["empty_subs"] = "{sender.alias_name}: You do not have any event subscriptions in this channel.",
        ["sub"] =
        "{sender.alias_name}: You have successfully subscribed to event %s",
        ["unsub"] = "{sender.alias_name}: You have successfully unsubscribed from event %s",
        ["not_subbed"] = "{sender.alias_name}: You were not subscribed to event %s"
    },
    russian = {
        ["no_subcommand"] =
        "{sender.alias_name}: Подкоманда не предоставлена. Используйте {channel.prefix}help event для большей информации.",
        ["no_message"] = "{sender.alias_name}: Сообщение не предоставлено.",
        ["not_parseable"] = "{sender.alias_name}: Это значение не может быть использовано. (%s)",
        ["unknown_type"] = "{sender.alias_name}: Неизвестный тип события. (%s)",
        ["user_not_found"] = "{sender.alias_name}: Пользователь не найден. (%s)",
        ["not_found"] = "{sender.alias_name}: Событие %s не найдено.",
        ["no_target"] = "{sender.alias_name}: Следующие значение события должно быть предоставлено.",
        ["namesake"] = "{sender.alias_name}: Такое же событие уже существует.",
        ["list"] =
        "{sender.alias_name}: Вы можете использовать '{channel.prefix}event list', чтобы узнать на какие события Вы можете подписаться.",
        ["subs"] = "{sender.alias_name}: Ваши подписки: %s",
        ["empty_subs"] = "{sender.alias_name}: Вы не подписаны на какие-либо события в этом канале.",
        ["sub"] =
        "{sender.alias_name}: Вы успешно подписались на событие %s",
        ["unsub"] = "{sender.alias_name}: Вы отписались от события %s",
        ["not_subbed"] = "{sender.alias_name}: Вы не были подписаны на событие %s"
    },
}

return {
    name = "notify",
    description = [[
The `!notify` command gives users the ability to manage event subscriptions.

> Event must be created before using `!notify` command. See about `!event` command [here](/!event).

## Syntax

### Subscribe to the event
`!notify sub [name]:[type]`

+ `[name]` - Twitch username or event name *(custom type only)*.
+ `[type]` - [Event type](/scripts.php?id=event#event-types).

### Unsubscribe from the event
`!notify unsub [name]:[type]`

+ `[name]` - Twitch username or event name *(custom type only)*.
+ `[type]` - [Event type](/scripts.php?id=event#event-types).

### Get your event subscriptions
`!notify subs`

### Get available events to subscribe
`!notify list`
]],
    delay_sec = 1,
    options = {},
    aliases = {},
    subcommands = { "sub", "unsub", "subs", "list" },
    minimal_rights = "user",
    handle = function(request)
        if request.subcommand_id == nil then
            return l10n_custom_formatted_line_request(request, lines, "no_subcommand", {})
        end

        local scid = request.subcommand_id

        if scid == "list" then
            return l10n_custom_formatted_line_request(request, lines, "list", {})
        elseif scid == "subs" then
            local names = {}

            local events = db_query([[
SELECT e.name, e.event_type FROM events e
INNER JOIN event_subscriptions es ON es.event_id = e.id
WHERE e.channel_id = $1 AND es.user_id = $2
]],
                { request.channel.id, request.sender.id })
            local user_ids = {}
            for i = 1, #events, 1 do
                local e = events[i]
                local t = tonumber(e.event_type)
                if t < 10 then
                    local id = tonumber(e.name)
                    table.insert(names, { name = id, type = t })
                    table.insert(user_ids, id)
                    print(id)
                else
                    table.insert(names, { name = e.name, type = event_type_to_str(t) })
                end
            end
            if #user_ids > 0 then
                local users = twitch_get_users({ ids = user_ids })
                for i = 1, #users, 1 do
                    local user = users[i]
                    for j = 1, #names, 1 do
                        if type(names[j].name) == "number" and
                            type(names[j].type) == "number" and
                            names[j].type < 10 and names[j].name == user.id
                        then
                            names[j].name = user.login
                            names[j].type = event_type_to_str(names[j].type)
                        end
                    end
                end
            end

            -- finalizing
            local n = {}
            for i = 1, #names, 1 do
                table.insert(n, names[i].name .. ':' .. names[i].type)
            end

            local line_id = "subs"
            if #n == 0 then
                line_id = "empty_subs"
            end

            return l10n_custom_formatted_line_request(request, lines, line_id, { table.concat(n, ', ') })
        end

        if request.message == nil then
            return l10n_custom_formatted_line_request(request, lines, "no_message", {})
        end

        local parts = str_split(request.message, ' ')

        local data_original = parts[1]
        local data = parse_target(data_original)
        table.remove(parts, 1)
        local data_name = nil

        if data == nil then
            return l10n_custom_formatted_line_request(request, lines, "not_parseable", { data_original })
        elseif data.type == nil then
            return l10n_custom_formatted_line_request(request, lines, "unknown_type", { data_original })
        elseif type(data.target) == "nil" then
            return l10n_custom_formatted_line_request(request, lines, "user_not_found", { data_original })
        elseif type(data.target) == "string" then
            data_name = data.target
        elseif type(data.target) == "table" then
            data_name = data.target.id
        end

        local events = db_query([[
SELECT e.id, es.id AS sub_id
FROM events e
LEFT JOIN event_subscriptions es ON es.event_id = e.id AND es.user_id = $1
WHERE e.name = $2 AND e.event_type = $3
]],
            { request.sender.id, data_name, data.type })

        if #events == 0 then
            return l10n_custom_formatted_line_request(request, lines, "not_found", { data_original })
        end

        local event = events[1]

        if scid == "sub" then
            if event.sub_id ~= nil then
                return l10n_custom_formatted_line_request(request, lines, "namesake", { data_original })
            end

            db_execute('INSERT INTO event_subscriptions(event_id, user_id) VALUES ($1, $2)',
                { event.id, request.sender.id })

            return l10n_custom_formatted_line_request(request, lines, "sub", { data_original })
        elseif scid == "unsub" then
            if event.sub_id == nil then
                return l10n_custom_formatted_line_request(request, lines, "not_subbed", { data_original })
            end

            db_execute('DELETE FROM event_subscriptions WHERE id = $1', { event.sub_id })
            return l10n_custom_formatted_line_request(request, lines, "unsub", { data_original })
        end
    end
}