Now tracks and caches all (I think) required data

This commit is contained in:
2016-08-05 02:27:46 +01:00
parent 1b2ea3c6bf
commit 47de861f45
8 changed files with 718 additions and 116 deletions

View File

@ -32,6 +32,7 @@ namespace DiscordObjects {
Channel(json data);
void load_from_json(json data);
std::string to_debug_string();
bool operator==(Channel rhs);
@ -73,6 +74,19 @@ namespace DiscordObjects {
user_limit = data.value("user_limit", -1);
}
inline std::string Channel::to_debug_string() {
return "**__Channel " + id + "__**"
+ "\n**guild_id:** " + guild_id
+ "\n**name:** " + name
+ "\n**type:** " + type
+ "\n**position:** " + std::to_string(position)
+ "\n**is_private:** " + std::to_string(is_private)
+ "\n**topic:** " + (topic == "" ? "[empty]" : topic)
+ "\n**last_message_id:** " + last_message_id
+ "\n**bitrate:** " + std::to_string(bitrate)
+ "\n**user_limit:** " + std::to_string(user_limit);
}
inline bool Channel::operator==(Channel rhs) {
return id == rhs.id && id != "null";
}

View File

@ -1,13 +1,15 @@
#ifndef BOT_DATA__STRUCTURES_Guild
#define BOT_DATA__STRUCTURES_Guild
#ifndef BOT_DATA__STRUCTURES_GUILD
#define BOT_DATA__STRUCTURES_GUILD
#include <string>
#include <memory>
#include <vector>
#include "../json/json.hpp"
#include "Channel.hpp"
#include "User.hpp"
#include "Role.hpp"
#include "GuildMember.hpp"
using json = nlohmann::json;
@ -48,6 +50,7 @@ namespace DiscordObjects {
Guild(json data);
void load_from_json(json data);
std::string to_debug_string();
bool operator==(Guild rhs);
@ -62,13 +65,15 @@ namespace DiscordObjects {
// bool embed_enabled;
// std::string embed_channel_id;
int verification_level;
// TODO: Implement all guil fields
// TODO: Implement all guild fields
// std::vector<?> voice_states
// std::vector<?> roles
// std::vector<?> emojis
// std::vector<?> features
bool unavailable;
std::vector<std::shared_ptr<Channel>> channels;
std::vector<Channel *> channels;
std::vector<GuildMember> members;
std::vector<Role *> roles;
//std::vector<std::unique_ptr<DiscordObjects::User>> users;
};
@ -93,6 +98,23 @@ namespace DiscordObjects {
afk_channel_id = data.value("afk_channel_id", "null");
afk_timeout = data.value("afk_timeout", -1);
verification_level = data.value("verification_level", -1);
unavailable = data.value("unavailable", false);
}
inline std::string Guild::to_debug_string() {
return "**__Guild " + id + "__**"
+ "\n**name:** " + name
+ "\n**icon:** " + icon
+ "\n**splash:** " + splash
+ "\n**owner_id:** " + owner_id
+ "\n**region:** " + region
+ "\n**afk_channel_id:** " + afk_channel_id
+ "\n**afk_timeout:** " + std::to_string(afk_timeout)
+ "\n**verification_level:** " + std::to_string(verification_level)
+ "\n**unavailable:** " + std::to_string(unavailable)
+ "\n**channels:** " + std::to_string(channels.size())
+ "\n**roles:** " + std::to_string(roles.size())
+ "\n**members:** " + std::to_string(members.size());
}
inline bool Guild::operator==(Guild rhs) {

View File

@ -0,0 +1,68 @@
#ifndef BOT_DATA__STRUCTURES_GUILDMEMBER
#define BOT_DATA__STRUCTURES_GUILDMEMBER
#include <string>
#include <vector>
#include "../json/json.hpp"
#include "User.hpp"
#include "Role.hpp"
namespace DiscordObjects {
class GuildMember {
public:
GuildMember();
GuildMember(json data, User *user);
void load_from_json(json data);
std::string to_debug_string();
bool operator==(GuildMember rhs);
User *user;
std::string nick;
std::vector<Role *> roles;
std::string joined_at; // TODO: better type
bool deaf;
bool mute;
};
inline GuildMember::GuildMember() {
user = nullptr;
nick = "null";
joined_at = "null";
deaf = false;
mute = false;
}
inline GuildMember::GuildMember(json data, User *user) {
this->user = user;
load_from_json(data);
}
inline void GuildMember::load_from_json(json data) {
nick = data.value("nick", "null");
joined_at = data.value("joined_at", "null");
deaf = data.value("deaf", false);
mute = data.value("mute", false);
}
inline std::string GuildMember::to_debug_string() {
return "**__GuildMember " + user->id + "__**"
+ "\n**mention:** <@" + user->id + "> / " + user->username + "#" + user->discriminator
+ "\n**bot:** " + std::to_string(user->bot)
+ "\n**mfa_enabled:** " + std::to_string(user->mfa_enabled)
+ "\n**avatar:** " + user->avatar
+ "\n**nick:** " + nick
+ "\n**joined_at:** " + joined_at
+ "\n**deaf:** " + std::to_string(deaf)
+ "\n**mute:** " + std::to_string(mute);
}
inline bool GuildMember::operator==(GuildMember rhs) {
return user->id == rhs.user->id;
}
}
#endif

View File

@ -0,0 +1,118 @@
#ifndef BOT_DATA__STRUCTURES_ROLE
#define BOT_DATA__STRUCTURES_ROLE
#include <string>
#include <sstream>
#include <iomanip>
#include "../json/json.hpp"
using json = nlohmann::json;
namespace DiscordObjects {
class Role {
public:
Role();
Role(json data);
void load_from_json(json data);
std::string to_debug_string();
bool operator==(Role rhs);
std::string id;
std::string name;
int colour;
bool hoist;
int position;
int permissions;
bool managed;
bool mentionable;
};
inline Role::Role() {
id = "null";
name = "null";
colour = -1;
hoist = false;
position = -1;
permissions = 0;
managed = false;
mentionable = false;
}
inline Role::Role(json data) {
load_from_json(data);
}
inline void Role::load_from_json(json data) {
id = data.value("id", "null");
name = data.value("name", "null");
colour = data.value("color", -1);
hoist = data.value("hoist", false);
position = data.value("position", -1);
permissions = data.value("permissions", 0);
managed = data.value("managed", false);
mentionable = data.value("mentionable", false);
}
inline std::string Role::to_debug_string() {
std::stringstream colour_ss;
colour_ss << std::setw(6) << std::setfill('0') << colour;
std::string colour_str = colour_ss.str();
return "**__Role " + id + "__**"
+ "\n**name:** " + name
+ "\n**colour:** #" + colour_str
+ "\n**hoist:** " + std::to_string(hoist)
+ "\n**position:** " + std::to_string(position)
+ "\n**permissions:** " + std::to_string(permissions)
+ "\n**managed:** " + std::to_string(managed)
+ "\n**mentionable:** " + std::to_string(mentionable);
}
inline bool Role::operator==(Role rhs) {
return id == rhs.id;
}
/* permission values */
enum class Permission {
CreateInstantInvite = 0x00000001, // Allows creation of instant invites
KickMembers = 0x00000002, // Allows kicking members
BanMembers = 0x00000004, // Allows banning members
Administrator = 0x00000008, // Allows all permissions and bypasses channel permission overwrites
ManageChannels = 0x00000010, // Allows management and editing of channels
ManageGuild = 0x00000020, // Allows management and editing of the guild
ReadMessages = 0x00000400, // Allows reading messages in a channel.The channel will not appear for users without this permission
SendMessages = 0x00000800, // Allows for sending messages in a channel.
SendTTSMessages = 0x00001000, // Allows for sending of / tts messages
ManageMessages = 0x00002000, // Allows for deletion of other users messages
EmbedLinks = 0x00004000, // Links sent by this user will be auto - embedded
AttachFiles = 0x00008000, // Allows for uploading images and files
ReadMessageHistory = 0x00010000, // Allows for reading of message history
MentionEveryone = 0x00020000, // Allows for using the @everyone tag to notify all users in a channel, and the @here tag to notify all online users in a channel
Connect = 0x00100000, // Allows for joining of a voice channel
Speak = 0x00200000, // Allows for speaking in a voice channel
MuteMembers = 0x00400000, // Allows for muting members in a voice channel
DeafenMembers = 0x00800000, // Allows for deafening of members in a voice channel
MoveMembers = 0x01000000, // Allows for moving of members between voice channels
UseVAD = 0x02000000, // Allows for using voice - activity - detection in a voice channel
ChangeNickname = 0x04000000, // Allows for modification of own nickname
ManageNicknames = 0x08000000, // Allows for modification of other users nicknames
ManageRoles = 0x10000000 // Allows management and editing of roles
};
/* implement bitwise operators */
inline Permission operator|(Permission lhs, Permission rhs) {
return static_cast<Permission>(static_cast<int>(lhs) | static_cast<int>(rhs));
}
inline Permission operator|=(Permission &lhs, Permission rhs) {
lhs = static_cast<Permission>(static_cast<int>(lhs) | static_cast<int>(rhs));
return lhs;
}
}
#endif

View File

@ -2,6 +2,7 @@
#define BOT_DATA__STRUCTURES_USER
#include <string>
#include <vector>
#include "../json/json.hpp"
@ -37,6 +38,8 @@ namespace DiscordObjects {
std::string avatar;
bool bot;
bool mfa_enabled;
std::vector<std::string> guilds;
};
inline User::User() {