friends: send friend requests

This commit is contained in:
ouwou
2021-05-10 02:13:12 -04:00
parent 81ae2b3a83
commit 06ba3acc93
7 changed files with 114 additions and 0 deletions

View File

@@ -697,6 +697,25 @@ void DiscordClient::RemoveRelationship(Snowflake id, sigc::slot<void(bool succes
});
}
void DiscordClient::SendFriendRequest(const Glib::ustring &username, int discriminator, sigc::slot<void(bool success, DiscordError code)> callback) {
FriendRequestObject obj;
obj.Username = username;
obj.Discriminator = discriminator;
m_http.MakePOST("/users/@me/relationships", nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) {
if (CheckCode(response, 204)) {
callback(true, DiscordError::NONE);
} else {
auto code = DiscordError::GENERIC;
try {
// pull me somewhere else?
const auto data = nlohmann::json::parse(response.text);
data.at("code").get_to(code);
} catch (...) {}
callback(false, code);
}
});
}
bool DiscordClient::CanModifyRole(Snowflake guild_id, Snowflake role_id, Snowflake user_id) const {
const auto guild = *GetGuild(guild_id);
if (guild.OwnerID == user_id) return true;

View File

@@ -139,6 +139,7 @@ public:
void DeleteEmoji(Snowflake guild_id, Snowflake emoji_id, sigc::slot<void(bool success)> callback);
std::optional<GuildApplicationData> GetGuildApplication(Snowflake guild_id) const;
void RemoveRelationship(Snowflake id, sigc::slot<void(bool success)> callback);
void SendFriendRequest(const Glib::ustring &username, int discriminator, sigc::slot<void(bool success, DiscordError code)> callback);
bool CanModifyRole(Snowflake guild_id, Snowflake role_id) const;
bool CanModifyRole(Snowflake guild_id, Snowflake role_id, Snowflake user_id) const;

36
discord/errors.hpp Normal file
View File

@@ -0,0 +1,36 @@
#pragma once
enum class DiscordError {
GENERIC = 0,
INVALID_FORM_BODY = 50035,
RELATIONSHIP_INCOMING_DISABLED = 80000,
RELATIONSHIP_INCOMING_BLOCKED = 80001,
RELATIONSHIP_INVALID_USER_BOT = 80002, // this is misspelled in discord's source lul
RELATIONSHIP_INVALID_SELF = 80003,
RELATIONSHIP_INVALID_DISCORD_TAG = 80004,
RELATIONSHIP_ALREADY_FRIENDS = 80007,
NONE = -1,
};
constexpr const char *GetDiscordErrorDisplayString(DiscordError error) {
switch (error) {
case DiscordError::INVALID_FORM_BODY:
return "Something's wrong with your input";
case DiscordError::RELATIONSHIP_INCOMING_DISABLED:
return "This user isn't accepting friend requests";
case DiscordError::RELATIONSHIP_INCOMING_BLOCKED:
return "You are blocked by this user";
case DiscordError::RELATIONSHIP_INVALID_USER_BOT:
return "You can't send a request to a bot";
case DiscordError::RELATIONSHIP_INVALID_SELF:
return "You can't send a request to yourself";
case DiscordError::RELATIONSHIP_INVALID_DISCORD_TAG:
return "No users with that tag exist";
case DiscordError::RELATIONSHIP_ALREADY_FRIENDS:
return "You are already friends with that user";
case DiscordError::GENERIC:
default:
return "An error occurred";
}
}

View File

@@ -463,3 +463,8 @@ void from_json(const nlohmann::json &j, RelationshipAddData &m) {
JS_D("type", m.Type);
JS_D("user", m.User);
}
void to_json(nlohmann::json &j, const FriendRequestObject &m) {
j["username"] = m.Username;
j["discriminator"] = m.Discriminator;
}

View File

@@ -19,6 +19,7 @@
#include "ban.hpp"
#include "auditlog.hpp"
#include "relationship.hpp"
#include "errors.hpp"
// most stuff below should just be objects that get processed and thrown away immediately
@@ -645,3 +646,10 @@ struct RelationshipAddData {
friend void from_json(const nlohmann::json &j, RelationshipAddData &m);
};
struct FriendRequestObject {
std::string Username;
int Discriminator;
friend void to_json(nlohmann::json &j, const FriendRequestObject &m);
};