Skip to content

Commit

Permalink
refactor: ♻️ use SABER_TRY instead of EKIZU_TRY; change CommandLoader…
Browse files Browse the repository at this point in the history
…::process_commands return type
  • Loading branch information
Xminent committed Jan 17, 2024
1 parent 44f525f commit bb182f4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 72 deletions.
6 changes: 3 additions & 3 deletions commands/Music/join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Join : Command {
Result<> execute(const ekizu::Message &message,
[[maybe_unused]] const std::vector<std::string> &args,
const boost::asio::yield_context &yield) override {
EKIZU_TRY(
SABER_TRY(
auto guild, bot.http().get_guild(*message.guild_id).send(yield));

auto voice_state =
Expand All @@ -32,15 +32,15 @@ struct Join : Command {
});

if (!voice_state) {
EKIZU_TRY(bot.http()
SABER_TRY(bot.http()
.create_message(message.channel_id)
.content("You are not in a voice channel!")
.reply(message.id)
.send(yield));
return outcome::success();
}

EKIZU_TRY(bot.join_voice_channel(
SABER_TRY(bot.join_voice_channel(
*message.guild_id, *voice_state->channel_id, yield));

return outcome::success();
Expand Down
20 changes: 10 additions & 10 deletions commands/Music/play.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Play : Command {
Result<> execute(const ekizu::Message &message,
[[maybe_unused]] const std::vector<std::string> &args,
const boost::asio::yield_context &yield) override {
EKIZU_TRY(
SABER_TRY(
auto guild, bot.http().get_guild(*message.guild_id).send(yield));

auto voice_state =
Expand All @@ -41,7 +41,7 @@ struct Play : Command {
});

if (!voice_state) {
EKIZU_TRY(bot.http()
SABER_TRY(bot.http()
.create_message(message.channel_id)
.content("You are not in a voice channel!")
.reply(message.id)
Expand All @@ -50,7 +50,7 @@ struct Play : Command {
}

if (args.empty()) {
EKIZU_TRY(bot.http()
SABER_TRY(bot.http()
.create_message(message.channel_id)
.content("Please specify a query.")
.reply(message.id)
Expand Down Expand Up @@ -148,12 +148,12 @@ struct Play : Command {
int time_total{};

// Start our voice connection.
EKIZU_TRY(auto config,
SABER_TRY(auto config,
bot.join_voice_channel(
*message.guild_id, *voice_state->channel_id, yield));
EKIZU_TRY(auto conn, config->connect(yield));
EKIZU_TRY(conn.run(yield));
EKIZU_TRY(conn.speak(ekizu::SpeakerFlag::Microphone, yield));
SABER_TRY(auto conn, config->connect(yield));
SABER_TRY(conn.run(yield));
SABER_TRY(conn.speak(ekizu::SpeakerFlag::Microphone, yield));

while (ogg_sync_pageout(&oy, &og) == 1) {
// Start a new stream, using the serial number from the page.
Expand Down Expand Up @@ -193,16 +193,16 @@ struct Play : Command {
const auto duration = samples / 48;
time_total += duration;

EKIZU_TRY(conn.send_opus(
SABER_TRY(conn.send_opus(
boost::as_bytes(
boost::span{op.packet, static_cast<size_t>(op.bytes)}),
yield));
}
}

bot.log("Played {}ms of audio", time_total);
EKIZU_TRY(conn.close(yield));
EKIZU_TRY(bot.leave_voice_channel(*message.guild_id, yield));
SABER_TRY(conn.close(yield));
SABER_TRY(bot.leave_voice_channel(*message.guild_id, yield));

return outcome::success();
}
Expand Down
4 changes: 2 additions & 2 deletions include/saber/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ struct CommandLoader {
SABER_EXPORT void load(std::string_view path,
const boost::asio::yield_context &yield);
SABER_EXPORT void load_all(const boost::asio::yield_context &yield);
SABER_EXPORT void process_commands(const ekizu::Message &message,
const boost::asio::yield_context &yield);
SABER_EXPORT Result<> process_commands(
const ekizu::Message &message, const boost::asio::yield_context &yield);
SABER_EXPORT void unload(const std::string &name);
SABER_EXPORT void get_commands(
ekizu::FunctionView<void(const boost::unordered_flat_map<
Expand Down
2 changes: 2 additions & 0 deletions include/saber/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

#include <boost/blank.hpp>
#include <boost/outcome/result.hpp>
#include <boost/outcome/try.hpp>

namespace saber {
namespace outcome = boost::outcome_v2;
template <typename T = boost::blank>
using Result = outcome::result<T>;
#define SABER_TRY BOOST_OUTCOME_TRY
} // namespace saber

#endif // SABER_RESULT_HPP
115 changes: 60 additions & 55 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ void CommandLoader::load_all(const boost::asio::yield_context &yield) {
m_parent.log<ekizu::LogLevel::Info>("Loaded {} commands", commands.size());
}

void CommandLoader::process_commands(const ekizu::Message &message,
const boost::asio::yield_context &yield) {
if (message.author.bot) { return; }
Result<> CommandLoader::process_commands(
const ekizu::Message &message, const boost::asio::yield_context &yield) {
if (message.author.bot) { return outcome::success(); }

auto content = message.content.substr(m_parent.prefix().size());
std::vector<std::string> args;
boost::algorithm::split(
args, boost::algorithm::trim_copy(content), boost::is_any_of(" "));

if (args.empty()) { return; }
if (args.empty()) { return outcome::success(); }

auto command_name = std::move(args.front());
args.erase(args.begin());
Expand All @@ -169,44 +169,48 @@ void CommandLoader::process_commands(const ekizu::Message &message,

std::unique_lock lk{m_mtx};

if (!command_map.contains(command_name)) { return; }
if (!command_map.contains(command_name)) { return outcome::success(); }

auto cmd = command_map.at(command_name);

if (cmd->options.guild_only) {
if (!message.guild_id) {
return (void)m_parent.http()
.create_message(message.channel_id)
.content("This command can only be used in guilds.")
.reply(message.id)
.send(yield);
SABER_TRY(m_parent.http()
.create_message(message.channel_id)
.content("This command can only be used in guilds.")
.reply(message.id)
.send(yield));
return outcome::success();
}

auto bot_permissions = m_parent.get_guild_permissions(
*message.guild_id, m_parent.bot_id());

if (!bot_permissions) {
return (void)m_parent.http()
.create_message(message.channel_id)
.content("Failed to get bot permissions.")
.reply(message.id)
.send(yield);
SABER_TRY(m_parent.http()
.create_message(message.channel_id)
.content("Failed to get bot permissions.")
.reply(message.id)
.send(yield));
return outcome::success();
}

// Check permissions.
for (const auto &perm : cmd->options.bot_permissions) {
if ((bot_permissions.value() & perm) != perm) {
return (void)m_parent.http()
.create_message(message.channel_id)
.content(fmt::format(
"Needed permissions: {}",
boost::algorithm::trim_copy(boost::algorithm::join(
cmd->options.bot_permissions |
boost::adaptors::transformed(
permissions_to_string),
", "))))
.reply(message.id)
.send(yield);
SABER_TRY(
m_parent.http()
.create_message(message.channel_id)
.content(fmt::format(
"Needed permissions: {}",
boost::algorithm::trim_copy(boost::algorithm::join(
cmd->options.bot_permissions |
boost::adaptors::transformed(
permissions_to_string),
", "))))
.reply(message.id)
.send(yield));
return outcome::success();
}
}

Expand All @@ -215,26 +219,29 @@ void CommandLoader::process_commands(const ekizu::Message &message,
*message.guild_id, message.author.id);

if (!member_permissions) {
return (void)m_parent.http()
.create_message(message.channel_id)
.content("Failed to get member permissions.")
.reply(message.id)
.send(yield);
SABER_TRY(m_parent.http()
.create_message(message.channel_id)
.content("Failed to get member permissions.")
.reply(message.id)
.send(yield));
return outcome::success();
}

for (const auto &perm : cmd->options.member_permissions) {
if ((member_permissions.value() & perm) != perm) {
return (void)m_parent.http()
.create_message(message.channel_id)
.content(fmt::format(
"You need the following permissions: {}",
boost::algorithm::trim_copy(boost::algorithm::join(
cmd->options.member_permissions |
boost::adaptors::transformed(
permissions_to_string),
", "))))
.reply(message.id)
.send(yield);
SABER_TRY(
m_parent.http()
.create_message(message.channel_id)
.content(fmt::format(
"You need the following permissions: {}",
boost::algorithm::trim_copy(boost::algorithm::join(
cmd->options.member_permissions |
boost::adaptors::transformed(
permissions_to_string),
", "))))
.reply(message.id)
.send(yield));
return outcome::success();
}
}
}
Expand All @@ -249,14 +256,16 @@ void CommandLoader::process_commands(const ekizu::Message &message,
.count();

if (delta > 0) {
return (void)m_parent.http()
.create_message(message.channel_id)
.content(fmt::format(
"Please wait {} more seconds before using this "
"command.",
delta))
.reply(message.id)
.send(yield);
SABER_TRY(
m_parent.http()
.create_message(message.channel_id)
.content(fmt::format(
"Please wait {} more seconds before using this "
"command.",
delta))
.reply(message.id)
.send(yield));
return outcome::success();
}
}
}
Expand All @@ -268,11 +277,7 @@ void CommandLoader::process_commands(const ekizu::Message &message,
// should be unlocked here. i.e. an unload command or something.
lk.unlock();

if (auto res = cmd->execute(message, args, yield); !res) {
m_parent.log<ekizu::LogLevel::Error>(
"Failed to execute command {}: {}", command_name,
res.error().message());
}
return cmd->execute(message, args, yield);
}

void CommandLoader::unload(const std::string &name) {
Expand Down
10 changes: 8 additions & 2 deletions src/saber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ SABER_EXPORT Result<ekizu::VoiceConnectionConfig *> Saber::join_voice_channel(
m_voice_ready_channels.emplace(guild_id, yield.get_executor());

auto channel = m_voice_ready_channels[guild_id];
EKIZU_TRY(m_shard.join_voice_channel(guild_id, channel_id, yield));
SABER_TRY(m_shard.join_voice_channel(guild_id, channel_id, yield));
channel->async_receive(yield);
m_voice_ready_channels.remove(guild_id);
return &m_voice_configs[guild_id];
Expand Down Expand Up @@ -195,7 +195,13 @@ void Saber::handle_event(ekizu::Event ev,
[this, &yield](const ekizu::MessageCreate &m) {
m_message_cache.put(m.message.id, m.message);
m_user_cache.put(m.message.author.id, m.message.author);
m_commands.process_commands(m.message, yield);

if (const auto res =
m_commands.process_commands(m.message, yield);
!res) {
log<ekizu::LogLevel::Warn>(
"Failed to process command: {}", res.error().message());
};
},
[this](ekizu::Resumed) { log<ekizu::LogLevel::Info>("Resumed"); },
[this](const auto &e) {
Expand Down

0 comments on commit bb182f4

Please sign in to comment.