lol
This commit is contained in:
commit
fec0b49e25
4 changed files with 1978 additions and 0 deletions
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
/target
|
||||
*.sqlite
|
||||
*.sqlite-shm
|
||||
*.sqlite-wal
|
||||
.env
|
1817
Cargo.lock
generated
Normal file
1817
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
16
Cargo.toml
Normal file
16
Cargo.toml
Normal file
|
@ -0,0 +1,16 @@
|
|||
[package]
|
||||
name = "discord-kurante"
|
||||
version = "0.1.0"
|
||||
authors = ["Edward Shen <code@eddie.sh>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
clap = "2"
|
||||
serenity = { git = "https://github.com/Lakelezz/serenity", branch = "await"}
|
||||
regex = "1"
|
||||
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "macros", "sqlite"] }
|
||||
futures = "0.3"
|
||||
tokio = { version = "0.2", features = ["full"] }
|
||||
dotenv = "0.15"
|
140
src/main.rs
Normal file
140
src/main.rs
Normal file
|
@ -0,0 +1,140 @@
|
|||
use regex::Regex;
|
||||
use serenity::async_trait;
|
||||
use serenity::client::Client;
|
||||
use serenity::framework::standard::{
|
||||
macros::{command, group},
|
||||
CommandResult, StandardFramework,
|
||||
};
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::prelude::{Context, EventHandler, TypeMapKey};
|
||||
use sqlx::sqlite::SqlitePool;
|
||||
use std::env;
|
||||
|
||||
struct Handler {
|
||||
ah_regex: Regex,
|
||||
best_doctor_regex: Regex,
|
||||
}
|
||||
|
||||
impl Default for Handler {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
ah_regex: Regex::new(r"A+H{5,}").unwrap(),
|
||||
best_doctor_regex: Regex::new(r"[iI].*(?:best|genius) doc").unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn message(&self, ctx: Context, message: Message) {
|
||||
let content = &message.content_safe(ctx.clone()).await;
|
||||
if self.ah_regex.is_match(content) {
|
||||
message
|
||||
.channel_id
|
||||
.say(ctx, "its ok ur gonna get a 6* someday")
|
||||
.await
|
||||
.unwrap();
|
||||
} else if self.best_doctor_regex.is_match(content) {
|
||||
message
|
||||
.channel_id
|
||||
.say(ctx, "no, u is smol brain doctor...")
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DbConnPool {
|
||||
pool: sqlx::pool::Pool<sqlx::sqlite::SqliteConnection>,
|
||||
}
|
||||
impl DbConnPool {
|
||||
async fn get_heck(&self) -> i32 {
|
||||
sqlx::query!("UPDATE Heck SET count = count + 1")
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
sqlx::query!("SELECT count FROM Heck")
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.count
|
||||
}
|
||||
}
|
||||
impl TypeMapKey for DbConnPool {
|
||||
type Value = Self;
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let framework = StandardFramework::new()
|
||||
.configure(|c| c.prefix("~"))
|
||||
.group(&GENERAL_GROUP);
|
||||
|
||||
// Login with a bot token from the environment
|
||||
let mut client =
|
||||
Client::new_with_extras(&env::var("DISCORD_TOKEN").expect("token"), |extras| {
|
||||
extras
|
||||
.event_handler(Handler::default())
|
||||
.framework(framework)
|
||||
})
|
||||
.await
|
||||
.expect("Error creating client");
|
||||
|
||||
let pool = SqlitePool::builder()
|
||||
.build(&env::var("DATABASE_URL").unwrap())
|
||||
.await?;
|
||||
|
||||
init_pool(&pool).await;
|
||||
|
||||
{
|
||||
let mut data = client.data.write().await;
|
||||
data.insert::<DbConnPool>(DbConnPool { pool });
|
||||
}
|
||||
|
||||
// start listening for events by starting a single shard
|
||||
if let Err(why) = client.start().await {
|
||||
println!("An error occurred while running the client: {:?}", why);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn init_pool(pool: &sqlx::pool::Pool<sqlx::sqlite::SqliteConnection>) {
|
||||
sqlx::query!(
|
||||
"CREATE TABLE IF NOT EXISTS Heck (id INTEGER PRIMARY KEY NOT NULL, count INTEGER NOT NULL)"
|
||||
)
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if sqlx::query!("SELECT count FROM Heck")
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
{
|
||||
sqlx::query!("INSERT INTO Heck VALUES (1, 0)")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[group]
|
||||
#[commands(heck)]
|
||||
struct General;
|
||||
#[command]
|
||||
async fn heck(ctx: &mut Context, msg: &Message) -> CommandResult {
|
||||
let db_pool = ctx.data.clone();
|
||||
let mut db_pool = db_pool.write().await;
|
||||
let db_pool = db_pool.get_mut::<DbConnPool>().unwrap();
|
||||
let value = db_pool.get_heck().await;
|
||||
msg.channel_id
|
||||
.say(ctx, format!("This command has been hecked {} times", value))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue