Skip to content

Instantly share code, notes, and snippets.

@Tsugami
Last active June 29, 2022 21:34
Show Gist options
  • Save Tsugami/98f6179c497267cddd8472d2bcbda20c to your computer and use it in GitHub Desktop.
Save Tsugami/98f6179c497267cddd8472d2bcbda20c to your computer and use it in GitHub Desktop.
events in poise
use poise::serenity_prelude::{self as serenity};
extern crate dotenv;
use dotenv::dotenv;
struct Data {}
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
async fn listener(_ctx: &serenity::Context, event: &poise::Event<'_>) -> Result<(), Error> {
match event {
poise::Event::Ready { data_about_bot } => {
println!("{} is ready!", data_about_bot.user.name);
Ok(())
},
poise::Event::GuildMemberAddition { new_member } => {
println!("{} joined {}", new_member.user.name, new_member.guild_id);
Ok(())
},
_ => Ok(()),
}
}
/// Display your or another user's account creation date
#[poise::command(prefix_command, slash_command, track_edits)]
async fn age(
ctx: Context<'_>,
#[description = "Selected user"] user: Option<serenity::User>,
) -> Result<(), Error> {
let user = user.as_ref().unwrap_or(ctx.author());
ctx.say(format!(
"{}'s account was created at {}",
user.name,
user.created_at()
))
.await?;
Ok(())
}
#[poise::command(prefix_command)]
async fn register(ctx: Context<'_>) -> Result<(), Error> {
poise::builtins::register_application_commands_buttons(ctx).await?;
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Error> {
dotenv().ok();
let discord_token = std::env::var("DISCORD_BOT_TOKEN").expect("discord token not found");
let intents = serenity::GatewayIntents::MESSAGE_CONTENT
| serenity::GatewayIntents::GUILD_MEMBERS
| serenity::GatewayIntents::GUILD_MESSAGES;
poise::Framework::build()
.token(&discord_token)
.intents(intents)
.user_data_setup(move |_ctx, _ready, _framework| Box::pin(async move { Ok(Data {}) }))
.options(poise::FrameworkOptions {
// configure framework here
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("~".into()),
..Default::default()
},
listener: |ctx, event, _framework, _state| Box::pin(listener(ctx, event)),
commands: vec![age(), register()],
..Default::default()
})
.run()
.await
.unwrap();
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment