start making bot

This commit is contained in:
Gzod01 2024-04-17 21:34:59 +02:00
parent beb953592c
commit 9622c3d0bc
2 changed files with 60 additions and 2 deletions

1
.env Normal file
View file

@ -0,0 +1 @@
DISCORD_TOKEN=MTIzMDE2NjI2NTM3NzU5MTM5Ng.GVqsyl.ZvP2Kigd5VgP4eITdGqAGYfDw58YeXJYSJg4-A

View file

@ -1,3 +1,60 @@
fn main() {
println!("Hello, world!");
use std::env;
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::prelude::*;
struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content.starts_with("!") {
let command_with_exclam: &str = msg.content.strip_prefix("!").unwrap();
if command_with_exclam == "ping" {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
println!("Error sending message: {why:?}");
}
} else {
println!("unknown command {command_with_exclam:?}");
}
}
if ["hello", "hi", "bonjour", "hola", "halo"]
.iter()
.any(|&s| msg.content.to_lowercase().contains(s))
{
if let Err(why) = msg.react(ctx, '👋').await {
println!("Error reacting message {why:?}");
}
}
}
// async fn interaction_create(&self, ctx: Context, interaction: Interaction){
// }
}
#[tokio::main]
async fn main() {
// Login with a bot token from 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.
let mut client = Client::builder(&token, intents)
.event_handler(Handler)
.await
.expect("Err creating client");
// Start listening for events by starting a single shard
if let Err(why) = client.start().await {
println!("Client error: {why:?}");
}
}
// fn main(){
// println!("helloworldf4a64fra984");
// }