summaryrefslogtreecommitdiff
path: root/luamods/followlist.lua
blob: 5a3f6bd9a910927b1bc50a5919ee19893d862b7a (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
local lines = {
	english = {
		["command_unavailable"] = "{sender.alias_name}: This command is not available.",
		["external_api_error"] = "{sender.alias_name}: Failed to get followlist for %s. Try again later. (%s)",
		["success"] = "{sender.alias_name}: %s",
	},
	russian = {
		["command_unavailable"] = "{sender.alias_name}: Эта команда недоступна.",
		["external_api_error"] = "{sender.alias_name}: Не удалось получить фолловлист %s. Попробуйте позже. (%s)",
		["success"] = "{sender.alias_name}: %s",
	},
}

return {
	name = "followlist",
	description = [[
Read the user's follows as plain text.
After collecting the list of chatters, the bot returns a link to the paste from
[the Pastebin-like service](https://paste.alright.party).

## Syntax

`!followlist <username>`

+ `<username>` - Twitch username *(optional)*.

## Responses
+ `https://paste.alright.party/XXXXX.txt`
]],
	delay_sec = 5,
	options = {},
	subcommands = {},
	aliases = { "flist", "follows" },
	minimal_rights = "user",
	handle = function(request)
		cfg = bot_config()
		if cfg == nil then
			return l10n_custom_formatted_line_request(request, lines, "command_unavailable", {})
		end

		if
			cfg.url.paste_service == nil
			or cfg.commands.paste_path == nil
			or cfg.commands.paste_body_name == nil
			or cfg.commands.paste_title_name == nil
		then
			return l10n_custom_formatted_line_request(request, lines, "command_unavailable", {})
		end

		username = request.sender.alias_name

		if request.message ~= nil then
			username = request.message
		end

		response =
			net_get_with_headers("https://tools.2807.eu/api/getfollows/" .. username, { Accept = "application/json" })

		if response.code ~= 200 then
			return l10n_custom_formatted_line_request(request, lines, "external_api_error", { username, response.code })
		end

		follows = json_parse(response.text)

		body = #follows .. " channels\r\n---------------------\r\n\r\n"

		for i = 1, #follows, 1 do
			follow = follows[i]
			follow_timestamp = time_parse(follow.followedAt, "%Y-%m-%dT%H:%M:%SZ")
			follow_diff = time_humanize(time_current() - follow_timestamp)
			body = body .. follow.login .. " (" .. follow_diff .. " ago)" .. "\r\n"
		end

		time = time_format(time_current(), "%d.%m.%Y %H:%M:%S %z")

		response = net_post_multipart_with_headers(cfg.url.paste_service, {
			[cfg.commands.paste_body_name] = body,
			[cfg.commands.paste_title_name] = username .. "'s followlist on " .. time,
		}, {
			Accept = "application/json",
		})

		if response.code ~= 201 and response.code ~= 200 then
			return l10n_custom_formatted_line_request(request, lines, "external_api_error", { response.code })
		end

		body = json_parse(response.text)

		link = json_get_value(body, cfg.commands.paste_path)
		if link == nil then
			return l10n_custom_formatted_line_request(request, lines, "command_unavailable", {})
		end

		return l10n_custom_formatted_line_request(request, lines, "success", { link })
	end,
}