Create basic echo bot

This commit is contained in:
Yash Karandikar 2021-12-26 15:13:09 -06:00
parent f59328bd22
commit 3d3921cf04
Signed by: karx
GPG key ID: A794DA2529474BA5
3 changed files with 1497 additions and 2 deletions

1444
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -6,3 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.52"
[dependencies.tokio]
version = "1.15.0"
features = ["full"]
[dependencies.serenity]
version = "0.10"
default-features = false
features = ["builder", "cache", "client", "gateway", "model", "utils", "native_tls_backend"]

View file

@ -1,3 +1,44 @@
fn main() {
println!("Hello, world!");
use std::env;
use serenity::{prelude::*, async_trait, model::{channel::Message, id::ChannelId}, http::CacheHttp};
struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
let nick = {
if let Some(member) = msg.member {
match member.nick {
Some(n) => n,
None => msg.author.name
}
} else {
msg.author.name
}
};
msg.channel_id.say(&ctx, format!("{}: {}", nick, msg.content)).await.unwrap();
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN not set");
let mut client = Client::builder(&token)
.event_handler(Handler)
.await?;
let channel_id = ChannelId(831255708875751477);
client.start().await?;
Ok(())
}
async fn send_message(client: &Client, channel_id: &ChannelId, content: &str) -> anyhow::Result<Message> {
let http = client.cache_and_http.http();
Ok(channel_id.say(http, content).await?)
}