-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSticker.cpp
309 lines (275 loc) · 7.85 KB
/
Sticker.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "Core/Bot.h"
#include <cmath>
#include "Command.h"
#include <nonstd/expected.hpp>
#include "../Data/Lang.h"
#include "png.h"
#include <CImg.h>
using namespace B12;
namespace
{
constexpr auto sticker_formats = std::to_array<std::pair<dpp::sticker_format, std::string_view>>(
{
{dpp::sf_png, "png"sv},
{dpp::sf_apng, "png"sv},
{dpp::sf_gif, "gif"sv}
}
);
constexpr auto find_sticker_format = [](
dpp::sticker_format format
) constexpr -> std::optional<std::string_view>
{
constexpr auto proj = [](const std::iter_value_t<decltype(sticker_formats)>& n)
{
return (n.first);
};
if (auto it = std::ranges::find(sticker_formats, format, proj); it != std::end(sticker_formats))
return {it->second};
return {std::nullopt};
};
auto message_get(
dpp::snowflake message_id,
dpp::snowflake channel_id
) -> std::optional<dpp::message>
{
std::optional<dpp::message> message{std::nullopt};
auto message_getter = AsyncExecutor<dpp::message>(
[&](const dpp::message& m)
{
message = m;
}
);
message_getter(&dpp::cluster::message_get, &Bot::bot(), message_id, channel_id);
message_getter.wait();
return (message);
}
auto sticker_get(dpp::snowflake id)
{
std::optional<dpp::sticker> ret;
auto sticker_retriever = AsyncExecutor<dpp::sticker>{
[&](const dpp::sticker& s0)
{
ret = s0;
}
};
sticker_retriever(&dpp::cluster::nitro_sticker_get, &Bot::bot(), id).wait();
return (ret);
}
auto sticker_content_get(const dpp::sticker& sticker)
{
auto content = std::optional<std::string>{std::nullopt};
auto request =
[](dpp::cluster* bot, const std::string& url, const dpp::http_completion_event& callback)
{
bot->request(url, dpp::m_get, callback);
};
auto content_retriever =
AsyncExecutor<dpp::http_request_completion_t, dpp::http_request_completion_t>{
[&](const dpp::http_request_completion_t& result)
{
if (result.status != 200)
return;
content = result.body;
}
};
content_retriever(request, &Bot::bot(), sticker.get_url());
content_retriever.wait();
return (content);
}
auto sticker_add(dpp::sticker& sticker) -> nonstd::expected<dpp::sticker, dpp::error_info>
{
auto ret = nonstd::expected<dpp::sticker, dpp::error_info>{};
auto on_error = [&](const dpp::error_info& err)
{
ret = nonstd::make_unexpected(err);
};
auto executor = AsyncExecutor<dpp::sticker>{
[&](const dpp::sticker& result)
{
ret = result;
},
on_error
};
executor(&dpp::cluster::guild_sticker_create, &Bot::bot(), sticker);
executor.wait();
return (ret);
}
void attachment_add(dpp::message& msg, const dpp::sticker& sticker, const std::string& content)
{
if (std::optional format = find_sticker_format(sticker.format_type); format.has_value())
{
msg.add_file(fmt::format("{}.{}", sticker.name, *format), content, fmt::format("image/{}", *format));
}
else
msg.add_file(sticker.name, content);
msg.set_file_content(content);
}
enum class ImageProcessResult
{
ERROR = 0,
RESIZED = 1,
NOOP = 2
};
auto image_process(std::optional<std::string>& content) -> ImageProcessResult
{
using enum ImageProcessResult;
constexpr auto MAX_SIZE = 300;
constexpr auto MAX_SIZE_F = static_cast<float>(MAX_SIZE);
//auto file = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
//auto info = png_create_info_struct(file);
//setjmp(png_jmpbuf(file));
png_image image{};
image.version = PNG_IMAGE_VERSION;
image.format = PNG_FORMAT_RGBA;
png_image_begin_read_from_memory(&image, content->data(), content->size());
if (image.width <= MAX_SIZE && image.height <= MAX_SIZE)
{
png_image_finish_read(&image, nullptr, nullptr, 0, nullptr);
return (NOOP);
}
auto og_size = PNG_IMAGE_SIZE(image);
auto buffer = std::make_unique<float[]>(og_size / sizeof(float));
if (png_image_finish_read(&image, 0, buffer.get(), 0, nullptr) == 0)
return (ERROR);
auto og_cimg = cimg_library::CImg<float>{buffer.get(), image.width, image.height};
uint32_t w{MAX_SIZE};
uint32_t h{MAX_SIZE};
if (image.width != image.height)
{
float wf = static_cast<float>(image.width);
float hf = static_cast<float>(image.height);
if (image.width > image.height)
{
hf = (hf / wf) * MAX_SIZE_F;
wf = MAX_SIZE_F;
}
else if (image.height > image.width)
{
wf = (hf / wf) * MAX_SIZE_F;
hf = MAX_SIZE_F;
}
w = static_cast<uint32_t>(floorf(wf));
h = static_cast<uint32_t>(floorf(hf));
}
image.width = w;
image.height = h;
std::string out;
png_alloc_size_t out_size = PNG_IMAGE_SIZE(image);
auto resized = og_cimg.get_resize(w, h, -100. - 100, 6);
out.resize(out_size);
if (!png_image_write_to_memory(
&image,
out.data(),
&out_size,
0,
resized.data(),
w * sizeof(*resized.data()),
nullptr
))
return (ERROR);
out.resize(out_size);
content = std::move(out);
return (RESIZED);
}
}
template <>
void Bot::command<"server sticker grab">(
const dpp::interaction_create_t& e,
const dpp::interaction& interaction,
command_option_view options
)
{
dpp::snowflake channel_id{interaction.channel_id};
dpp::snowflake message_id{0};
for (const dpp::command_data_option& opt : options)
{
if (opt.type == dpp::co_mentionable && opt.name == "channel")
{
channel_id = std::get<dpp::snowflake>(opt.value);
}
else if (opt.type == dpp::co_string && opt.name == "message")
{
auto value = std::get<std::string>(opt.value);
message_id = std::stoull(value);
}
}
if (!message_id)
{
e.reply(dpp::message("Error: invalid message ID").set_flags(dpp::m_ephemeral));
return;
}
auto message = message_get(message_id, channel_id);
if (!message)
{
e.reply(dpp::message("Error: message not found").set_flags(dpp::m_ephemeral));
return;
}
if (message->stickers.empty())
{
e.reply(dpp::message("Error: message does not have stickers!").set_flags(dpp::m_ephemeral));
return;
}
auto think = AsyncExecutor<dpp::confirmation>(shion::noop);
std::mutex message_mutex;
dpp::message ret;
think(&dpp::interaction_create_t::thinking, &e, false);
for (const dpp::sticker& s : message->stickers)
{
auto content = sticker_content_get(s);
if (!content)
{
ret.content.append(fmt::format("{} Could not download image data", lang::ERROR_EMOJI));
continue;
}
auto grabbed_sticker = sticker_get(s.id);
if (!grabbed_sticker)
{
ret.content.append(
fmt::format(
"{} Could not request sticker details, was it deleted? Adding image as attachment for manual addition.",
lang::ERROR_EMOJI
)
);
attachment_add(ret, s, *content);
continue;
}
dpp::sticker to_add;
auto resize_result = image_process(content);
to_add.filecontent = std::move(*content);
to_add.guild_id = interaction.guild_id;
to_add.sticker_user = bot().me;
to_add.name = grabbed_sticker->name;
to_add.description = grabbed_sticker->description;
to_add.filename = to_add.name + ".png";
to_add.tags = grabbed_sticker->tags;
to_add.type = dpp::st_guild;
std::ranges::replace(to_add.filename, ' ', '_');
auto add_result = sticker_add(to_add);
if (!add_result.has_value())
{
ret.content.append(
fmt::format(
"{} Failed to add sticker `{}`: \"{}\"\nAdding image as attachment for manual addition",
lang::ERROR_EMOJI,
grabbed_sticker->name,
add_result.error().message
)
);
attachment_add(ret, s, to_add.filecontent);
}
else
{
ret.content.append(
fmt::format(
"{} Added sticker \"{}\"!",
"\u2705",
grabbed_sticker->name
)
);
}
if (resize_result == ImageProcessResult::RESIZED)
ret.content.append(" (note : the image was resized due to being too large)");
}
think.wait();
e.edit_original_response(ret);
}