Merge pull request #3 from ZyLacx/sqlx
Sqlx addition, simple queries for events and birthdays
This commit is contained in:
commit
95d6899201
7 changed files with 96 additions and 52 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -9,3 +9,5 @@
|
||||||
|
|
||||||
# IDE config
|
# IDE config
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
|
mooverdb.db
|
|
@ -16,10 +16,8 @@ serenity = { version = "0.12.0", default-features = false, features = ["client",
|
||||||
serenity_utils = "0.7.0"
|
serenity_utils = "0.7.0"
|
||||||
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
|
||||||
regex = "1.9.0"
|
regex = "1.9.0"
|
||||||
# sqlite = "0.32.0"
|
|
||||||
# async-sqlite = { version = "0.2.1", default-features = false }
|
|
||||||
async-rusqlite = "0.4.0"
|
|
||||||
chrono = "0.4.31"
|
chrono = "0.4.31"
|
||||||
|
sqlx = {version="0.7.3", features=["runtime-tokio", "sqlite"]}
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
DEBUG = []
|
DEBUG = []
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -34,11 +34,12 @@ impl EventHandler for Handler {
|
||||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||||
println!("{} is connected!", ready.user.name);
|
println!("{} is connected!", ready.user.name);
|
||||||
|
|
||||||
|
#[cfg(feature="RELEASE")] {
|
||||||
|
use util::debug::hello;
|
||||||
|
hello(ctx.http.clone()).await;
|
||||||
|
}
|
||||||
|
|
||||||
// use util::debug::hello;
|
// notice::notice_wrapper(ctx).await;
|
||||||
// hello(ctx.http.clone()).await;
|
|
||||||
|
|
||||||
notice::notice_wrapper(ctx).await;
|
|
||||||
|
|
||||||
// let scheduler = every(1).day().at(13, 30, 0)
|
// let scheduler = every(1).day().at(13, 30, 0)
|
||||||
// .perform(|| async {
|
// .perform(|| async {
|
||||||
|
@ -71,7 +72,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
#[cfg(feature="DEBUG")]
|
#[cfg(feature="DEBUG")]
|
||||||
let token_str = "DEBUGTOKEN";
|
let token_str = "DEBUGTOKEN";
|
||||||
|
|
||||||
let token = dotenv_var(token_str).context("No TOKEN in env")?;
|
let token = dotenv_var(token_str).context("TOKEN not found in env")?;
|
||||||
|
|
||||||
let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
|
let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ async fn response(http: Arc<Http>, msg: Message) -> bool {
|
||||||
const RESPONSES: [&str; 4] = [
|
const RESPONSES: [&str; 4] = [
|
||||||
"To som jaaa",
|
"To som jaaa",
|
||||||
"Henloooo",
|
"Henloooo",
|
||||||
"Čo môj?",
|
"No čo je?",
|
||||||
"Hm?"
|
"Hm?"
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
0
src/other/embeds.rs
Normal file
0
src/other/embeds.rs
Normal file
|
@ -1,67 +1,110 @@
|
||||||
use serenity::prelude::*;
|
use chrono::{Datelike, Local};
|
||||||
use chrono::{Local, Datelike};
|
use serenity::{all::{GuildId, UserId}, http::Http, prelude::*};
|
||||||
|
|
||||||
// use async_sqlite::{rusqlite::{Rows, Statement}, ClientBuilder, JournalMode};
|
|
||||||
|
|
||||||
use crate::util::debug::send_error;
|
use crate::util::debug::send_error;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Ok;
|
||||||
|
|
||||||
use async_rusqlite::Connection;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::util::security::dotenv_var;
|
||||||
|
|
||||||
|
use sqlx::{FromRow, Connection, SqliteConnection};
|
||||||
|
|
||||||
pub async fn notice_wrapper(ctx: Context) {
|
pub async fn notice_wrapper(ctx: Context) {
|
||||||
match notice(ctx.clone()).await {
|
match notice(ctx.clone()).await {
|
||||||
Ok(_) => return,
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
send_error(ctx.http.clone(), e.to_string()).await;
|
send_error(ctx.http.clone(), e.to_string()).await;
|
||||||
return
|
return;
|
||||||
}
|
},
|
||||||
|
Result::Ok(_) => return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BirtdayRow {
|
async fn announce_event(guild_id: GuildId, name: &str, special_message: &str, http: Arc<Http>) {
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn celebrate_birthday(guild_id: GuildId, user_id: UserId, nick: &str, http: Arc<Http>) {
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, FromRow, Debug)]
|
||||||
|
struct BirthdayRow {
|
||||||
|
#[sqlx(try_from="i64")]
|
||||||
|
id: u64,
|
||||||
day: u8,
|
day: u8,
|
||||||
month: u8,
|
month: u8,
|
||||||
nick: String
|
nick: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn notice(ctx: Context) -> anyhow::Result<()> {
|
#[derive(Clone, FromRow, Debug)]
|
||||||
|
struct EventRow {
|
||||||
|
#[sqlx(try_from="i64")]
|
||||||
|
guild: u64,
|
||||||
|
name: String,
|
||||||
|
day: u8,
|
||||||
|
month: u8,
|
||||||
|
special_message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn notice(ctx: serenity::client::Context) -> anyhow::Result<()> {
|
||||||
|
use anyhow::Context;
|
||||||
|
|
||||||
let local = Local::now();
|
let local = Local::now();
|
||||||
let day = local.day();
|
let day = local.day();
|
||||||
let month = local.month();
|
let month = local.month();
|
||||||
|
|
||||||
|
let db_path = dotenv_var("DATABASE_URL").context("DATABASE_URL not found in env")?;
|
||||||
|
|
||||||
let conn = Connection::open("my.db").await?;
|
let mut db = SqliteConnection::connect(db_path.as_str()).await?;
|
||||||
|
|
||||||
let stmt = conn.call(move |conn| {
|
let birtdays = sqlx::query_as::<_, BirthdayRow>(
|
||||||
conn.prepare("SELECT * FROM birthdays WHERE day=6 AND month=3;")
|
"SELECT * FROM birthdays
|
||||||
}).await?;
|
WHERE day=? AND month=?;"
|
||||||
|
)
|
||||||
|
.bind(day)
|
||||||
|
.bind(month)
|
||||||
|
.fetch_all(&mut db)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// let result = Vec::new();
|
let global_events = sqlx::query_as::<_, EventRow>(
|
||||||
// let stmt = client.conn(|conn| {
|
"SELECT guild, name, day, month, specialMessage from events
|
||||||
// // conn.prepare(format!("SELECT * FROM {db} WHERE day={day} AND month={month};").as_str());
|
WHERE day=? AND month=? AND guild=0;"
|
||||||
// conn.prepare("SELECT * FROM birthdays WHERE day=6 AND month=3;")
|
)
|
||||||
// }).await?;
|
.bind(day)
|
||||||
|
.bind(month)
|
||||||
|
.fetch_all(&mut db)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let rows = stmt.query([])?;
|
let guilds = ctx.http.get_guilds(None, None).await?;
|
||||||
|
|
||||||
let result: Vec<BirtdayRow> = Vec::new();
|
for guild in guilds {
|
||||||
while let Some(row) = rows.next()? {
|
let guild_id = guild.id;
|
||||||
let bd = BirtdayRow {
|
|
||||||
day: row.get(1)?,
|
for bd in &birtdays {
|
||||||
month: row.get(2)?,
|
let user_id = UserId::new(bd.id);
|
||||||
nick: row.get(3)?
|
guild_id.member(ctx.http(), user_id).await?;
|
||||||
};
|
|
||||||
result.push(bd);
|
celebrate_birthday(guild_id, user_id, bd.nick.as_str(), ctx.http.clone()).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("VALUE: {:?}", result[0].day);
|
for e in &global_events {
|
||||||
// for db in ["birthdays", "events"] {
|
announce_event(guild_id, e.name.as_str(), e.special_message.as_str(), ctx.http.clone()).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// // let mut stmt = client.conn(|conn| {
|
let global_events = sqlx::query_as::<_, EventRow>(
|
||||||
// // conn.
|
"SELECT guild, name, day, month, specialMessage from events
|
||||||
// // }).await;
|
WHERE day=? AND month=? AND guild!=0;"
|
||||||
// }
|
)
|
||||||
|
.bind(day)
|
||||||
|
.bind(month)
|
||||||
|
.fetch_all(&mut db)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
for e in &global_events {
|
||||||
|
announce_event(GuildId::new(e.guild), e.name.as_str(), e.special_message.as_str(), ctx.http.clone()).await;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
0
src/other/tenor.rs
Normal file
0
src/other/tenor.rs
Normal file
Loading…
Reference in a new issue