Skip to content

Commit

Permalink
Switch to Poise (#303)
Browse files Browse the repository at this point in the history
* Switch to poise

* Remove MESSAGE_CONTENT intent

* Update deps

* FMT
  • Loading branch information
adumbidiot authored Apr 10, 2024
1 parent 124dac8 commit 9fa9f37
Show file tree
Hide file tree
Showing 17 changed files with 377 additions and 445 deletions.
85 changes: 81 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
publish = false

[dependencies]
anyhow = "1.0.81"
anyhow = "1.0.82"
argh = "0.1.12"
camino = { version = "1.1.6", features = [ "serde1" ] }
# clokwerk = "0.4.0"
Expand All @@ -28,6 +28,7 @@ thiserror = "1.0.58"
tokio = { version = "1.37.0", features = [ "rt-multi-thread", "signal" ] }
toml = "0.8.12"
zalgo = { git = "https://github.com/adumbidiot/zalgo-rs" }
poise = "0.6.1"

[profile.release]
lto = "fat"
Expand Down
26 changes: 14 additions & 12 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod announce;
pub mod help;
pub mod invite;
pub mod join;
pub mod leave;
Expand All @@ -12,16 +13,17 @@ pub mod vaporwave;
pub mod zalgo;

pub use crate::commands::{
announce::ANNOUNCE_COMMAND,
invite::INVITE_COMMAND,
join::JOIN_COMMAND,
leave::LEAVE_COMMAND,
movie_quote::MOVIE_QUOTE_COMMAND,
ping::PING_COMMAND,
play::PLAY_COMMAND,
random_tweet::RANDOM_TWEET_COMMAND,
reddit::REDDIT_COMMAND,
stop::STOP_COMMAND,
vaporwave::VAPORWAVE_COMMAND,
zalgo::ZALGO_COMMAND,
announce::announce,
help::help,
invite::invite,
join::join,
leave::leave,
movie_quote::movie_quote,
ping::ping,
play::play,
random_tweet::random_tweet,
reddit::reddit,
stop::stop,
vaporwave::vaporwave,
zalgo::zalgo,
};
67 changes: 52 additions & 15 deletions src/commands/announce.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,71 @@
use crate::CommandContext;
use log::{
info,
warn,
};
use serenity::{
cache::Cache,
client::Context,
framework::standard::{
macros::command,
Args,
CommandResult,
},
http::Http,
model::channel::{
ChannelType,
Message,
},
model::channel::ChannelType,
};

/*
#[command]
#[description("Broadcast a message to robotics members")]
#[allowed_roles("bot")] //TODO: Admin only
#[usage("\"<announcement>\"")]
#[min_args(1)]
#[max_args(1)]
#[example("\"Hello! This is an announcement!\"")]
pub async fn announce(ctx: &Context, _msg: &Message, mut args: Args) -> CommandResult {
let announcement = args.single_quoted::<String>()?;
announce_discord(&ctx.http, &ctx.cache, &announcement).await;
Ok(())
}
*/

async fn has_bot_role(ctx: CommandContext<'_>) -> anyhow::Result<bool> {
let (guild_id, role_id) = {
let guild = match ctx.guild() {
Some(guild) => guild,
None => {
return Ok(false);
}
};

let role_id = guild.roles.iter().find_map(|(id, role)| {
if role.name != "bot" {
return None;
}

Some(*id)
});

let role_id = match role_id {
Some(role_id) => role_id,
None => {
return Ok(false);
}
};

(guild.id, role_id)
};

let has_role = ctx.author().has_role(ctx.http(), guild_id, role_id).await?;

Ok(has_role)
}

/// Broadcast a message to robotics members
#[poise::command(slash_command, check = "has_bot_role")]
pub async fn announce(
ctx: CommandContext<'_>,
#[description = "The announcement"] announcement: String,
) -> anyhow::Result<()> {
let serenity_context = ctx.serenity_context();
announce_discord(
&serenity_context.http,
&serenity_context.cache,
&announcement,
)
.await;
Ok(())
}

pub async fn announce_discord(http: &Http, cache: &Cache, data: &str) {
for guild_id in cache.guilds().into_iter() {
Expand Down
19 changes: 19 additions & 0 deletions src/commands/help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::CommandContext;

/// Get help
#[poise::command(slash_command)]
pub async fn help(
ctx: CommandContext<'_>,
#[description = "Command to get help for"] mut command: Option<String>,
) -> anyhow::Result<()> {
if ctx.invoked_command_name() != "help" {
command = match command {
Some(command) => Some(format!("{} {}", ctx.invoked_command_name(), command)),
None => Some(ctx.invoked_command_name().to_string()),
};
}

poise::builtins::help(ctx, command.as_deref(), Default::default()).await?;

Ok(())
}
21 changes: 7 additions & 14 deletions src/commands/invite.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
use serenity::{
client::Context,
framework::standard::{
macros::command,
Args,
CommandResult,
},
model::channel::Message,
};
use crate::CommandContext;

#[command]
#[description("Get an invite link for this bot")]
pub async fn invite(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
let app_info = ctx.http.get_current_application_info().await?;
/// Get an invite link for this bot
#[poise::command(slash_command)]
pub async fn invite(ctx: CommandContext<'_>) -> anyhow::Result<()> {
let app_info = ctx.http().get_current_application_info().await?;

let id = app_info.id;
let permissions = "1341644225";
let link = format!(
"https://discordapp.com/oauth2/authorize?client_id={id}&scope=bot&permissions={permissions}",
);
msg.channel_id.say(&ctx.http, &link).await?;
ctx.say(link).await?;

Ok(())
}
Loading

0 comments on commit 9fa9f37

Please sign in to comment.