Skip to content

Commit

Permalink
replace pointers for channels and interaction options
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkrissym committed Dec 2, 2023
1 parent c1350a4 commit 799c449
Show file tree
Hide file tree
Showing 19 changed files with 164 additions and 252 deletions.
32 changes: 2 additions & 30 deletions Discord.C++/Channel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Channel.h"

#include <variant>

#include "Discord.h"
#include "static.h"

Expand Down Expand Up @@ -31,38 +33,8 @@ DiscordCPP::Channel::Channel(const std::string& token)
: DiscordCPP::DiscordObject(token) {
}

DiscordCPP::Channel* DiscordCPP::Channel::from_json(Discord* client, const json& data, const std::string& token) {
switch (data.at("type").get<int>()) {
case Type::GUILD_TEXT:
case Type::GUILD_NEWS:
return (Channel*)new GuildChannel(data, token);
case Type::GUILD_VOICE:
return (Channel*)new VoiceChannel(client, data, token);
case Type::DM:
case Type::GROUP_DM:
return new DMChannel(data, token);
default:
return new Channel(data, token);
}
}

void DiscordCPP::Channel::delete_channel() {
std::string url = "/channels/" + get_id();

api_call(url, "DELETE");
}

DiscordCPP::Channel* DiscordCPP::Channel::copy() {
switch (type) {
case Type::GUILD_TEXT:
case Type::GUILD_NEWS:
return (Channel*)new GuildChannel(*(GuildChannel*)this);
case Type::GUILD_VOICE:
return (Channel*)new VoiceChannel(*(VoiceChannel*)this);
case Type::DM:
case Type::GROUP_DM:
return new DMChannel(*(DMChannel*)this);
default:
return new Channel(*this);
}
}
6 changes: 0 additions & 6 deletions Discord.C++/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,11 @@ class Channel : public DiscordObject {
DLL_EXPORT Channel(const json& data, const std::string& token);
DLL_EXPORT Channel(const std::string& id, const std::string& token);
DLL_EXPORT explicit Channel(const std::string& token);
DLL_EXPORT Channel(const Channel&) = default;
DLL_EXPORT Channel() = default;

DLL_EXPORT static Channel* from_json(Discord* client, const json& data, const std::string& token);

/// Delete this channel
DLL_EXPORT void delete_channel();

/// @returns a copy of this channel object
DLL_EXPORT Channel* copy();

/// @return Channelname as std::string
DLL_EXPORT explicit operator std::string() { return name; };

Expand Down
34 changes: 34 additions & 0 deletions Discord.C++/ChannelHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <variant>

#include "Channel.h"
#include "DMChannel.h"
#include "GuildChannel.h"
#include "VoiceChannel.h"

namespace DiscordCPP {
using ChannelVariant = std::variant<DMChannel, VoiceChannel, GuildChannel, Channel>;

class ChannelHelper {
public:
DLL_EXPORT static ChannelVariant channel_from_json(Discord* client, const json& data, const std::string& token) {
switch (data.at("type").get<int>()) {
case Channel::Type::GUILD_TEXT:
case Channel::Type::GUILD_NEWS:
return GuildChannel(data, token);
case Channel::Type::GUILD_VOICE:
return VoiceChannel(client, data, token);
case Channel::Type::DM:
case Channel::Type::GROUP_DM:
return DMChannel(data, token);
default:
return Channel(data, token);
}
}

DLL_EXPORT static std::string get_channel_id(const ChannelVariant& channel) {
return std::visit([](auto& c) { return c.get_id(); }, channel);
}
};
} // namespace DiscordCPP
1 change: 1 addition & 0 deletions Discord.C++/DMChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DMChannel : public TextChannel {
public:
DLL_EXPORT DMChannel(const json& data, const std::string& token);
DLL_EXPORT DMChannel(const std::string& id, const std::string& token);
DLL_EXPORT DMChannel() = default;

/// the recipients if the channel is DM
DLL_EXPORT std::vector<User> get_recipients() { return recipients; }
Expand Down
17 changes: 9 additions & 8 deletions Discord.C++/Discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <ctime>
#include <iostream>

#include "ChannelHelper.h"
#include "GuildChannel.h"
#include "static.h"

/** Creates a Discord instance with one or more shards
Expand Down Expand Up @@ -281,14 +283,14 @@ void DiscordCPP::Discord::handle_raw_event(const std::string &event_name,
} else if (event_name == "CHANNEL_CREATE") {
if (has_value(data, "guild_id")) {
std::string guild_id = data.at("guild_id").get<std::string>();
Channel *channel = Channel::from_json(this, data, get_token());
ChannelVariant channel = ChannelHelper::channel_from_json(this, data, get_token());

get_guild(guild_id)->_add_channel(channel);
}
} else if (event_name == "CHANNEL_UPDATE") {
if (has_value(data, "guild_id")) {
std::string guild_id = data.at("guild_id").get<std::string>();
Channel *channel = Channel::from_json(this, data, get_token());
ChannelVariant channel = ChannelHelper::channel_from_json(this, data, get_token());

get_guild(guild_id)->_update_channel(channel);
}
Expand All @@ -303,13 +305,12 @@ void DiscordCPP::Discord::handle_raw_event(const std::string &event_name,
if (has_value(data, "channel_id") &&
has_value(data, "last_pin_timestamp")) {
std::string channel_id = data.at("channel_id").get<std::string>();
std::string last_pin =
data.at("last_pin_timestamp").get<std::string>();
std::string last_pin = data.at("last_pin_timestamp").get<std::string>();

for (auto &_guild : _guilds) {
for (auto &channel : _guild->channels) {
if (channel->get_id() == channel_id) {
((TextChannel *)channel)->_set_last_pin_timestamp(last_pin);
if (ChannelHelper::get_channel_id(channel) == channel_id) {
std::get<GuildChannel>(channel)._set_last_pin_timestamp(last_pin);
return;
}
}
Expand Down Expand Up @@ -416,8 +417,8 @@ void DiscordCPP::Discord::handle_raw_event(const std::string &event_name,
for (auto &_guild : _guilds) {
if (_guild->get_id() == guild_id) {
for (auto &channel : _guild->channels) {
if (channel->get_id() == channel_id) {
((TextChannel *)channel)->_set_last_message_id(msg_id);
if (ChannelHelper::get_channel_id(channel) == channel_id) {
std::get<GuildChannel>(channel)._set_last_message_id(msg_id);
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions Discord.C++/Discord.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "ApplicationCommand.h"
#include "AudioSource.h"
#include "Channel.h"
#include "ChannelHelper.h"
#include "DMChannel.h"
#include "DiscordObject.h"
#include "Embed.h"
Expand Down
2 changes: 0 additions & 2 deletions Discord.C++/DiscordObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class DiscordObject {
DLL_EXPORT explicit DiscordObject(std::string token);
/// @param[in] token Discord token
DLL_EXPORT explicit DiscordObject(std::string token, std::string id);
/// @param[in] old the Channel to copy
DLL_EXPORT DiscordObject(const DiscordObject& old) = default;

/// @return the id of the object
DLL_EXPORT [[nodiscard]] std::string get_id() const { return id; }
Expand Down
55 changes: 7 additions & 48 deletions Discord.C++/Guild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>

#include "ChannelHelper.h"
#include "DMChannel.h"
#include "TextChannel.h"
#include "VoiceChannel.h"
Expand Down Expand Up @@ -55,7 +56,7 @@ DiscordCPP::Guild::Guild(Discord* client, const json& data, const std::string& t

if (has_value(data, "channels")) {
for (json channel : data.at("channels")) {
channels.push_back(Channel::from_json(client, channel, token));
channels.push_back(ChannelHelper::channel_from_json(client, channel, token));
}
}

Expand All @@ -69,55 +70,14 @@ DiscordCPP::Guild::Guild(Discord* client, const std::string& id, const std::stri
*this = Guild(client, api_call("/guilds/" + id), token);
}

DiscordCPP::Guild::Guild(const Guild& old)
: DiscordCPP::DiscordObject(old) {
name = old.name;
icon = old.icon;
splash = old.splash;
owner = old.owner;
permissions = old.permissions;
region = old.region;
afk_channel = old.afk_channel;
afk_timeout = old.afk_timeout;
embed_enabled = old.embed_enabled;
embed_channel = old.embed_channel;
verification_level = old.verification_level;
default_message_notifications = old.default_message_notifications;
explicit_content_filter = old.explicit_content_filter;
// roles
// emojis
features = old.features;
mfa_level = old.mfa_level;
application_id = old.application_id;
widget_enabled = old.widget_enabled;
widget_channel = old.widget_channel;
system_channel = old.system_channel;
joined_at = old.joined_at;
large = old.large;
unavailable = old.unavailable;
member_count = old.member_count;
// voice_states
members = old.members;
for (auto channel : old.channels) {
channels.push_back(channel->copy());
}
// presences
}

DiscordCPP::Guild::~Guild() {
for (auto& channel : channels) {
delete channel;
}
}

void DiscordCPP::Guild::_add_channel(Channel* channel) {
void DiscordCPP::Guild::_add_channel(ChannelVariant channel) {
channels.push_back(channel);
}

void DiscordCPP::Guild::_update_channel(Channel* channel) {
void DiscordCPP::Guild::_update_channel(ChannelVariant channel) {
std::string channel_id = ChannelHelper::get_channel_id(channel);
for (size_t i = 0; i < channels.size(); i++) {
if (channels[i]->get_id() == channel->get_id()) {
delete channels[i];
if (ChannelHelper::get_channel_id(channels[i]) == channel_id) {
channels.erase(channels.begin() + i);
channels.push_back(channel);
break;
Expand All @@ -127,8 +87,7 @@ void DiscordCPP::Guild::_update_channel(Channel* channel) {

void DiscordCPP::Guild::_remove_channel(const std::string& channel_id) {
for (size_t i = 0; i < channels.size(); i++) {
if (channels[i]->get_id() == channel_id) {
delete channels[i];
if (ChannelHelper::get_channel_id(channels[i]) == channel_id) {
channels.erase(channels.begin() + i);
break;
}
Expand Down
25 changes: 9 additions & 16 deletions Discord.C++/Guild.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
#include <string>

#include "Channel.h"
#include "ChannelHelper.h"
#include "DiscordObject.h"
#include "GuildChannel.h"
#include "Member.h"
#include "User.h"
#include "VoiceChannel.h"

#ifdef _WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT
#endif

namespace DiscordCPP {

namespace VerificationLevel {
Expand Down Expand Up @@ -84,8 +79,8 @@ class Guild : public DiscordObject {
int default_message_notifications = 0;
/// ExplicitContentFilterLevel
int explicit_content_filter = 0;
// vector<Role *> roles;
// vector<Emoji *> emojis;
// vector<Role> roles;
// vector<Emoji> emojis;
/// enabled guild features
std::vector<std::string> features;
/// MFALevel
Expand Down Expand Up @@ -113,11 +108,11 @@ class Guild : public DiscordObject {
/// users in the guild
std::vector<Member> members;
/// channels of the guild
std::vector<Channel*> channels;
// vector<Presence *> presences;
std::vector<ChannelVariant> channels;
// vector<Presence> presences;

DLL_EXPORT void _add_channel(Channel* channel);
DLL_EXPORT void _update_channel(Channel* channel);
DLL_EXPORT void _add_channel(ChannelVariant channel);
DLL_EXPORT void _update_channel(ChannelVariant channel);
DLL_EXPORT void _remove_channel(const std::string& channel_id);
DLL_EXPORT void _add_member(Member member);
DLL_EXPORT void _update_member(Member member);
Expand All @@ -128,11 +123,9 @@ class Guild : public DiscordObject {
public:
DLL_EXPORT Guild(Discord* client, const json& data, const std::string& token);
DLL_EXPORT Guild(Discord* client, const std::string& id, const std::string& token);
DLL_EXPORT Guild(const Guild& old);
DLL_EXPORT Guild() = default;
DLL_EXPORT ~Guild();

///@return Guildname as std::string
///@return Guild name as std::string
DLL_EXPORT explicit operator std::string() { return name; };

/// leave this guild
Expand Down Expand Up @@ -208,7 +201,7 @@ class Guild : public DiscordObject {
/// @return users in the guild
DLL_EXPORT std::vector<Member> get_members() { return members; }
/// @return channels of the guild
DLL_EXPORT std::vector<Channel*> get_channels() { return channels; }
DLL_EXPORT std::vector<ChannelVariant> get_channels() { return channels; }
};

} // namespace DiscordCPP
30 changes: 4 additions & 26 deletions Discord.C++/InteractionData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#include "Logger.h"

DiscordCPP::InteractionData::InteractionData(const json& data, const std::string& token)
: DiscordCPP::DiscordObject(token, data.at("id").get<std::string>()) {
data.at("name").get_to<std::string>(name);
type = static_cast<ApplicationCommand::Type>(data.at("type").get<int>());
: DiscordCPP::DiscordObject(token, data.at("id").get<std::string>()),
name(data.at("name").get<std::string>()),
type(static_cast<ApplicationCommand::Type>(data.at("type").get<int>())) {
if (has_value(data, "resolved")) {
resolved_data = InteractionResolvedData(data.at("resolved"), token);
}
if (has_value(data, "options")) {
for (const json& option : data.at("options")) {
options.push_back(InteractionDataOption::from_json(option));
options.push_back(InteractionDataOptionHelper::interaction_data_option_from_json(option));
}
}
guild_id = get_optional<std::string>(data, "guild_id");
Expand All @@ -21,25 +21,3 @@ DiscordCPP::InteractionData::InteractionData(const json& data, const std::string
// values
// components
}

DiscordCPP::InteractionData::InteractionData(const InteractionData& other)
: DiscordCPP::DiscordObject(other) {
name = other.name;
type = other.type;
resolved_data = other.resolved_data;
for (auto option : other.options) {
options.push_back(option->copy());
}
guild_id = other.guild_id;
custom_id = other.custom_id;
// component_type
// values
target_id = other.target_id;
// components
}

DiscordCPP::InteractionData::~InteractionData() {
for (auto& option : options) {
delete option;
}
}
Loading

0 comments on commit 799c449

Please sign in to comment.