From c2f494581753c13efad47e76fbd67a44ca047579 Mon Sep 17 00:00:00 2001 From: fzorb Date: Sun, 13 Jul 2025 22:40:01 +0300 Subject: [PATCH] basic boilerplate + blatantly steal example like a skid --- .gitignore | 1 + Cargo.toml | 7 +++++++ src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b101c95 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "jon-rs" +version = "0.1.0" +edition = "2024" + +[dependencies] +serenity = "0.12" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..77e4b59 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,58 @@ +use std::env; + +use serenity::async_trait; +use serenity::model::channel::Message; +use serenity::model::gateway::Ready; +use serenity::prelude::*; + +struct Handler; + +#[async_trait] +impl EventHandler for Handler { + // Set a handler for the `message` event. This is called whenever a new message is received. + // + // 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"); + // Set gateway intents, which decides what events the bot will be notified about + let intents = GatewayIntents::GUILD_MESSAGES + | GatewayIntents::DIRECT_MESSAGES + | GatewayIntents::MESSAGE_CONTENT; + + // 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, intents).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