added count command
This commit is contained in:
46
src/commands/count.rs
Normal file
46
src/commands/count.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use regex::Regex;
|
||||
use serenity::{
|
||||
framework::standard::{macros::command, Args, CommandResult},
|
||||
model::prelude::*,
|
||||
prelude::*,
|
||||
};
|
||||
use std::{collections::HashMap, env};
|
||||
use tokio_postgres::NoTls;
|
||||
|
||||
#[command]
|
||||
pub async fn kitna(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||
let query: String = args.single::<String>()?;
|
||||
let words: HashMap<&str, Regex> = [
|
||||
("nword", Regex::new(r"(?i)(nig+(er|a)|nig{2,})").unwrap()),
|
||||
("acha", Regex::new(r"(?i)a(c??h??|6??)a+").unwrap()),
|
||||
("sus", Regex::new(r"(?i)sus|(?i)amon?g\s?us").unwrap()),
|
||||
]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect();
|
||||
let db: String = env::var("DB_URL").expect("bhay DB_URL daal na");
|
||||
let (client, conn) = tokio_postgres::connect(&db, NoTls)
|
||||
.await
|
||||
.expect("cant connect bha");
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = conn.await {
|
||||
eprintln!("connection error: {}", e);
|
||||
}
|
||||
});
|
||||
let id = msg.author.id.as_u64().to_owned().to_string();
|
||||
for (name, regex) in words {
|
||||
if regex.is_match(&query) || &query == name {
|
||||
let query_result = client
|
||||
.query_one(
|
||||
format!("SELECT count FROM user{} where name='{}'", id, name).as_str(),
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect("cant select the count");
|
||||
let count: i32 = query_result.get("count");
|
||||
msg.reply(ctx, format!("{} count for you: {}", name, count)).await?;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
@@ -1 +1,2 @@
|
||||
pub mod general;
|
||||
pub mod count;
|
||||
|
71
src/handler/count.rs
Normal file
71
src/handler/count.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use serenity::model::channel::Message;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use regex::Regex;
|
||||
use tokio_postgres::NoTls;
|
||||
|
||||
pub async fn count(msg: Message) {
|
||||
let words: HashMap<&str, Regex> = [
|
||||
("nword", Regex::new(r"(?i)(nig+(er|a)|nig{2,})").unwrap()),
|
||||
("acha", Regex::new(r"(?i)a(c??h??|6??)a+").unwrap()),
|
||||
("sus", Regex::new(r"(?i)sus|(?i)amon?g\s?us").unwrap()),
|
||||
]
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect();
|
||||
let db: String = env::var("DB_URL").expect("bhay DB_URL daal na");
|
||||
let (client, conn) = tokio_postgres::connect(&db, NoTls)
|
||||
.await
|
||||
.expect("cant connect bha");
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = conn.await {
|
||||
eprintln!("connection error: {}", e);
|
||||
}
|
||||
});
|
||||
let id = msg.author.id.as_u64().to_owned().to_string();
|
||||
client
|
||||
.execute(
|
||||
format!(
|
||||
"
|
||||
CREATE TABLE IF NOT EXISTS user{} (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR NOT NULL,
|
||||
count INTEGER NOT NULL
|
||||
)
|
||||
",
|
||||
id
|
||||
)
|
||||
.as_str(),
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect("cant create table");
|
||||
|
||||
for name in ["nword", "acha", "sus"] {
|
||||
if words[name].is_match(&msg.content) {
|
||||
let query_result = client
|
||||
.query(
|
||||
format!("SELECT count FROM user{} where name='{}'", id, name).as_str(),
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect("cant select the count");
|
||||
if query_result.is_empty() {
|
||||
client
|
||||
.execute(
|
||||
format!("insert into user{} (name, count) values ('{}', 0)", id, name).as_str(),
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect("cant insert shit");
|
||||
}
|
||||
client
|
||||
.execute(
|
||||
format!("UPDATE user{} SET count = count + 1 where name='{}'", id, name).as_str(),
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect("cant update");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,9 +1,11 @@
|
||||
mod interactions;
|
||||
mod count;
|
||||
use serenity::{
|
||||
async_trait,
|
||||
model::{
|
||||
event::ResumedEvent,
|
||||
gateway::Ready,
|
||||
channel::Message,
|
||||
interactions::{
|
||||
ApplicationCommand, Interaction, InteractionData, InteractionResponseType,
|
||||
InteractionType,
|
||||
@@ -27,6 +29,10 @@ impl EventHandler for Handler {
|
||||
async fn resume(&self, _: Context, _: ResumedEvent) {
|
||||
info!("how th when the");
|
||||
}
|
||||
async fn message(&self, _: Context, msg: Message) {
|
||||
count::count(msg).await;
|
||||
}
|
||||
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
if interaction.kind == InteractionType::ApplicationCommand {
|
||||
if let Some(InteractionData::ApplicationCommand(data)) = interaction.data.as_ref() {
|
||||
|
@@ -10,6 +10,7 @@ use std::{collections::HashSet, env, sync::Arc};
|
||||
use tracing::error;
|
||||
pub struct ShardManagerContainer;
|
||||
use commands::general::*;
|
||||
use commands::count::*;
|
||||
use handler::Handler;
|
||||
|
||||
impl TypeMapKey for ShardManagerContainer {
|
||||
@@ -20,6 +21,10 @@ impl TypeMapKey for ShardManagerContainer {
|
||||
#[commands(ping)]
|
||||
struct General;
|
||||
|
||||
#[group]
|
||||
#[commands(kitna)]
|
||||
struct Count;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let token = env::var("DISCORD_TOKEN").expect("Token daal madarchod");
|
||||
@@ -38,7 +43,8 @@ async fn main() {
|
||||
|
||||
let framework = StandardFramework::new()
|
||||
.configure(|c| c.owners(owners).prefix("xx"))
|
||||
.group(&GENERAL_GROUP);
|
||||
.group(&GENERAL_GROUP)
|
||||
.group(&COUNT_GROUP);
|
||||
|
||||
let mut client = Client::builder(&token)
|
||||
.framework(framework)
|
||||
|
Reference in New Issue
Block a user