feat: event_manager
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
28
src/event_handlers.rs
Normal file
28
src/event_handlers.rs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
use poise::serenity_prelude as serenity;
|
||||||
|
use crate::Data;
|
||||||
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
|
|
||||||
|
pub async fn sc_event_handler(
|
||||||
|
ctx: &serenity::Context,
|
||||||
|
event: &serenity::FullEvent,
|
||||||
|
_framework: poise::FrameworkContext<'_, Data, Error>,
|
||||||
|
data: &Data,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
match event {
|
||||||
|
serenity::FullEvent::Ready { .. }=> {
|
||||||
|
println!("Bot is ready!");
|
||||||
|
}
|
||||||
|
serenity::FullEvent::Message { new_message } => message_handler(ctx, new_message, data)?,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn message_handler(
|
||||||
|
_ctx: &serenity::Context,
|
||||||
|
msg: &serenity::Message,
|
||||||
|
_data: &Data,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
println!("Received message: {:?}", msg.content);
|
||||||
|
Ok(())
|
||||||
|
}
|
30
src/main.rs
30
src/main.rs
@@ -1,29 +1,37 @@
|
|||||||
use poise::serenity_prelude as serenity;
|
use poise::serenity_prelude as serenity;
|
||||||
|
|
||||||
struct Data {} // User data, which is stored and accessible in all command invocations
|
pub struct Data {} // User data, which is stored and accessible in all command invocations
|
||||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
type Context<'a> = poise::Context<'a, Data, Error>;
|
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||||
|
|
||||||
/// Displays your or another user's account creation date
|
pub mod event_handlers;
|
||||||
|
|
||||||
|
/// Ping command with latency measurement
|
||||||
#[poise::command(slash_command, prefix_command)]
|
#[poise::command(slash_command, prefix_command)]
|
||||||
async fn age(
|
async fn ping(ctx: Context<'_>) -> Result<(), Error> {
|
||||||
ctx: Context<'_>,
|
let original_msg_timestamp = ctx.created_at();
|
||||||
#[description = "Selected user"] user: Option<serenity::User>,
|
let msg = ctx.say("Pong!").await?.into_message().await?;
|
||||||
) -> Result<(), Error> {
|
let latency = msg.timestamp.timestamp_millis() - original_msg_timestamp.timestamp_millis();
|
||||||
let u = user.as_ref().unwrap_or_else(|| ctx.author());
|
ctx.say(format!("Latency: {}ms", latency)).await?;
|
||||||
let response = format!("{}'s account was created at {}", u.name, u.created_at());
|
|
||||||
ctx.say(response).await?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
|
let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
|
||||||
let intents = serenity::GatewayIntents::non_privileged();
|
let intents =
|
||||||
|
serenity::GatewayIntents::non_privileged().union(serenity::GatewayIntents::MESSAGE_CONTENT);
|
||||||
|
|
||||||
let framework = poise::Framework::builder()
|
let framework = poise::Framework::builder()
|
||||||
.options(poise::FrameworkOptions {
|
.options(poise::FrameworkOptions {
|
||||||
commands: vec![age()],
|
prefix_options: poise::PrefixFrameworkOptions {
|
||||||
|
prefix: Some(String::from("!")),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
commands: vec![ping()],
|
||||||
|
event_handler: |ctx, event, framework, data| {
|
||||||
|
Box::pin(event_handlers::sc_event_handler(ctx, event, framework, data))
|
||||||
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.setup(|ctx, _ready, framework| {
|
.setup(|ctx, _ready, framework| {
|
||||||
|
Reference in New Issue
Block a user