Debian Codegraph @

docpages/example_code/record_user.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2 #include <iomanip>
3 #include <sstream>
4
5 int main() {
6 /* Example to record a user in a VC
7 *
8 * Recording is output as './me.pcm' and you can play it via the soundboard example
9 * or use ffmpeg 'ffplay -f s16le -ar 48000 -ac 2 -i ./me.pcm'
10 */
11
12 /* Replace with the user's id you wish to record */
13 dpp::snowflake user_id = 407877550216314882;
14
15 /* Setup the bot */
16 dpp::cluster bot("token");
17
18 FILE *fd;
19 fd = fopen("./me.pcm", "wb");
20
21 bot.on_log(dpp::utility::cout_logger());
22
23 /* The event is fired when someone issues your commands */
24 bot.on_slashcommand([&fd](const dpp::slashcommand_t& event) {
25 /* Check which command they ran */
26 if (event.command.get_command_name() == "record") {
27 /* Get the guild */
28 dpp::guild* g = dpp::find_guild(event.command.guild_id);
29
30 /* Attempt to connect to a voice channel, returns false if we fail to connect. */
31 if (!g->connect_member_voice(*event.owner, event.command.get_issuing_user().id)) {
32 event.reply("You don't seem to be in a voice channel!");
33 return;
34 }
35
36 /* Tell the user we joined their channel. */
37 event.reply("Joined your channel, now recording!");
38 } else if (event.command.get_command_name() == "stop") {
39 event.from()->disconnect_voice(event.command.guild_id);
40 fclose(fd);
41
42 event.reply("Stopped recording.");
43 }
44 });
45
46 bot.on_voice_receive([&fd, &user_id](const dpp::voice_receive_t& event) {
47 if (event.user_id == user_id) {
48 fwrite((char *)event.audio, 1, event.audio_size, fd);
49 }
50 });
51
52 bot.on_ready([&bot](const dpp::ready_t& event) {
53 if (dpp::run_once<struct register_bot_commands>()) {
54 /* Create a new command. */
55 dpp::slashcommand recordcommand("record", "Joins your voice channel and records you.", bot.me.id);
56 dpp::slashcommand stopcommand("stop", "Stops recording you.", bot.me.id);
57
58 bot.global_bulk_command_create({ recordcommand, stopcommand });
59 }
60 });
61
62 /* Start bot */
63 bot.start(dpp::st_wait);
64
65 return 0;
66 }