init commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
.env
|
||||||
1693
Cargo.lock
generated
Normal file
1693
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
Cargo.toml
Normal file
19
Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
[package]
|
||||||
|
name = "jackbot"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
dotenv = "0.15"
|
||||||
|
tracing = "0.1.23"
|
||||||
|
tracing-subscriber = "0.2"
|
||||||
|
|
||||||
|
[dependencies.tokio]
|
||||||
|
version = "1.0"
|
||||||
|
features = ["macros", "signal", "rt-multi-thread"]
|
||||||
|
|
||||||
|
[dependencies.serenity]
|
||||||
|
version = "0.11.5"
|
||||||
|
features = ["cache", "framework", "standard_framework", "rustls_backend"]
|
||||||
161
src/main.rs
Normal file
161
src/main.rs
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
//! 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"]
|
||||||
|
//! ```
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::env;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use serenity::{async_trait, framework::standard::macros::command};
|
||||||
|
use serenity::client::bridge::gateway::ShardManager;
|
||||||
|
use serenity::framework::standard::macros::group;
|
||||||
|
use serenity::framework::standard::{ CommandResult, StandardFramework};
|
||||||
|
use serenity::http::Http;
|
||||||
|
use serenity::model::channel::Message;
|
||||||
|
use serenity::model::event::ResumedEvent;
|
||||||
|
use serenity::model::gateway::Ready;
|
||||||
|
use serenity::prelude::*;
|
||||||
|
use tracing::{error, info};
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
pub struct ShardManagerContainer;
|
||||||
|
|
||||||
|
impl TypeMapKey for ShardManagerContainer {
|
||||||
|
type Value = Arc<Mutex<ShardManager>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Time;
|
||||||
|
|
||||||
|
impl TypeMapKey for Time {
|
||||||
|
type Value = Arc<Mutex<Instant>>;
|
||||||
|
}
|
||||||
|
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(jackon, jackoff, quit)]
|
||||||
|
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 http = Http::new(&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("!")).group(&GENERAL_GROUP);
|
||||||
|
|
||||||
|
let intents = GatewayIntents::GUILD_MESSAGES
|
||||||
|
| GatewayIntents::DIRECT_MESSAGES
|
||||||
|
| GatewayIntents::MESSAGE_CONTENT;
|
||||||
|
let mut client = Client::builder(&token, intents)
|
||||||
|
.framework(framework)
|
||||||
|
.event_handler(Handler)
|
||||||
|
.await
|
||||||
|
.expect("Err creating client");
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut data = client.data.write().await;
|
||||||
|
data.insert::<ShardManagerContainer>(client.shard_manager.clone());
|
||||||
|
|
||||||
|
data.insert::<Time>(Arc::new(Mutex::new(Instant::now())));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[command]
|
||||||
|
#[owners_only]
|
||||||
|
async fn quit(ctx: &Context, msg: &Message) -> CommandResult {
|
||||||
|
let data = ctx.data.read().await;
|
||||||
|
|
||||||
|
if let Some(manager) = data.get::<ShardManagerContainer>() {
|
||||||
|
msg.reply(ctx, "Shutting down!").await?;
|
||||||
|
manager.lock().await.shutdown_all().await;
|
||||||
|
} else {
|
||||||
|
msg.reply(ctx, "There was a problem getting the shard manager").await?;
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[command]
|
||||||
|
async fn jackon(ctx: &Context, msg: &Message) -> CommandResult {
|
||||||
|
let count = {
|
||||||
|
let data_read = ctx.data.read().await;
|
||||||
|
data_read.get::<Time>().expect("Expected Time in TypeMap.").clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut instant = count.lock().await;
|
||||||
|
*instant = Instant::now();
|
||||||
|
|
||||||
|
let str = "Jack started baby!";
|
||||||
|
msg.channel_id.say(&ctx.http, str).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[command]
|
||||||
|
async fn jackoff(ctx: &Context, msg: &Message) -> CommandResult {
|
||||||
|
let raw_count = {
|
||||||
|
let data_read = ctx.data.read().await;
|
||||||
|
data_read.get::<Time>().expect("Expected Time in TypeMap.").clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
let time = raw_count.lock().await;
|
||||||
|
let str = format!("That was a good sesh Took ya: {} sec", time.elapsed().as_secs_f64());
|
||||||
|
msg.channel_id.say(&ctx.http, str).await?;
|
||||||
|
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user