Browse Source

initial commit

master
Stephen 2 months ago
commit
1d9a970ad3
6 changed files with 1597 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +1487
    -0
      Cargo.lock
  3. +14
    -0
      Cargo.toml
  4. +2
    -0
      rustfmt.toml
  5. +28
    -0
      src/config.rs
  6. +63
    -0
      src/main.rs

+ 3
- 0
.gitignore View File

@ -0,0 +1,3 @@
/target
.idea
config.toml

+ 1487
- 0
Cargo.lock
File diff suppressed because it is too large
View File


+ 14
- 0
Cargo.toml View File

@ -0,0 +1,14 @@
[package]
name = "cat-disruptor-6500"
version = "0.1.0"
authors = ["Stephen <stephen@stephendownward.ca>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serenity = {version = "0.9", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "static_assertions"] }
tokio = {version = "0.2", features = ["full", "time"] }
phf = { version = "0.8", features = ["macros"] }
toml = "0.5"
serde = { version = "1.0", features = ["derive"] }

+ 2
- 0
rustfmt.toml View File

@ -0,0 +1,2 @@
tab_spaces = 4
hard_tabs = true

+ 28
- 0
src/config.rs View File

@ -0,0 +1,28 @@
use serde::Deserialize;
use std::fs;
#[derive(Deserialize)]
pub struct Config {
pub(crate) token: String,
}
pub fn get_conf() -> Config {
let file_contents = read_conf_file();
match toml::from_str(&file_contents) {
Ok(x) => x,
Err(x) => {
panic!("Error parsing config file: {}", x);
}
}
}
fn read_conf_file() -> String {
let filenames = ["config.toml", "/etc/cat_disruptor_6500/config.toml"];
for filename in filenames.iter() {
if let Ok(x) = fs::read_to_string(filename) {
return x;
}
}
panic!("Could not open config file");
}

+ 63
- 0
src/main.rs View File

@ -0,0 +1,63 @@
mod config;
use phf::phf_map;
use serenity::async_trait;
use serenity::model::channel::{Message, ReactionType};
use serenity::model::prelude::Ready;
use serenity::prelude::*;
use serenity::Client;
use std::str::FromStr;
struct Handler;
static EMOJI_MAP: phf::Map<&'static str, &'static str> = phf_map! {
"cat" => "🐈",
"chicken" => "🐔",
"spaghetti" => "🍝",
"dog" => "🐕",
"bot" => "🤖",
"mango" => "🥭",
"banana" => "🍌",
"bee" => "🐝"
};
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
for (key, value) in EMOJI_MAP.entries() {
let msg_lower = format!(" {} ", msg.content.to_lowercase());
if msg_lower.contains(&format!(" {} ", key))
|| msg_lower.contains(&format!(" {}s ", key))
{
let reaction_type = match ReactionType::from_str(value) {
Ok(x) => x,
Err(x) => {
println!("Could not react: {}", x);
return;
}
};
if let Err(e) = msg.react(&ctx, reaction_type).await {
println!("Error reacting: {}", e);
}
}
}
}
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected", ready.user.name);
}
}
#[tokio::main]
async fn main() {
let config = config::get_conf();
let token = config.token;
let mut client = Client::builder(&token)
.event_handler(Handler)
.await
.expect("Error creating client");
if let Err(e) = client.start().await {
println!("Client error: {}", e);
}
}

Loading…
Cancel
Save