Skip to content

Commit

Permalink
feat: ✨ add more events; update example
Browse files Browse the repository at this point in the history
  • Loading branch information
Xminent committed Oct 28, 2023
1 parent aa92d8e commit eba040c
Show file tree
Hide file tree
Showing 40 changed files with 1,540 additions and 44 deletions.
65 changes: 32 additions & 33 deletions examples/simple_with_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,38 @@ void handle_event(Event ev)
{
const auto &logger = get_logger();

std::visit(
overload{ [&logger](const Ready &payload) {
logger->info(
fmt::format("Logged in as {}",
payload.user.username));

logger->info(fmt::format("API version: {}",
payload.v));
logger->info(fmt::format(
"Guilds: {}", payload.guilds.size()));

logger->info(fmt::format(
"Ready object: {}",
nlohmann::json{ payload }.dump()));
},
[&logger](const MessageCreate &payload) {
const auto &[msg] = payload;

logger->info(fmt::format("Message: {}",
msg.content));
},
[&logger](const Log &log) {
logger->debug(
fmt::format("Log: {}", log.message));
},
[&logger](const auto &e) {
logger->info(fmt::format("Uncaught event: {}",
typeid(e).name()));
// logger->info(fmt::format(
// "Uncaught event: {}",
// nlohmann::json{ e }.dump()));
} },
ev);
std::visit(overload{ [&logger](const Ready &payload) {
logger->info(
fmt::format("Logged in as {}",
payload.user.username));

logger->info(fmt::format("API version: {}",
payload.v));
logger->info(
fmt::format("Guilds: {}",
payload.guilds.size()));

logger->info(fmt::format(
"Ready object: {}",
nlohmann::json{ payload }.dump()));
},
[&logger](const MessageCreate &payload) {
const auto &[msg] = payload;

logger->info(fmt::format("Message: {}",
msg.content));
},
[&logger](const Log &log) {
logger->debug(fmt::format("Log: {}",
log.message));
},
[&logger](const auto &e) {
logger->info(fmt::format(
"Uncaught {} event: {}",
typeid(e).name(),
nlohmann::json{ e }.dump()));
} },
ev);
}

int main()
Expand Down
36 changes: 36 additions & 0 deletions include/ekizu/activity_flags.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef EKIZU_ACTIVITY_FLAGS_HPP
#define EKIZU_ACTIVITY_FLAGS_HPP

#include <cstdint>
#include <type_traits>

namespace ekizu
{
enum class ActivityFlags : uint64_t {
Instance = 1 << 0,
Join = 1 << 1,
Spectate = 1 << 2,
JoinRequest = 1 << 3,
Sync = 1 << 4,
Play = 1 << 5,
PartyPrivacyFriends = 1 << 6,
PartyPrivacyVoiceChannel = 1 << 7,
Embedded = 1 << 8,
};

constexpr ActivityFlags operator|(ActivityFlags lhs, ActivityFlags rhs)
{
return static_cast<ActivityFlags>(
static_cast<std::underlying_type_t<ActivityFlags> >(lhs) |
static_cast<std::underlying_type_t<ActivityFlags> >(rhs));
}

constexpr ActivityFlags operator&(ActivityFlags lhs, ActivityFlags rhs)
{
return static_cast<ActivityFlags>(
static_cast<std::underlying_type_t<ActivityFlags> >(lhs) &
static_cast<std::underlying_type_t<ActivityFlags> >(rhs));
}
} // namespace ekizu

#endif // EKIZU_ACTIVITY_FLAGS_HPP
41 changes: 41 additions & 0 deletions include/ekizu/command_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef EKIZU_MODELS_COMMAND_DATA_HPP
#define EKIZU_MODELS_COMMAND_DATA_HPP

#include <ekizu/command_data_resolved.hpp>

namespace ekizu
{
struct CommandDataOption {
/// Name of the option.
std::string name;
/// Value of the option.
std::string value;
};

EKIZU_EXPORT void to_json(nlohmann::json &j, const CommandDataOption &c);
EKIZU_EXPORT void from_json(const nlohmann::json &j, CommandDataOption &c);

enum class CommandType : uint8_t { ChatInput, User, Message, Unknown };

struct CommandData {
/// ID of the guild the command is registered to.
std::optional<Snowflake> guild_id;
/// ID of the command.
Snowflake id;
/// Name of the command.
std::string name;
/// Type of the command.
CommandType kind;
/// List of options specified by the user.
std::vector<CommandDataOption> options;
/// Resolved data from the interaction's options.
std::optional<CommandInteractionDataResolved> resolved;
/// If this is a user or message command, the ID of the targeted user/message.
std::optional<Snowflake> target_id;
};

EKIZU_EXPORT void to_json(nlohmann::json &j, const CommandData &c);
EKIZU_EXPORT void from_json(const nlohmann::json &j, CommandData &c);
} // namespace ekizu

#endif // EKIZU_MODELS_COMMAND_DATA_HPP
67 changes: 67 additions & 0 deletions include/ekizu/command_data_resolved.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef EKIZU_MODELS_COMMAND_INTERACTION_DATA_RESOLVED_HPP
#define EKIZU_MODELS_COMMAND_INTERACTION_DATA_RESOLVED_HPP

#include <ekizu/member_flags.hpp>
#include <ekizu/message.hpp>

namespace ekizu
{
struct InteractionChannel {
/// ID of the channel.
Snowflake id;
/// Type of the channel.
ChannelType kind;
/// Name of the channel.
std::string name;
/// ID of the channel the thread was created in.
std::optional<Snowflake> parent_id;
/// Computed permissions, including overwrites, for the invoking user in the channel.
Permissions permissions;
/// Metadata about a thread.
std::optional<ThreadMetadata> thread_metadata;
};

struct InteractionMember {
/// Member's guild avatar.
std::optional<std::string> avatar;
/// If the member is timed out, when the timeout will expire.
std::optional<std::string> communication_disabled_until;
/// Flags for the member.
MemberFlags flags;
/// Member guild join date.
std::string joined_at;
/// Member nickname.
std::optional<std::string> nick;
/// Whether the user has yet to pass the guild's Membership Screening requirements.
bool pending;
/// Total permissions of the member in this channel, including overwrites.
Permissions permissions;
std::optional<std::string> premium_since;
/// Member roles.
std::vector<Snowflake> roles;
};

struct CommandInteractionDataResolved {
/// Map of resolved attachments.
std::unordered_map<Snowflake, Attachment> attachments;
/// Map of resolved channels.
std::unordered_map<Snowflake, InteractionChannel> channels;
/// Map of resolved members.
///
/// Resolved members' ID will map to a resolved user as well.
std::unordered_map<Snowflake, InteractionMember> members;
/// Map of resolved messages.
std::unordered_map<Snowflake, Message> messages;
/// Map of resolved roles.
std::unordered_map<Snowflake, Role> roles;
/// Map of resolved users.
std::unordered_map<Snowflake, User> users;
};

EKIZU_EXPORT void to_json(nlohmann::json &j,
const CommandInteractionDataResolved &r);
EKIZU_EXPORT void from_json(const nlohmann::json &j,
CommandInteractionDataResolved &r);
} // namespace ekizu

#endif // EKIZU_MODELS_COMMAND_INTERACTION_DATA_RESOLVED_HPP
30 changes: 22 additions & 8 deletions include/ekizu/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,42 @@
#include <ekizu/gateway/guild_delete.hpp>
#include <ekizu/gateway/guild_emojis_update.hpp>
#include <ekizu/gateway/guild_integrations_update.hpp>
#include <ekizu/gateway/guild_member_add.hpp>
#include <ekizu/gateway/guild_member_remove.hpp>
#include <ekizu/gateway/guild_member_update.hpp>
#include <ekizu/gateway/guild_scheduled_event_create.hpp>
#include <ekizu/gateway/guild_scheduled_event_delete.hpp>
#include <ekizu/gateway/guild_scheduled_event_update.hpp>
#include <ekizu/gateway/guild_scheduled_event_user_add.hpp>
#include <ekizu/gateway/guild_scheduled_event_user_remove.hpp>
#include <ekizu/gateway/guild_stickers_update.hpp>
#include <ekizu/gateway/guild_update.hpp>
#include <ekizu/gateway/integration_create.hpp>
#include <ekizu/gateway/integration_delete.hpp>
#include <ekizu/gateway/integration_update.hpp>
#include <ekizu/gateway/invite_create.hpp>
#include <ekizu/gateway/invite_delete.hpp>
#include <ekizu/gateway/log.hpp>
#include <ekizu/gateway/message_create.hpp>
#include <ekizu/gateway/message_delete.hpp>
#include <ekizu/gateway/message_delete_bulk.hpp>
#include <ekizu/gateway/message_update.hpp>
#include <ekizu/gateway/presence_update.hpp>
#include <ekizu/gateway/ready.hpp>
#include <ekizu/gateway/resumed.hpp>

namespace ekizu
{
using Event =
std::variant<BanAdd, BanRemove, ChannelCreate, ChannelDelete,
ChannelPinsUpdate, ChannelUpdate, GuildCreate, GuildDelete,
GuildEmojisUpdate, GuildIntegrationsUpdate,
GuildScheduledEventCreate, GuildScheduledEventDelete,
GuildScheduledEventUpdate, GuildScheduledEventUserAdd,
GuildScheduledEventUserRemove, GuildStickersUpdate,
GuildUpdate, Log, MessageCreate, Ready, Resumed>;
using Event = std::variant<
BanAdd, BanRemove, ChannelCreate, ChannelDelete, ChannelPinsUpdate,
ChannelUpdate, GuildCreate, GuildDelete, GuildEmojisUpdate,
GuildIntegrationsUpdate, GuildScheduledEventCreate,
GuildScheduledEventDelete, GuildScheduledEventUpdate,
GuildScheduledEventUserAdd, GuildScheduledEventUserRemove,
GuildStickersUpdate, GuildUpdate, IntegrationCreate, IntegrationDelete,
IntegrationUpdate, InviteCreate, InviteDelete, GuildMemberAdd,
GuildMemberRemove, GuildMemberUpdate, Log, MessageCreate, MessageDelete,
MessageDeleteBulk, MessageUpdate, PresenceUpdate, Ready, Resumed>;
} // namespace ekizu

#endif // EKIZU_EVENT_HPP
17 changes: 17 additions & 0 deletions include/ekizu/gateway/guild_member_add.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef EKIZU_GATEWAY_GUILD_MEMBER_ADD_HPP
#define EKIZU_GATEWAY_GUILD_MEMBER_ADD_HPP

#include <ekizu/guild_member.hpp>

namespace ekizu
{
struct GuildMemberAdd {
Snowflake guild_id;
GuildMember member;
};

EKIZU_EXPORT void to_json(nlohmann::json &j, const GuildMemberAdd &m);
EKIZU_EXPORT void from_json(const nlohmann::json &j, GuildMemberAdd &m);
} // namespace ekizu

#endif // EKIZU_GATEWAY_GUILD_MEMBER_ADD_HPP
17 changes: 17 additions & 0 deletions include/ekizu/gateway/guild_member_remove.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef EKIZU_GATEWAY_GUILD_MEMBER_REMOVE_HPP
#define EKIZU_GATEWAY_GUILD_MEMBER_REMOVE_HPP

#include <ekizu/guild_member.hpp>

namespace ekizu
{
struct GuildMemberRemove {
Snowflake guild_id;
GuildMember member;
};

EKIZU_EXPORT void to_json(nlohmann::json &j, const GuildMemberRemove &m);
EKIZU_EXPORT void from_json(const nlohmann::json &j, GuildMemberRemove &m);
} // namespace ekizu

#endif // EKIZU_GATEWAY_GUILD_MEMBER_REMOVE_HPP
26 changes: 26 additions & 0 deletions include/ekizu/gateway/guild_member_update.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef EKIZU_GATEWAY_GUILD_MEMBER_UPDATE_HPP
#define EKIZU_GATEWAY_GUILD_MEMBER_UPDATE_HPP

#include <ekizu/guild_member.hpp>

namespace ekizu
{
struct GuildMemberUpdate {
Snowflake guild_id;
std::vector<Snowflake> roles;
User user;
std::optional<std::string> nick;
std::optional<std::string> avatar;
std::optional<std::string> joined_at;
std::optional<std::string> premium_since;
std::optional<bool> deaf;
std::optional<bool> mute;
bool pending{};
std::optional<std::string> communication_disabled_until;
};

EKIZU_EXPORT void to_json(nlohmann::json &j, const GuildMemberUpdate &m);
EKIZU_EXPORT void from_json(const nlohmann::json &j, GuildMemberUpdate &m);
} // namespace ekizu

#endif // EKIZU_GATEWAY_GUILD_MEMBER_UPDATE_HPP
16 changes: 16 additions & 0 deletions include/ekizu/gateway/integration_create.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef EKIZU_GATEWAY_INTEGRATION_CREATE_HPP
#define EKIZU_GATEWAY_INTEGRATION_CREATE_HPP

#include <ekizu/guild_interaction.hpp>

namespace ekizu
{
struct IntegrationCreate {
GuildIntegration integration;
};

EKIZU_EXPORT void to_json(nlohmann::json &j, const IntegrationCreate &i);
EKIZU_EXPORT void from_json(const nlohmann::json &j, IntegrationCreate &i);
} // namespace ekizu

#endif // EKIZU_GATEWAY_INTEGRATION_CREATE_HPP
16 changes: 16 additions & 0 deletions include/ekizu/gateway/integration_delete.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef EKIZU_GATEWAY_INTEGRATION_DELETE_HPP
#define EKIZU_GATEWAY_INTEGRATION_DELETE_HPP

#include <ekizu/guild_interaction.hpp>

namespace ekizu
{
struct IntegrationDelete {
GuildIntegration integration;
};

EKIZU_EXPORT void to_json(nlohmann::json &j, const IntegrationDelete &i);
EKIZU_EXPORT void from_json(const nlohmann::json &j, IntegrationDelete &i);
} // namespace ekizu

#endif // EKIZU_GATEWAY_INTEGRATION_DELETE_HPP
16 changes: 16 additions & 0 deletions include/ekizu/gateway/integration_update.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef EKIZU_GATEWAY_INTEGRATION_UPDATE_HPP
#define EKIZU_GATEWAY_INTEGRATION_UPDATE_HPP

#include <ekizu/guild_interaction.hpp>

namespace ekizu
{
struct IntegrationUpdate {
GuildIntegration integration;
};

EKIZU_EXPORT void to_json(nlohmann::json &j, const IntegrationUpdate &i);
EKIZU_EXPORT void from_json(const nlohmann::json &j, IntegrationUpdate &i);
} // namespace ekizu

#endif // EKIZU_GATEWAY_INTEGRATION_UPDATE_HPP
Empty file.
Loading

0 comments on commit eba040c

Please sign in to comment.