Skip to content

Commit

Permalink
Create a internal DM channel if we haven't encountered it before, due…
Browse files Browse the repository at this point in the history
… to v8 change

we do not have a CREATE_CHANNEL event for DMs.
  • Loading branch information
Alasnkz committed Nov 3, 2020
1 parent 4bea040 commit eb95a13
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
38 changes: 37 additions & 1 deletion src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ Channel::Channel(ChannelId_t pawn_id, json const &data, GuildId_t guild_id) :
}
}

Channel::Channel(ChannelId_t pawn_id, Snowflake_t channel_id, Type type) :
m_PawnId(pawn_id),
m_Id(channel_id),
m_Type(type)
{
}

void Channel::Update(json const &data)
{
utils::TryGetJsonValue(data, m_Name, "name"),
Expand Down Expand Up @@ -395,7 +402,7 @@ ChannelId_t ChannelManager::AddChannel(json const &data, GuildId_t guild_id/* =
return INVALID_CHANNEL_ID;
}

Channel_t const &channel = FindChannelById(sfid);
Channel_t const& channel = FindChannelById(sfid);
if (channel)
return channel->GetPawnId(); // channel already exists

Expand All @@ -414,6 +421,35 @@ ChannelId_t ChannelManager::AddChannel(json const &data, GuildId_t guild_id/* =
return id;
}

ChannelId_t ChannelManager::AddDMChannel(json const& data)
{
Snowflake_t sfid;
if (!utils::TryGetJsonValue(data, sfid, "channel_id"))
{
Logger::Get()->Log(LogLevel::ERROR,
"invalid JSON: expected \"channel_id\" in \"{}\"", data.dump());
return INVALID_CHANNEL_ID;
}

Channel_t const& channel = FindChannelById(sfid);
if (channel)
return channel->GetPawnId(); // channel already exists

ChannelId_t id = 1;
while (m_Channels.find(id) != m_Channels.end())
++id;

if (!m_Channels.emplace(id, Channel_t(new Channel(id, sfid, Channel::Type::DM))).second)
{
Logger::Get()->Log(LogLevel::ERROR,
"can't create channel: duplicate key '{}'", id);
return INVALID_CHANNEL_ID;
}

Logger::Get()->Log(LogLevel::INFO, "successfully added channel with id '{}'", id);
return id;
}

void ChannelManager::DeleteChannel(json const &data)
{
Snowflake_t sfid;
Expand Down
3 changes: 3 additions & 0 deletions src/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Channel

public:
Channel(ChannelId_t pawn_id, json const &data, GuildId_t guild_id);
Channel(ChannelId_t pawn_id, Snowflake_t channel_id, Type type);
~Channel() = default;

private:
Expand Down Expand Up @@ -129,6 +130,8 @@ class ChannelManager : public Singleton<ChannelManager>
}

ChannelId_t AddChannel(json const &data, GuildId_t guild_id = 0);
ChannelId_t AddDMChannel(json const & data);

void DeleteChannel(json const &data);

Channel_t const &FindChannel(ChannelId_t id);
Expand Down
12 changes: 10 additions & 2 deletions src/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

Message::Message(MessageId_t pawn_id, json const &data) : m_PawnId(pawn_id)
{
std::string author_id, channel_id;
std::string author_id, channel_id, guild_id;
_valid =
utils::TryGetJsonValue(data, m_Id, "id") &&
utils::TryGetJsonValue(data, author_id, "author", "id") &&
Expand All @@ -28,7 +28,15 @@ Message::Message(MessageId_t pawn_id, json const &data) : m_PawnId(pawn_id)
}

Channel_t const &channel = ChannelManager::Get()->FindChannelById(channel_id);
m_Channel = channel ? channel->GetPawnId() : INVALID_CHANNEL_ID;
if (!channel && !utils::TryGetJsonValue(data, guild_id, "guild_id"))
{
ChannelId_t cid = ChannelManager::Get()->AddDMChannel(data);
m_Channel = ChannelManager::Get()->FindChannel(cid)->GetPawnId();
}
else
{
m_Channel = channel ? channel->GetPawnId() : INVALID_CHANNEL_ID;
}

User_t const &user = UserManager::Get()->FindUserById(author_id);
m_Author = user ? user->GetPawnId() : INVALID_USER_ID;
Expand Down

0 comments on commit eb95a13

Please sign in to comment.