From f14ba9816bf32c7998a97a9cb55565a9e9a9bae5 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Tue, 4 Aug 2020 20:31:39 +0200 Subject: Initial commit --- 07_sample_bot_structure/.env.example | 10 +++ 07_sample_bot_structure/Cargo.toml | 14 ++++ 07_sample_bot_structure/src/commands/math.rs | 18 +++++ 07_sample_bot_structure/src/commands/meta.rs | 13 ++++ 07_sample_bot_structure/src/commands/mod.rs | 3 + 07_sample_bot_structure/src/commands/owner.rs | 25 +++++++ 07_sample_bot_structure/src/main.rs | 96 +++++++++++++++++++++++++++ 7 files changed, 179 insertions(+) create mode 100644 07_sample_bot_structure/.env.example create mode 100644 07_sample_bot_structure/Cargo.toml create mode 100644 07_sample_bot_structure/src/commands/math.rs create mode 100644 07_sample_bot_structure/src/commands/meta.rs create mode 100644 07_sample_bot_structure/src/commands/mod.rs create mode 100644 07_sample_bot_structure/src/commands/owner.rs create mode 100644 07_sample_bot_structure/src/main.rs (limited to '07_sample_bot_structure') diff --git a/07_sample_bot_structure/.env.example b/07_sample_bot_structure/.env.example new file mode 100644 index 0000000..3b3b4ac --- /dev/null +++ b/07_sample_bot_structure/.env.example @@ -0,0 +1,10 @@ +# This declares an environment variable named "DISCORD_TOKEN" with the given +# value. When calling `kankyo::load()`, it will read the `.env` file and parse +# these key-value pairs and insert them into the environment. +# +# Environment variables are separated by newlines and must not have space +# around the equals sign (`=`). +DISCORD_TOKEN=put your token here +# Declares the level of logging to use. Read the documentation for the `log` +# and `env_logger` crates for more information. +RUST_LOG=debug diff --git a/07_sample_bot_structure/Cargo.toml b/07_sample_bot_structure/Cargo.toml new file mode 100644 index 0000000..7660cc0 --- /dev/null +++ b/07_sample_bot_structure/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "07_sample_bot_structure" +version = "0.1.0" +authors = ["my name "] +edition = "2018" + +[dependencies] +env_logger = "0.6" +kankyo = "0.2" +log = "0.4" + +[dependencies.serenity] +features = ["cache", "framework", "standard_framework", "rustls_backend"] +path = "../../" diff --git a/07_sample_bot_structure/src/commands/math.rs b/07_sample_bot_structure/src/commands/math.rs new file mode 100644 index 0000000..ce7beed --- /dev/null +++ b/07_sample_bot_structure/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::().unwrap(); + let two = args.single::().unwrap(); + + let product = one * two; + + let _ = msg.channel_id.say(&ctx.http, product); + + Ok(()) +} diff --git a/07_sample_bot_structure/src/commands/meta.rs b/07_sample_bot_structure/src/commands/meta.rs new file mode 100644 index 0000000..d58af10 --- /dev/null +++ b/07_sample_bot_structure/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/07_sample_bot_structure/src/commands/mod.rs b/07_sample_bot_structure/src/commands/mod.rs new file mode 100644 index 0000000..9c5dfaa --- /dev/null +++ b/07_sample_bot_structure/src/commands/mod.rs @@ -0,0 +1,3 @@ +pub mod math; +pub mod meta; +pub mod owner; diff --git a/07_sample_bot_structure/src/commands/owner.rs b/07_sample_bot_structure/src/commands/owner.rs new file mode 100644 index 0000000..cebb759 --- /dev/null +++ b/07_sample_bot_structure/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::() { + 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/07_sample_bot_structure/src/main.rs b/07_sample_bot_structure/src/main.rs new file mode 100644 index 0000000..2c3a594 --- /dev/null +++ b/07_sample_bot_structure/src/main.rs @@ -0,0 +1,96 @@ +//! Requires the 'framework' feature flag be enabled in your project's +//! `Cargo.toml`. +//! +//! This can be enabled by specifying the feature in the dependency section: +//! +//! ```toml +//! [dependencies.serenity] +//! git = "https://github.com/serenity-rs/serenity.git" +//! features = ["framework", "standard_framework"] +//! ``` +mod commands; + +use std::{ + collections::HashSet, + env, + sync::Arc, +}; +use serenity::{ + client::bridge::gateway::ShardManager, + framework::{ + StandardFramework, + standard::macros::group, + }, + model::{event::ResumedEvent, gateway::Ready}, + prelude::*, +}; +use log::{error, info}; + +use commands::{ + math::*, + meta::*, + owner::*, +}; +struct ShardManagerContainer; + +impl TypeMapKey for ShardManagerContainer { + type Value = Arc>; +} + +struct Handler; + +impl EventHandler for Handler { + fn ready(&self, _: Context, ready: Ready) { + info!("Connected as {}", ready.user.name); + } + + fn resume(&self, _: Context, _: ResumedEvent) { + info!("Resumed"); + } +} + +#[group] +#[commands(multiply, ping, quit)] +struct General; + +fn main() { + // This will load the environment variables located at `./.env`, relative to + // the CWD. See `./.env.example` for an example on how to structure this. + kankyo::load().expect("Failed to load .env file"); + + // Initialize the logger to use environment variables. + // + // In this case, a good default is setting the environment variable + // `RUST_LOG` to debug`. + env_logger::init(); + + let token = env::var("DISCORD_TOKEN") + .expect("Expected a token in the environment"); + + let mut client = Client::new(&token, Handler).expect("Err creating client"); + + { + let mut data = client.data.write(); + data.insert::(Arc::clone(&client.shard_manager)); + } + + let owners = match client.cache_and_http.http.get_current_application_info() { + Ok(info) => { + let mut set = HashSet::new(); + set.insert(info.owner.id); + + set + }, + Err(why) => panic!("Couldn't get application info: {:?}", why), + }; + + client.with_framework(StandardFramework::new() + .configure(|c| c + .owners(owners) + .prefix("~")) + .group(&GENERAL_GROUP)); + + if let Err(why) = client.start() { + error!("Client error: {:?}", why); + } +} -- cgit v1.2.3-54-g00ecf