-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
server.h
226 lines (202 loc) · 9.24 KB
/
server.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
#include <string>
#include "discord_object_interface.h"
#include "user.h"
#include "channel.h"
#include "snowflake.h"
#include "cache.h"
namespace SleepyDiscord {
enum Permission : int64_t;
struct Role;
/*Guild Member Structure
Field Type Description
user object user object
nick string? this users guild nickname (if one is set)
roles array array of role object id's
joined_at datetime date the user joined the guild
deaf bool if the user is deafened
mute bool if the user is muted
*/
struct ServerMember : public IdentifiableDiscordObject<User> {
ServerMember() = default;
//ServerMember(const std::string * rawJson);
ServerMember(const nonstd::string_view & rawJSON);
ServerMember(const json::Value& json);
//ServerMember(const json::Values values);
User user;
std::string nick;
std::vector<Snowflake<Role>> roles;
std::string joinedAt;
bool deaf = false;
bool mute = false;
inline operator User&() {
return user;
}
//const static std::initializer_list<const char*const> fields;
JSONStructStart
std::make_tuple(
json::pair (&ServerMember::user , "user" , json::OPTIONAL_FIELD),
json::pair (&ServerMember::nick , "nick" , json::OPTIONAL_FIELD ),
json::pair<json::ContainerTypeHelper>(&ServerMember::roles , "roles" , json::OPTIONAL_FIELD),
json::pair (&ServerMember::joinedAt, "joined_at", json::OPTIONAL_FIELD),
json::pair (&ServerMember::deaf , "deaf" , json::OPTIONAL_FIELD),
json::pair (&ServerMember::mute , "mute" , json::OPTIONAL_FIELD)
);
JSONStructEnd
};
struct Server : public IdentifiableDiscordObject<Server> {
//~Server();
Server() = default;
//Server(const std::string * rawJson);
Server(const nonstd::string_view & rawJSON);
Server(const json::Value& json);
//Server(const json::Values values);
std::string name;
std::string icon;
std::string splash;
Snowflake<User> ownerID;
Permission permissions;
std::string region;
std::string AFKchannelID;
int AFKTimeout;
bool embedEnable;
std::string embedChannelID;
int verificationLevel;
int defaultMessageNotifications;
std::list<Role> roles;
//voice_states
//emojis
//features
bool unavailable;
//presences
int MFALevel;
std::string joinedAt;
//those are only filled in from the onServer event
bool large;
std::list<ServerMember> members;
std::list<Channel> channels;
std::list<ServerMember>::iterator findMember(const Snowflake<User> userID);
std::list<Channel>::iterator findChannel(const Snowflake<Channel> channelID);
std::list<Role>::iterator findRole(const Snowflake<Role> roleID);
//const static std::initializer_list<const char*const> fields;
JSONStructStart
std::make_tuple(
json::pair (&Server::ID , "id" , json::REQUIRIED_FIELD),
json::pair (&Server::name , "name" , json::REQUIRIED_FIELD),
json::pair (&Server::icon , "icon" , json::NULLABLE_FIELD ),
json::pair (&Server::splash , "splash" , json::NULLABLE_FIELD ),
json::pair (&Server::ownerID , "owner_id" , json::OPTIONAL_FIELD ),
json::pair<json::EnumTypeHelper >(&Server::permissions , "permissions" , json::OPTIONAL_FIELD ),
json::pair (&Server::region , "region" , json::OPTIONAL_FIELD ),
json::pair (&Server::AFKchannelID , "afk_channel_id" , json::NULLABLE_FIELD ),
json::pair (&Server::AFKTimeout , "afk_timeout" , json::OPTIONAL_FIELD ),
json::pair (&Server::embedEnable , "embed_enabled" , json::OPTIONAL_FIELD ),
json::pair (&Server::embedChannelID , "embed_channel_id" , json::OPTIONAL_FIELD ),
json::pair (&Server::verificationLevel , "verification_level" , json::OPTIONAL_FIELD ),
json::pair (&Server::defaultMessageNotifications, "default_message_notifications", json::OPTIONAL_FIELD ),
json::pair<json::ContainerTypeHelper>(&Server::roles , "roles" , json::OPTIONAL_FIELD ),
json::pair (&Server::unavailable , "unavailable" , json::OPTIONAL_FIELD ),
json::pair (&Server::MFALevel , "mfa_level" , json::OPTIONAL_FIELD ),
json::pair (&Server::joinedAt , "joined_at" , json::OPTIONAL_FIELD ),
json::pair (&Server::large , "large" , json::OPTIONAL_FIELD ),
json::pair<json::ContainerTypeHelper>(&Server::members , "members" , json::OPTIONAL_FIELD ),
json::pair<json::ContainerTypeHelper>(&Server::channels , "channels" , json::OPTIONAL_FIELD )
);
JSONStructEnd
};
struct UnavailableServer : public IdentifiableDiscordObject<Server> {
UnavailableServer() = default;
//UnavailableServer(const std::string * rawJson);
UnavailableServer(const nonstd::string_view & rawJSON);
UnavailableServer(const json::Value& json);
//UnavailableServer(const json::Values values);
enum class AvailableFlag : int8_t {
NotSet = '\xFE', //-2 in hex
Unavaiable = true,
avaiable = false,
};
AvailableFlag unavailable = AvailableFlag::NotSet;
JSONStructStart
std::make_tuple(
json::pair(&UnavailableServer::ID, "id", json::REQUIRIED_FIELD),
json::pair<json::EnumTypeHelper>
(&UnavailableServer::unavailable, "unavailable", json::OPTIONAL_FIELD)
);
JSONStructEnd
};
template<>
struct GetDefault<UnavailableServer::AvailableFlag> {
static inline const UnavailableServer::AvailableFlag get() {
return UnavailableServer::AvailableFlag::NotSet;
}
};
template<>
struct GetEnumBaseType<UnavailableServer::AvailableFlag> {
//this makes the json wrapper know to use getBool instead of getInt
using Value = bool;
};
class ServerCache : public Cache<Server> {
public:
using Cache<Server>::Cache;
ServerCache() : Cache() {} //for some odd reason the default constructor isn't inherited
ServerCache(Cache<Server> list) : Cache<Server>(list) {}
/*
//Linear time complexity if unordered map: to do figure out how to do this with constant time complexity
template<class Container, class Object>
iterator findOneWithObject(Container Server::*list, const Snowflake<Object>& objectID) {
return std::find_if(begin(), end(), [&objectID, list](Server& server) {
auto result = objectID.findObject(server.*list);
return result != std::end(server.*list);
});
}
*/
inline const_iterator findSeverWith(const Snowflake<Channel>& channelID) {
return findOneWithObject(&Server::channels, channelID);
}
inline const_iterator findServerWith(const Snowflake<Role> roleID) {
return findOneWithObject(&Server::roles, roleID);
}
//Linear time complexity if using list
//Usually Constant time complexity if using unordered maps
inline iterator findServer(const Snowflake<Server> serverID) {
return serverID.findObject(*this);
}
};
struct ServerEmbed : public DiscordObject {
ServerEmbed() = default;
//ServerEmbed(const std::string * rawJson);
ServerEmbed(const nonstd::string_view & rawJSON);
ServerEmbed(const json::Value& json);
//ServerEmbed(const json::Values values);
bool enabled;
Snowflake<ServerEmbed> channelID;
//const static std::initializer_list<const char*const> fields;
JSONStructStart
std::make_tuple(
json::pair(&ServerEmbed::enabled , "enabled" , json::REQUIRIED_FIELD),
json::pair(&ServerEmbed::channelID, "channel_id", json::NULLABLE_FIELD )
);
JSONStructEnd
};
struct ServerMembersRequest {
ServerMembersRequest() = default;
ServerMembersRequest(const json::Value& json);
ServerMembersRequest(const nonstd::string_view & json);
Snowflake<Server> serverID;
std::string query;
int limit;
bool presence;
std::vector<Snowflake<User>> userIDs;
std::string nonce;
JSONStructStart
std::make_tuple(
json::pair(&ServerMembersRequest::serverID, "guild_id" , json::REQUIRIED_FIELD),
json::pair(&ServerMembersRequest::query , "query" , json::OPTIONAL_FIELD ),
json::pair(&ServerMembersRequest::limit , "limit" , json::OPTIONAL_FIELD ),
json::pair(&ServerMembersRequest::presence, "presences", json::OPTIONAL_FIELD ),
json::pair<json::ContainerTypeHelper>(&ServerMembersRequest::userIDs , "user_ids" , json::OPTIONAL_FIELD ),
json::pair(&ServerMembersRequest::nonce , "nonce" , json::OPTIONAL_FIELD )
);
JSONStructEnd
};
}