Skip to content

Commit

Permalink
add roles to resolved interaction data
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkrissym committed Sep 26, 2024
1 parent 0a30269 commit 377a970
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
8 changes: 3 additions & 5 deletions Discord.C++/InteractionData.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#include "InteractionData.h"

#include "Logger.h"

DiscordCPP::InteractionData::InteractionData(const json& data, const std::string& token)
: 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(InteractionDataOptionHelper::interaction_data_option_from_json(option));
}
}
guild_id = get_optional<std::string>(data, "guild_id");
if (has_value(data, "resolved")) {
resolved_data = InteractionResolvedData(data.at("resolved"), guild_id.value_or(""), token);
}
custom_id = get_optional<std::string>(data, "custom_id");
target_id = get_optional<std::string>(data, "target_id");
// component_type
Expand Down
10 changes: 8 additions & 2 deletions Discord.C++/InteractionResolvedData.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "InteractionResolvedData.h"

DiscordCPP::InteractionResolvedData::InteractionResolvedData(const json& data, const std::string& token) {
#include "Role.h"

DiscordCPP::InteractionResolvedData::InteractionResolvedData(const json& data, const std::string& guild_id, const std::string& token) {
if (has_value(data, "users")) {
for (auto [key, value] : data["users"].items()) {
users.insert(std::make_pair(key, User(value, token)));
Expand All @@ -13,7 +15,11 @@ DiscordCPP::InteractionResolvedData::InteractionResolvedData(const json& data, c
}
}

// roles
if (has_value(data, "roles")) {
for (auto [key, value] : data["roles"].items()) {
roles.insert(std::make_pair(key, Role(value, guild_id, token)));
}
}

if (has_value(data, "channels")) {
for (auto [key, value] : data["channels"].items()) {
Expand Down
11 changes: 9 additions & 2 deletions Discord.C++/InteractionResolvedData.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#pragma once

#include <map>
#include <string>

#include "Channel.h"
#include "ChannelHelper.h"
#include "Member.h"
#include "Message.h"
#include "Role.h"

namespace DiscordCPP {

class InteractionResolvedData {
private:
/// Map of user ids and user objects.
Expand All @@ -16,7 +19,8 @@ class InteractionResolvedData {
/// Map of user ids and member objects.
std::map<std::string, Member> members;

// roles
/// Map of role ids and role objects.
std::map<std::string, Role> roles;

/// Map of channel ids and channel objects.
std::map<std::string, ChannelVariant> channels;
Expand All @@ -27,14 +31,17 @@ class InteractionResolvedData {
// attachments

public:
DLL_EXPORT InteractionResolvedData(const json& data, const std::string& token);
DLL_EXPORT InteractionResolvedData(const json& data, const std::string& guild_id, const std::string& token);

/// @return Map of user ids and user objects.
DLL_EXPORT std::map<std::string, User> get_users() { return users; }

/// @return Map of user ids and member objects.
DLL_EXPORT std::map<std::string, Member> get_members() { return members; }

/// @return Map of role ids and role objects.
std::map<std::string, Role> get_roles() { return roles; }

/// @return Map of channel ids and channel objects.
DLL_EXPORT std::map<std::string, ChannelVariant> get_channels() { return channels; }

Expand Down
4 changes: 0 additions & 4 deletions test_bot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#include <thread>
#include <vector>

#include "ApplicationCommand.h"
#include "ApplicationCommandOption.h"
#include "InteractionData.h"

#ifndef _WIN32
#include <cstdlib>
#endif
Expand Down

0 comments on commit 377a970

Please sign in to comment.