summaryrefslogtreecommitdiff
path: root/discord
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2020-10-12 18:17:53 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2020-10-12 18:17:53 -0400
commitf5ae8c3d3fd4c6b928da6c7c3c785525fc60b7b4 (patch)
treeecc57ee465504d787fd368c634a31ba8fae7f928 /discord
parentd48fe29da9d8a19cec982be5a8ea50589fb92c87 (diff)
downloadabaddon-portaudio-f5ae8c3d3fd4c6b928da6c7c3c785525fc60b7b4.tar.gz
abaddon-portaudio-f5ae8c3d3fd4c6b928da6c7c3c785525fc60b7b4.zip
add kick/ban
Diffstat (limited to 'discord')
-rw-r--r--discord/discord.cpp37
-rw-r--r--discord/discord.hpp4
-rw-r--r--discord/http.cpp21
-rw-r--r--discord/http.hpp1
4 files changed, 63 insertions, 0 deletions
diff --git a/discord/discord.cpp b/discord/discord.cpp
index 460c9e9..725f76f 100644
--- a/discord/discord.cpp
+++ b/discord/discord.cpp
@@ -231,6 +231,21 @@ Snowflake DiscordClient::GetMemberHoistedRole(Snowflake guild_id, Snowflake user
return roles[0]->ID;
}
+Snowflake DiscordClient::GetMemberHighestRole(Snowflake guild_id, Snowflake user_id) const {
+ const auto *data = GetMember(user_id, guild_id);
+ if (data == nullptr) return Snowflake::Invalid;
+
+ if (data->Roles.size() == 0) return Snowflake::Invalid;
+ if (data->Roles.size() == 1) return data->Roles[0];
+
+ return *std::max(data->Roles.begin(), data->Roles.end(), [this](const auto &a, const auto &b) -> bool {
+ const auto *role_a = GetRole(*a);
+ const auto *role_b = GetRole(*b);
+ if (role_a == nullptr || role_b == nullptr) return false; // for some reason a Snowflake(0) sneaks into here
+ return role_a->Position < role_b->Position;
+ });
+}
+
std::unordered_set<Snowflake> DiscordClient::GetUsersInGuild(Snowflake id) const {
auto it = m_guild_to_users.find(id);
if (it != m_guild_to_users.end())
@@ -324,6 +339,20 @@ Permission DiscordClient::ComputeOverwrites(Permission base, Snowflake member_id
return perms;
}
+bool DiscordClient::CanManageMember(Snowflake channel_id, Snowflake actor, Snowflake target) const {
+ const auto *channel = GetChannel(channel_id);
+ if (channel == nullptr) return false;
+ const auto *guild = GetGuild(channel->GuildID);
+ if (guild != nullptr && guild->OwnerID == target) return false;
+ const auto actor_highest_id = GetMemberHighestRole(channel->GuildID, actor);
+ const auto target_highest_id = GetMemberHighestRole(channel->GuildID, target);
+ const auto *actor_highest = GetRole(actor_highest_id);
+ const auto *target_highest = GetRole(target_highest_id);
+ if (actor_highest == nullptr) return false;
+ if (target_highest == nullptr) return true;
+ return actor_highest->Position > target_highest->Position;
+}
+
void DiscordClient::SendChatMessage(std::string content, Snowflake channel) {
// @([^@#]{1,32})#(\\d{4})
CreateMessageObject obj;
@@ -369,6 +398,14 @@ void DiscordClient::LeaveGuild(Snowflake id) {
m_http.MakeDELETE("/users/@me/guilds/" + std::to_string(id), [](auto) {});
}
+void DiscordClient::KickUser(Snowflake user_id, Snowflake guild_id) {
+ m_http.MakeDELETE("/guilds/" + std::to_string(guild_id) + "/members/" + std::to_string(user_id), [](auto) {});
+}
+
+void DiscordClient::BanUser(Snowflake user_id, Snowflake guild_id) {
+ m_http.MakePUT("/guilds/" + std::to_string(guild_id) + "/bans/" + std::to_string(user_id), "{}", [](auto) {});
+}
+
void DiscordClient::UpdateToken(std::string token) {
if (!IsStarted()) {
m_token = token;
diff --git a/discord/discord.hpp b/discord/discord.hpp
index a2bbea0..5bfb29a 100644
--- a/discord/discord.hpp
+++ b/discord/discord.hpp
@@ -84,6 +84,7 @@ public:
const PermissionOverwrite *GetPermissionOverwrite(Snowflake channel_id, Snowflake id) const;
const Emoji *GetEmoji(Snowflake id) const;
Snowflake GetMemberHoistedRole(Snowflake guild_id, Snowflake user_id, bool with_color = false) const;
+ Snowflake GetMemberHighestRole(Snowflake guild_id, Snowflake user_id) const;
std::unordered_set<Snowflake> GetUsersInGuild(Snowflake id) const;
std::unordered_set<Snowflake> GetRolesInGuild(Snowflake id) const;
@@ -91,6 +92,7 @@ public:
bool HasChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const;
Permission ComputePermissions(Snowflake member_id, Snowflake guild_id) const;
Permission ComputeOverwrites(Permission base, Snowflake member_id, Snowflake channel_id) const;
+ bool CanManageMember(Snowflake channel_id, Snowflake actor, Snowflake target) const; // kick, ban, edit nickname (cant think of a better name)
void SendChatMessage(std::string content, Snowflake channel);
void DeleteMessage(Snowflake channel_id, Snowflake id);
@@ -98,6 +100,8 @@ public:
void SendLazyLoad(Snowflake id);
void JoinGuild(std::string code);
void LeaveGuild(Snowflake id);
+ void KickUser(Snowflake user_id, Snowflake guild_id);
+ void BanUser(Snowflake user_id, Snowflake guild_id); // todo: reason, delete messages
void UpdateToken(std::string token);
diff --git a/discord/http.cpp b/discord/http.cpp
index 4c9cf87..41f6c17 100644
--- a/discord/http.cpp
+++ b/discord/http.cpp
@@ -71,6 +71,27 @@ void HTTPClient::MakePOST(std::string path, std::string payload, std::function<v
#endif
}
+void HTTPClient::MakePUT(std::string path, std::string payload, std::function<void(cpr::Response r)> cb) {
+ printf("PUT %s\n", path.c_str());
+ auto url = cpr::Url { m_api_base + path };
+ auto headers = cpr::Header {
+ { "Authorization", m_authorization },
+ { "Content-Type", "application/json" },
+ };
+ auto body = cpr::Body { payload };
+#ifdef USE_LOCAL_PROXY
+ m_futures.push_back(cpr::PutCallback(
+ std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
+ url, headers, body,
+ cpr::Proxies { { "http", "127.0.0.1:8888" }, { "https", "127.0.0.1:8888" } },
+ cpr::VerifySsl { false }));
+#else
+ m_futures.push_back(cpr::PutCallback(
+ std::bind(&HTTPClient::OnResponse, this, std::placeholders::_1, cb),
+ url, headers, body));
+#endif
+}
+
void HTTPClient::MakeGET(std::string path, std::function<void(cpr::Response r)> cb) {
printf("GET %s\n", path.c_str());
auto url = cpr::Url { m_api_base + path };
diff --git a/discord/http.hpp b/discord/http.hpp
index 12667f7..db60498 100644
--- a/discord/http.hpp
+++ b/discord/http.hpp
@@ -18,6 +18,7 @@ public:
void MakeGET(std::string path, std::function<void(cpr::Response r)> cb);
void MakePATCH(std::string path, std::string payload, std::function<void(cpr::Response r)> cb);
void MakePOST(std::string path, std::string payload, std::function<void(cpr::Response r)> cb);
+ void MakePUT(std::string path, std::string payload, std::function<void(cpr::Response r)> cb);
private:
void OnResponse(cpr::Response r, std::function<void(cpr::Response r)> cb);