diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/math.rs | 18 | ||||
-rw-r--r-- | src/commands/meta.rs | 13 | ||||
-rw-r--r-- | src/commands/mod.rs | 3 | ||||
-rw-r--r-- | src/commands/owner.rs | 25 | ||||
-rw-r--r-- | src/main.rs | 71 |
5 files changed, 130 insertions, 0 deletions
diff --git a/src/commands/math.rs b/src/commands/math.rs new file mode 100644 index 0000000..ce7beed --- /dev/null +++ b/src/commands/math.rs @@ -0,0 +1,18 @@ +use serenity::prelude::*; +use serenity::model::prelude::*; +use serenity::framework::standard::{ + Args, CommandResult, + macros::command, +}; + +#[command] +pub fn multiply(ctx: &mut Context, msg: &Message, mut args: Args) -> CommandResult { + let one = args.single::<f64>().unwrap(); + let two = args.single::<f64>().unwrap(); + + let product = one * two; + + let _ = msg.channel_id.say(&ctx.http, product); + + Ok(()) +} diff --git a/src/commands/meta.rs b/src/commands/meta.rs new file mode 100644 index 0000000..d58af10 --- /dev/null +++ b/src/commands/meta.rs @@ -0,0 +1,13 @@ +use serenity::prelude::*; +use serenity::model::prelude::*; +use serenity::framework::standard::{ + CommandResult, + macros::command, +}; + +#[command] +fn ping(ctx: &mut Context, msg: &Message) -> CommandResult { + let _ = msg.channel_id.say(&ctx.http, "Pong!"); + + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..9c5dfaa --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1,3 @@ +pub mod math; +pub mod meta; +pub mod owner; diff --git a/src/commands/owner.rs b/src/commands/owner.rs new file mode 100644 index 0000000..cebb759 --- /dev/null +++ b/src/commands/owner.rs @@ -0,0 +1,25 @@ +use crate::ShardManagerContainer; +use serenity::prelude::*; +use serenity::model::prelude::*; +use serenity::framework::standard::{ + CommandResult, + macros::command, +}; + +#[command] +#[owners_only] +fn quit(ctx: &mut Context, msg: &Message) -> CommandResult { + let data = ctx.data.read(); + + if let Some(manager) = data.get::<ShardManagerContainer>() { + manager.lock().shutdown_all(); + } else { + let _ = msg.reply(&ctx, "There was a problem getting the shard manager"); + + return Ok(()); + } + + let _ = msg.reply(&ctx, "Shutting down!"); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..42855c3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,71 @@ +use rand::Rng; +use std::env; + +use serenity::{ + model::{channel::Message, gateway::Ready}, + prelude::*, +}; + +struct Handler; + +impl EventHandler for Handler { + // Set a handler for the `message` event - so that whenever a new message + // is received - the closure (or function) passed will be called. + // + // Event handlers are dispatched through a threadpool, and so multiple + // events can be dispatched simultaneously. + fn message(&self, ctx: Context, msg: Message) { + if msg.content[0..2] == *"!s" { + // Sending a message can fail, due to a network error, an + // authentication error, or lack of permissions to post in the + // channel, so log to stdout when some error happens, with a + // description of it. + println!("{}", &msg.content[3..]); + let arg: u32 = msg.content[3..].parse().unwrap(); + let mut rng = rand::thread_rng(); + let mut answer = String::from("```md\n"); + let mut hits = 0; + for _ in 0..arg { + let rn = rng.gen_range(1, 7); + if rn >= 5 { + hits += 1; + } + answer = format!("{}[{}] ", answer.clone(), rn); + } + let answer = format!("{}\nhits: {}\n```", answer, hits); + if let Err(why) = msg.channel_id.say(&ctx.http, answer) { + println!("Error sending message: {:?}", why); + } + } + } + + // Set a handler to be called on the `ready` event. This is called when a + // shard is booted, and a READY payload is sent by Discord. This payload + // contains data like the current user's guild Ids, current user data, + // private channels, and more. + // + // In this case, just print what the current user's username is. + fn ready(&self, _: Context, ready: Ready) { + println!("{} is connected!", ready.user.name); + } +} + +fn main() { + // Configure the client with your Discord bot token in the environment. + //let token = env::var("DISCORD_TOKEN") + // .expect("Expected a token in the environment"); + let token = "Mjk0NTU0MDU4Nzg4NzAwMTYx.XmfrfA.l8MmCKYem6IBlYOjViUeT9tMSaE"; + + // Create a new instance of the Client, logging in as a bot. This will + // automatically prepend your bot token with "Bot ", which is a requirement + // by Discord for bot users. + let mut client = Client::new(&token, Handler).expect("Err creating client"); + + // Finally, start a single shard, and start listening to events. + // + // Shards will automatically attempt to reconnect, and will perform + // exponential backoff until it reconnects. + if let Err(why) = client.start() { + println!("Client error: {:?}", why); + } +} |