//! 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 commands::{kanji::*, owner::*}; use serenity::{ async_trait, client::bridge::gateway::ShardManager, framework::{standard::macros::group, StandardFramework}, http::Http, model::{event::ResumedEvent, gateway::Ready}, prelude::*, }; use tracing::{error, info}; pub struct ShardManagerContainer; impl TypeMapKey for ShardManagerContainer { type Value = Arc>; } struct Handler; #[async_trait] impl EventHandler for Handler { async fn ready(&self, _: Context, ready: Ready) { info!("Connected as {}", ready.user.name); } async fn resume(&self, _: Context, _: ResumedEvent) { info!("Resumed"); } } #[group] #[commands(joyo, jinmeiyo, kyoiku, jlpt, hyogai, sleep)] struct General; #[tokio::main] async 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. dotenv::dotenv().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`. tracing_subscriber::fmt::init(); let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); let prefix = env::var("PREFIX").expect("Expected a prefix in the environment"); let http = Http::new_with_token(&token); // We will fetch your bot's owners and id let (owners, _bot_id) = match http.get_current_application_info().await { Ok(info) => { let mut owners = HashSet::new(); owners.insert(info.owner.id); (owners, info.id) }, Err(why) => panic!("Could not access application info: {:?}", why), }; // Create the framework let framework = StandardFramework::new().configure(|c| c.owners(owners).prefix(prefix)).group(&GENERAL_GROUP); let mut client = Client::builder(&token) .framework(framework) .event_handler(Handler) .await .expect("Err creating client"); { let mut data = client.data.write().await; data.insert::(client.shard_manager.clone()); } let shard_manager = client.shard_manager.clone(); tokio::spawn(async move { tokio::signal::ctrl_c().await.expect("Could not register ctrl+c handler"); shard_manager.lock().await.shutdown_all().await; }); if let Err(why) = client.start().await { error!("Client error: {:?}", why); } }