From 640e0e475cb4031eec2f994956b57c890e3e04f6 Mon Sep 17 00:00:00 2001 From: Michael Matthews Date: Mon, 12 Jul 2021 20:29:40 -0400 Subject: [PATCH] have demo code working now --- Cargo.lock | 47 +++++++++++++++++--------------------- Cargo.toml | 3 ++- src/main.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 86 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 999de97..6a0b5cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,6 +5,7 @@ name = "Osselbot" version = "0.1.0" dependencies = [ "serenity", + "tokio", ] [[package]] @@ -123,17 +124,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "command_attr" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a6c3666f685cb1efc0628b8c984dbad9c372d080450736c7732089c385ed81d" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "cpufeatures" version = "0.1.5" @@ -326,6 +316,15 @@ version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "http" version = "0.2.4" @@ -559,6 +558,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "once_cell" version = "1.8.0" @@ -822,19 +831,16 @@ dependencies = [ "bitflags", "bytes 1.0.1", "chrono", - "command_attr", "flate2", "futures", "percent-encoding", "reqwest", "serde", "serde_json", - "static_assertions", "tokio", "tracing", "typemap_rev", "url", - "uwl", ] [[package]] @@ -872,12 +878,6 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "syn" version = "1.0.73" @@ -926,6 +926,7 @@ dependencies = [ "libc", "memchr", "mio", + "num_cpus", "pin-project-lite", "tokio-macros", "winapi", @@ -1100,12 +1101,6 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" -[[package]] -name = "uwl" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4bf03e0ca70d626ecc4ba6b0763b934b6f2976e8c744088bb3c1d646fbb1ad0" - [[package]] name = "version_check" version = "0.9.3" diff --git a/Cargo.toml b/Cargo.toml index fba3d73..3147f91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serenity = "0.10" +serenity = { version = "0.10.8", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] } +tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } diff --git a/src/main.rs b/src/main.rs index c61411a..c42cdaf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,64 @@ -#![allow(non_snake_case)] -fn main() { - println!("Hello, world!"); +#![allow(non_snake_case)] //Osselbot doesn't follow snake case so we need this + +use std::env; + +use serenity::{ + async_trait, + model::{channel::Message, gateway::Ready}, + prelude::*, +}; + +struct Handler; + +#[async_trait] +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. + async fn message(&self, ctx: Context, msg: Message) { + if msg.content == "!ping" { + // 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. + if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await { + 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. + async fn ready(&self, _: Context, ready: Ready) { + println!("{} is connected!", ready.user.name); + } } + +#[tokio::main] +async 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"); + + // 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::builder(&token) + .event_handler(Handler) + .await + .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().await { + println!("Client error: {:?}", why); + } +} \ No newline at end of file