Merge branch 'slowmode'

This commit is contained in:
ouwou
2021-04-15 02:35:36 -04:00
11 changed files with 236 additions and 11 deletions

View File

@@ -261,6 +261,14 @@ bool DiscordClient::HasGuildPermission(Snowflake user_id, Snowflake guild_id, Pe
return (base & perm) == perm;
}
bool DiscordClient::HasAnyChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const {
const auto channel = m_store.GetChannel(channel_id);
if (!channel.has_value()) return false;
const auto base = ComputePermissions(user_id, *channel->GuildID);
const auto overwrites = ComputeOverwrites(base, user_id, channel_id);
return (overwrites & perm) != Permission::NONE;
}
bool DiscordClient::HasChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const {
const auto channel = m_store.GetChannel(channel_id);
if (!channel.has_value()) return false;
@@ -345,7 +353,16 @@ bool DiscordClient::CanManageMember(Snowflake guild_id, Snowflake actor, Snowfla
void DiscordClient::ChatMessageCallback(std::string nonce, const http::response_type &response) {
if (!CheckCode(response)) {
m_signal_message_send_fail.emit(nonce);
if (response.status_code == http::TooManyRequests) {
try { // not sure if this body is guaranteed
RateLimitedResponse r = nlohmann::json::parse(response.text);
m_signal_message_send_fail.emit(nonce, r.RetryAfter);
} catch (...) {
m_signal_message_send_fail.emit(nonce, 0);
}
} else {
m_signal_message_send_fail.emit(nonce, 0);
}
}
}

View File

@@ -94,6 +94,8 @@ public:
std::unordered_set<Snowflake> GetChannelsInGuild(Snowflake id) const;
bool HasGuildPermission(Snowflake user_id, Snowflake guild_id, Permission perm) const;
bool HasAnyChannelPermission(Snowflake user_id, Snowflake channel_id, Permission perm) const;
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;
@@ -312,8 +314,8 @@ public:
typedef sigc::signal<void, GuildJoinRequestUpdateData> type_signal_guild_join_request_update;
typedef sigc::signal<void, GuildJoinRequestDeleteData> type_signal_guild_join_request_delete;
typedef sigc::signal<void, Message> type_signal_message_sent;
typedef sigc::signal<void, std::string> type_signal_message_send_fail;
typedef sigc::signal<void, bool, GatewayCloseCode> type_signal_disconnected; // bool true if reconnecting
typedef sigc::signal<void, std::string /* nonce */, float /* retry_after */> type_signal_message_send_fail; // retry after param will be 0 if it failed for a reason that isnt slowmode
typedef sigc::signal<void, bool, GatewayCloseCode> type_signal_disconnected; // bool true if reconnecting
typedef sigc::signal<void> type_signal_connected;
type_signal_gateway_ready signal_gateway_ready();

View File

@@ -444,3 +444,10 @@ void to_json(nlohmann::json &j, const VerificationGateInfoObject &m) {
JS_IF("version", m.Version);
JS_IF("enabled", m.Enabled);
}
void from_json(const nlohmann::json &j, RateLimitedResponse &m) {
JS_D("code", m.Code);
JS_D("global", m.Global);
JS_O("message", m.Message);
JS_D("retry_after", m.RetryAfter);
}

View File

@@ -615,3 +615,13 @@ struct VerificationGateInfoObject {
friend void from_json(const nlohmann::json &j, VerificationGateInfoObject &m);
friend void to_json(nlohmann::json &j, const VerificationGateInfoObject &m);
};
// not sure what the structure for this really is
struct RateLimitedResponse {
int Code;
bool Global;
std::optional<std::string> Message;
float RetryAfter;
friend void from_json(const nlohmann::json &j, RateLimitedResponse &m);
};