Debian Codegraph @

src/dpp/events/voice_state_update.cpp

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 * Copyright 2021 Craig Edwards and D++ contributors
7 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 ************************************************************************************/
22 #include <dpp/discordevents.h>
23 #include <dpp/cluster.h>
24 #include <dpp/discordvoiceclient.h>
25 #include <dpp/guild.h>
26 #include <dpp/voicestate.h>
27 #include <dpp/stringops.h>
28 #include <dpp/json.h>
29
30
31
32 namespace dpp::events {
33 /**
34 * @brief Handle event
35 *
36 * @param client Websocket client (current shard)
37 * @param j JSON data for the event
38 * @param raw Raw JSON string
39 */
40 void voice_state_update::handle(discord_client* client, json &j, const std::string &raw) {
41
42 json& d = j["d"];
43 dpp::voice_state_update_t vsu(client->owner, client->shard_id, raw);
44 vsu.state = dpp::voicestate().fill_from_json(&d);
45 vsu.state.shard_id = client->shard_id;
46
47 /* Update guild voice states */
48 dpp::guild* g = dpp::find_guild(vsu.state.guild_id);
49 if (g) {
50 if (vsu.state.channel_id.empty()) {
51 auto ve = g->voice_members.find(vsu.state.user_id);
52 if (ve != g->voice_members.end()) {
53 g->voice_members.erase(ve);
54 }
55 } else {
56 g->voice_members[vsu.state.user_id] = vsu.state;
57 }
58
59 if (client->creator->cache_policy.user_policy != dpp::cp_none) {
60 if (d.contains("member")) {
61 auto& member = d["member"];
62 guild_member m;
63 m.fill_from_json(&member, g->id, vsu.state.user_id);
64 g->members[m.user_id] = m;
65 }
66 }
67 }
68
69 if (vsu.state.user_id == client->creator->me.id) {
70 auto current_vc = client->get_voice(vsu.state.guild_id);
71 if (vsu.state.channel_id.empty()) {
72 /* Instruction to disconnect from vc */
73 client->disconnect_voice_internal(vsu.state.guild_id, false);
74 } else if (current_vc && vsu.state.channel_id != current_vc->channel_id) {
75 /* When there is a active vc and will be moved to a different channel (by user) */
76 /* Instruction to connect to a different channel */
77 std::shared_lock lock(client->voice_mutex);
78 auto v = client->connecting_voice_channels.find(vsu.state.guild_id);
79 if (v != client->connecting_voice_channels.end()) {
80 auto enable_dave = v->second->dave;
81 client->connecting_voice_channels.erase(v);
82 client->connecting_voice_channels[vsu.state.guild_id] = std::make_shared<voiceconn>(client,
83 [](discord_client* client) {
84 /* do nothing, discord will handle reconnection*/
85 }, vsu.state.guild_id, vsu.state.channel_id, enable_dave);
86 /* Should set the session_id here, did this ever work before without setting session_id?? */
87 client->connecting_voice_channels[vsu.state.guild_id]->session_id = vsu.state.session_id;
88 }
89 } else {
90 std::shared_lock lock(client->voice_mutex);
91 auto v = client->connecting_voice_channels.find(vsu.state.guild_id);
92 /* Check to see if we have a connection to a voice channel in progress on this guild */
93 if (v != client->connecting_voice_channels.end()) {
94 voiceconn& connection = *v->second;
95 connection.session_id = vsu.state.session_id;
96 if (connection.is_ready() && !connection.is_active()) {
97 connection.connect();
98 }
99 }
100 }
101 }
102
103 if (!client->creator->on_voice_state_update.empty()) {
104 client->creator->queue_work(1, [c = client->creator, vsu]() {
105 c->on_voice_state_update.call(vsu);
106 });
107 }
108 }
109
110 };