show slash command/APPLICATION_COMMAND messages

This commit is contained in:
ouwou
2020-12-22 21:18:39 -05:00
parent 09f40cf9a9
commit 2e2ed5fc8f
5 changed files with 89 additions and 36 deletions

View File

@@ -221,6 +221,17 @@ void ChatMessageItemContainer::UpdateTextComponent(Gtk::TextView *tv) {
case MessageType::CHANNEL_PINNED_MESSAGE:
b->insert_markup(s, "<span color='#999999'><i>[message pinned]</i></span>");
break;
case MessageType::APPLICATION_COMMAND: {
if (data->Application.has_value()) {
static const auto regex = Glib::Regex::create(R"(</(.*?):(\d+)>)");
Glib::MatchInfo match;
if (regex->match(data->Content, match)) {
const auto cmd = match.fetch(1);
const auto app = data->Application->Name;
b->insert_markup(s, "<i>used <span color='#697ec4'>" + cmd + "</span> with " + app + "</i>");
}
}
} break;
default: break;
}
}
@@ -945,23 +956,18 @@ ChatMessageHeader::ChatMessageHeader(const Message *data) {
m_meta_ev->signal_button_press_event().connect(sigc::mem_fun(*this, &ChatMessageHeader::on_author_button_press));
if (data->WebhookID.has_value()) {
if (author->IsBot || data->WebhookID.has_value()) {
m_extra = Gtk::manage(new Gtk::Label);
m_extra->get_style_context()->add_class("message-container-extra");
m_extra->set_single_line_mode(true);
m_extra->set_margin_start(12);
m_extra->set_can_focus(false);
m_extra->set_use_markup(true);
m_extra->set_markup("<b>Webhook</b>");
} else if (data->Author.IsBot) {
m_extra = Gtk::manage(new Gtk::Label);
m_extra->get_style_context()->add_class("message-container-extra");
m_extra->set_single_line_mode(true);
m_extra->set_margin_start(12);
m_extra->set_can_focus(false);
m_extra->set_use_markup(true);
m_extra->set_markup("<b>BOT</b>");
}
if (author->IsBot)
m_extra->set_markup("<b>BOT</b>");
else if (data->WebhookID.has_value())
m_extra->set_markup("<b>Webhook</b>");
m_timestamp->set_text(data->Timestamp);
m_timestamp->set_opacity(0.5);

View File

@@ -51,7 +51,7 @@ class DiscordClient {
public:
static const constexpr char *DiscordGateway = "wss://gateway.discord.gg/?v=8&encoding=json&compress=zlib-stream";
static const constexpr char *DiscordAPI = "https://discord.com/api";
static const constexpr char *DiscordAPI = "https://discord.com/api/v8";
public:
DiscordClient(bool mem_store = false);

View File

@@ -164,6 +164,25 @@ void to_json(nlohmann::json &j, const ReactionData &m) {
j["emoji"] = m.Emoji;
}
void from_json(const nlohmann::json &j, MessageApplicationData &m) {
JS_D("id", m.ID);
JS_O("cover_image", m.CoverImage);
JS_D("description", m.Description);
JS_N("icon", m.Icon);
JS_D("name", m.Name);
}
void to_json(nlohmann::json &j, const MessageApplicationData &m) {
j["id"] = m.ID;
JS_IF("cover_image", m.CoverImage);
j["description"] = m.Description;
if (m.Icon == "")
j["icon"] = nullptr;
else
j["icon"] = m.Icon;
j["name"] = m.Name;
}
void from_json(const nlohmann::json &j, Message &m) {
JS_D("id", m.ID);
JS_D("channel_id", m.ChannelID);
@@ -188,7 +207,7 @@ void from_json(const nlohmann::json &j, Message &m) {
JS_O("webhook_id", m.WebhookID);
JS_D("type", m.Type);
// JS_O("activity", m.Activity);
// JS_O("application", m.Application);
JS_O("application", m.Application);
JS_O("message_reference", m.MessageReference);
JS_O("flags", m.Flags);
JS_O("stickers", m.Stickers);
@@ -212,6 +231,8 @@ void Message::from_json_edited(const nlohmann::json &j) {
JS_O("pinned", IsPinned);
JS_O("webhook_id", WebhookID);
JS_O("type", Type);
JS_O("application", Application);
JS_O("message_reference", MessageReference);
JS_O("flags", Flags);
JS_O("stickers", Stickers);
}

View File

@@ -24,7 +24,7 @@ enum class MessageType {
GUILD_DISCOVERY_DISQUALIFIED = 14, // nope
GUILD_DISCOVERY_REQUALIFIED = 15, // nope
INLINE_REPLY = 19, // kinda
APPLICATION_COMMAND, // nope
APPLICATION_COMMAND = 20, // yep
};
enum class MessageFlags {
@@ -151,6 +151,17 @@ struct ReactionData {
friend void to_json(nlohmann::json &j, const ReactionData &m);
};
struct MessageApplicationData {
Snowflake ID;
std::optional<std::string> CoverImage;
std::string Description;
std::string Icon; // null
std::string Name;
friend void from_json(const nlohmann::json &j, MessageApplicationData &m);
friend void to_json(nlohmann::json &j, const MessageApplicationData &m);
};
struct Message {
Snowflake ID;
Snowflake ChannelID;
@@ -173,7 +184,7 @@ struct Message {
std::optional<Snowflake> WebhookID;
MessageType Type;
// std::optional<MessageActivityData> Activity;
// std::optional<MessageApplicationData> Application;
std::optional<MessageApplicationData> Application;
std::optional<MessageReferenceData> MessageReference;
std::optional<MessageFlags> Flags = MessageFlags::NONE;
std::optional<std::vector<Sticker>> Stickers;

View File

@@ -203,29 +203,33 @@ void Store::SetMessage(Snowflake id, const Message &message) {
Bind(m_set_msg_stmt, 14, message.WebhookID);
Bind(m_set_msg_stmt, 15, static_cast<uint64_t>(message.Type));
if (message.MessageReference.has_value()) {
std::string tmp = nlohmann::json(*message.MessageReference).dump();
Bind(m_set_msg_stmt, 16, tmp);
} else
if (message.Application.has_value())
Bind(m_set_msg_stmt, 16, nlohmann::json(*message.Application).dump());
else
Bind(m_set_msg_stmt, 16, nullptr);
if (message.Flags.has_value())
Bind(m_set_msg_stmt, 17, static_cast<uint64_t>(*message.Flags));
if (message.MessageReference.has_value())
Bind(m_set_msg_stmt, 17, nlohmann::json(*message.MessageReference).dump());
else
Bind(m_set_msg_stmt, 17, nullptr);
if (message.Stickers.has_value()) {
std::string tmp = nlohmann::json(*message.Stickers).dump();
Bind(m_set_msg_stmt, 18, tmp);
} else
if (message.Flags.has_value())
Bind(m_set_msg_stmt, 18, static_cast<uint64_t>(*message.Flags));
else
Bind(m_set_msg_stmt, 18, nullptr);
if (message.Stickers.has_value())
Bind(m_set_msg_stmt, 19, nlohmann::json(*message.Stickers).dump());
else
Bind(m_set_msg_stmt, 19, nullptr);
if (message.Reactions.has_value()) {
std::string tmp = nlohmann::json(*message.Reactions).dump();
Bind(m_set_msg_stmt, 19, tmp);
Bind(m_set_msg_stmt, 20, tmp);
} else
Bind(m_set_msg_stmt, 19, nullptr);
Bind(m_set_msg_stmt, 20, message.IsDeleted());
Bind(m_set_msg_stmt, 21, message.IsEdited());
Bind(m_set_msg_stmt, 20, nullptr);
Bind(m_set_msg_stmt, 21, message.IsDeleted());
Bind(m_set_msg_stmt, 22, message.IsEdited());
if (!RunInsert(m_set_msg_stmt))
fprintf(stderr, "message insert failed: %s\n", sqlite3_errstr(m_db_err));
@@ -470,21 +474,31 @@ std::optional<Message> Store::GetMessage(Snowflake id) const {
uint64_t tmpi;
Get(m_get_msg_stmt, 14, tmpi);
ret.Type = static_cast<MessageType>(tmpi);
Get(m_get_msg_stmt, 15, tmps);
if (tmps != "")
ret.MessageReference = nlohmann::json::parse(tmps).get<MessageReferenceData>();
Get(m_get_msg_stmt, 16, tmpi);
ret.Flags = static_cast<MessageFlags>(tmpi);
Get(m_get_msg_stmt, 17, tmps);
ret.Application = nlohmann::json::parse(tmps).get<MessageApplicationData>();
Get(m_get_msg_stmt, 16, tmps);
if (tmps != "")
ret.Stickers = nlohmann::json::parse(tmps).get<std::vector<Sticker>>();
ret.MessageReference = nlohmann::json::parse(tmps).get<MessageReferenceData>();
Get(m_get_msg_stmt, 17, tmpi);
ret.Flags = static_cast<MessageFlags>(tmpi);
Get(m_get_msg_stmt, 18, tmps);
if (tmps != "")
ret.Stickers = nlohmann::json::parse(tmps).get<std::vector<Sticker>>();
Get(m_get_msg_stmt, 19, tmps);
if (tmps != "")
ret.Reactions = nlohmann::json::parse(tmps).get<std::vector<ReactionData>>();
bool tmpb = false;
Get(m_get_msg_stmt, 19, tmpb);
if (tmpb) ret.SetDeleted();
Get(m_get_msg_stmt, 20, tmpb);
if (tmpb) ret.SetDeleted();
Get(m_get_msg_stmt, 21, tmpb);
if (tmpb) ret.SetEdited();
Reset(m_get_msg_stmt);
@@ -646,6 +660,7 @@ bool Store::CreateTables() {
pinned BOOL,
webhook_id INTEGER,
type INTEGER,
application TEXT, /* json */
reference TEXT, /* json */
flags INTEGER,
stickers TEXT, /* json */
@@ -836,7 +851,7 @@ bool Store::CreateStatements() {
constexpr const char *set_msg = R"(
REPLACE INTO messages VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)
)";