48 lines
1.1 KiB
Rust
48 lines
1.1 KiB
Rust
![]() |
extern crate dotenv;
|
||
|
|
||
|
use std::env;
|
||
|
|
||
|
use serenity::async_trait;
|
||
|
use serenity::model::channel::Message;
|
||
|
use serenity::model::gateway::Ready;
|
||
|
use serenity::prelude::*;
|
||
|
|
||
|
use dotenv::dotenv;
|
||
|
|
||
|
struct Handler;
|
||
|
|
||
|
#[async_trait]
|
||
|
impl EventHandler for Handler {
|
||
|
async fn message(&self, ctx: Context, msg: Message) {
|
||
|
if msg.content == "!ping" {
|
||
|
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
|
||
|
println!("Error sending message: {:?}", why);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async fn ready(&self, _: Context, ready: Ready) {
|
||
|
println!("{} is connected!", ready.user.name);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[tokio::main]
|
||
|
async fn main() {
|
||
|
dotenv().ok();
|
||
|
let token;
|
||
|
let key = "TOKEN";
|
||
|
match env::var(key) {
|
||
|
Ok(val) => token = val,
|
||
|
Err(_) => return,
|
||
|
}
|
||
|
let intents = GatewayIntents::GUILD_MESSAGES
|
||
|
| GatewayIntents::DIRECT_MESSAGES
|
||
|
| GatewayIntents::MESSAGE_CONTENT;
|
||
|
|
||
|
let mut client = Client::builder(&token, intents).event_handler(Handler).await.expect("Error creating client");
|
||
|
|
||
|
if let Err(why) = client.start().await {
|
||
|
println!("Client error: {:?}", why);
|
||
|
}
|
||
|
}
|