summaryrefslogtreecommitdiff
path: root/luamods/join.lua
blob: e18660c88adad6aab6f407647224ce980819e865 (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
local lines = {
    english = {
        ["command_unavailable"] = "{sender.alias_name}: This command is not available.",
        ["user_not_found"] = "{sender.alias_name}: User %s not found",
        ["join_not_allowed"] = "{sender.alias_name}: The bot cannot join chat rooms!",
        ["join_from_bot_channel"] =
        "{sender.alias_name}: In order for the bot to join your chat, you need to send {default.prefix}join directly in %s chat.",
        ["rejoined"] = "{sender.alias_name}: I have rejoined your chat room!",
        ["already_in"] = "{sender.alias_name}: I'm already in your chat room!",
        ["chat_response"] =
        "Hi, I'm %s and I'll be serving this chat. Send {default.prefix}help to learn about the commands!",
        ["join"] = "{sender.alias_name}: Successfully joined your chat room!",
    },
    russian = {
        ["command_unavailable"] = "{sender.alias_name}: Эта команда недоступна.",
        ["user_not_found"] = "{sender.alias_name}: Пользователь %s не найден",
        ["join_not_allowed"] = "{sender.alias_name}: Этот бот не может заходить в чаты!",
        ["join_from_bot_channel"] =
        "{sender.alias_name}: Вам нужно отправить {default.prefix}join прямо в чат %s, чтобы бот мог зайти к Вам в чат",
        ["rejoined"] = "{sender.alias_name}: Я перезашёл в этот чат!",
        ["already_in"] = "{sender.alias_name}: Я уже в этом чате!",
        ["chat_response"] =
        "Привет, я %s и я буду обслуживать этот чат. Отправьте {default.prefix}help, чтобы узнать больше о командах!",
        ["join"] = "{sender.alias_name}: Успешно зашёл в чат!",
    },
}

return {
    name = "join",
    description = "Add the bot to your channel.",
    delay_sec = 1,
    options = {},
    aliases = {},
    subcommands = { "silent" },
    minimal_rights = "user",
    handle = function(request)
        local cfg = bot_config()
        if cfg == nil then
            return l10n_custom_formatted_line_request(request, lines, "command_unavailable", {})
        end

        if not cfg.commands.join_allowed then
            return l10n_custom_formatted_line_request(request, lines, "join_not_allowed", {})
        end

        local channel_name = request.sender.alias_name
        local channel_id = request.sender.alias_id
        local silent_mode = false

        if request.message ~= nil and
            cfg.owner.id ~= nil and
            request.sender.alias_id == cfg.owner.id
        then
            local users = twitch_get_users({ logins = { request.message } })

            if #users == 0 then
                return l10n_custom_formatted_line_request(request, lines, "user_not_found", { request.message })
            end

            local user = users[1]

            channel_name = user.login
            channel_id = user.id

            silent_mode = request.subcommand_id ~= nil and request.subcommand_id == "silent"
        end

        if not cfg.commands.join_allow_from_ther_chats and request.channel.alias_name ~= bot_username() then
            return l10n_custom_formatted_line_request(request, lines, "join_from_bot_channel", { bot_username() })
        end

        local db_channels = db_query('SELECT id, alias_name, opted_out_at FROM channels WHERE alias_id = $1',
            { channel_id })

        if #db_channels > 0 then
            local db_channel = db_channels[1]

            if db_channel.opted_out_at ~= nil then
                print(db_channel)
                print(db_channel.id)
                print(type(db_channel.opted_out_at))
                db_execute('UPDATE channels SET opted_out_at = NULL WHERE id = $1', { db_channel.id })

                irc_join_channel(db_channel.alias_name)
                irc_send_message(
                    channel_name,
                    l10n_custom_formatted_line_request(request, lines, "chat_response", { bot_username() })
                )
                return l10n_custom_formatted_line_request(request, lines, "rejoined", {})
            end

            return l10n_custom_formatted_line_request(request, lines, "already_in", {})
        end

        db_execute('INSERT INTO channels(alias_id, alias_name) VALUES ($1, $2)',
            { channel_id, channel_name })

        irc_join_channel(channel_name)

        if not silent_mode then
            irc_send_message(
                channel_name,
                l10n_custom_formatted_line_request(request, lines, "chat_response", { bot_username() })
            )
        else
            local db_channel = db_query('SELECT id FROM channels WHERE alias_id = $1', { channel_id })[1]
            db_execute('INSERT IGNORE INTO channel_preferences(id, silent_mode) VALUES ($1, $2)',
                { db_channel.id, silent_mode })
        end

        return l10n_custom_formatted_line_request(request, lines, "join", {})
    end
}