Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • stephen/cat-disruptor-6500
  • roygbyte/cat-disruptor-6500
  • tinyconan/cat-disruptor-6500
3 results
Show changes
...@@ -3,36 +3,32 @@ extern crate diesel; ...@@ -3,36 +3,32 @@ extern crate diesel;
mod config; mod config;
mod framebuffer; mod framebuffer;
mod handler; mod handlers;
mod joker;
mod models; mod models;
mod program; mod program;
mod schema; mod schema;
use crate::handler::Handler; use crate::handlers::Dispatcher;
use diesel::{Connection, PgConnection};
use dotenv::dotenv;
use serenity::Client;
use std::env;
fn establish_connection() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); use serenity::prelude::GatewayIntents;
PgConnection::establish(&database_url).expect("Error connecting to database") use serenity::Client;
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let config = config::get_conf(); let config = config::get_conf();
let token = config.token; let token = config.token;
let conn = establish_connection(); let mut client = Client::builder(
&token,
let mut client = Client::builder(&token) GatewayIntents::GUILD_MESSAGES
.event_handler(Handler::new(conn)) .union(GatewayIntents::GUILD_MESSAGE_REACTIONS)
.await .union(GatewayIntents::MESSAGE_CONTENT),
.expect("Error creating client"); )
.event_handler(Dispatcher::new(config.llama))
.await
.expect("Error creating client");
if let Err(e) = client.start().await { if let Err(e) = client.start().await {
println!("Client error: {}", e); eprintln!("Client error: {e}");
} }
} }
use crate::schema::user_programs; use crate::schema::user_programs;
use bigdecimal::BigDecimal; use bigdecimal::BigDecimal;
#[derive(Queryable)]
pub struct UserProgram {
pub id: i32,
pub discord_user_id: u64,
pub name: String,
pub code: String,
}
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "user_programs"] #[diesel(table_name = user_programs)]
pub struct NewUserProgram<'a> { pub struct NewUserProgram<'a> {
pub discord_user_id: BigDecimal, pub discord_user_id: BigDecimal,
pub name: &'a str, pub name: &'a str,
pub code: &'a str, pub code: &'a str,
} }
#[derive(Clone, Queryable)]
pub struct ServerSetting {
#[allow(dead_code)]
pub id: i32,
#[allow(dead_code)]
pub guild_id: BigDecimal,
pub starboard_threshold: i32,
pub starboard_emoji_id: BigDecimal,
pub starboard_channel: BigDecimal,
}
...@@ -18,7 +18,7 @@ impl Program { ...@@ -18,7 +18,7 @@ impl Program {
} }
} }
pub fn list_programs_by_user(conn: &PgConnection, user_id: UserId) -> Option<Vec<String>> { pub fn list_programs_by_user(conn: &mut PgConnection, user_id: UserId) -> Option<Vec<String>> {
user_programs user_programs
.filter(columns::discord_user_id.eq(BigDecimal::from_u64(*user_id.as_u64()).unwrap())) .filter(columns::discord_user_id.eq(BigDecimal::from_u64(*user_id.as_u64()).unwrap()))
.select(columns::name) .select(columns::name)
...@@ -26,7 +26,7 @@ impl Program { ...@@ -26,7 +26,7 @@ impl Program {
.ok() .ok()
} }
pub fn list_published_programs(conn: &PgConnection) -> Option<Vec<(i32, String)>> { pub fn list_published_programs(conn: &mut PgConnection) -> Option<Vec<(i32, String)>> {
user_programs user_programs
.filter(columns::published.eq(1)) .filter(columns::published.eq(1))
.select((columns::id, columns::name)) .select((columns::id, columns::name))
...@@ -34,7 +34,7 @@ impl Program { ...@@ -34,7 +34,7 @@ impl Program {
.ok() .ok()
} }
pub fn load_published_program(&mut self, conn: &PgConnection, id: i32) -> Option<String> { pub fn load_published_program(&mut self, conn: &mut PgConnection, id: i32) -> Option<String> {
let program: Vec<(String, String)> = user_programs let program: Vec<(String, String)> = user_programs
.filter(columns::id.eq(id).and(columns::published.eq(1))) .filter(columns::id.eq(id).and(columns::published.eq(1)))
.limit(1) .limit(1)
...@@ -55,7 +55,7 @@ impl Program { ...@@ -55,7 +55,7 @@ impl Program {
} }
pub fn set_program_published( pub fn set_program_published(
conn: &PgConnection, conn: &mut PgConnection,
name: &str, name: &str,
user_id: UserId, user_id: UserId,
published: bool, published: bool,
...@@ -67,7 +67,7 @@ impl Program { ...@@ -67,7 +67,7 @@ impl Program {
.and(columns::name.eq(name)), .and(columns::name.eq(name)),
), ),
) )
.set(columns::published.eq(if published { 1 } else { 0 })) .set(columns::published.eq(i32::from(published)))
.execute(conn) .execute(conn)
.ok()? == 1 .ok()? == 1
{ {
...@@ -99,7 +99,7 @@ impl Program { ...@@ -99,7 +99,7 @@ impl Program {
.join("\n") .join("\n")
} }
pub fn save_program(&self, conn: &PgConnection, user_id: UserId, name: &str) -> Option<()> { pub fn save_program(&self, conn: &mut PgConnection, user_id: UserId, name: &str) -> Option<()> {
let code = self.stringy_line_nums(); let code = self.stringy_line_nums();
let new_program = NewUserProgram { let new_program = NewUserProgram {
discord_user_id: BigDecimal::from_u64(*user_id.as_u64()).unwrap(), discord_user_id: BigDecimal::from_u64(*user_id.as_u64()).unwrap(),
...@@ -118,7 +118,12 @@ impl Program { ...@@ -118,7 +118,12 @@ impl Program {
Some(()) Some(())
} }
pub fn load_program(&mut self, conn: &PgConnection, user_id: UserId, name: &str) -> Option<()> { pub fn load_program(
&mut self,
conn: &mut PgConnection,
user_id: UserId,
name: &str,
) -> Option<()> {
let code: Vec<String> = user_programs let code: Vec<String> = user_programs
.filter( .filter(
columns::discord_user_id columns::discord_user_id
......
table! {
server_settings (id) {
id -> Int4,
guild_id -> Numeric,
starboard_threshold -> Int4,
starboard_emoji_id -> Numeric,
starboard_channel -> Numeric,
}
}
table! {
starboard_mappings (original_id) {
original_id -> Numeric,
repost_id -> Nullable<Numeric>,
}
}
table! { table! {
user_programs (id) { user_programs (id) {
id -> Int4, id -> Int4,
...@@ -7,3 +24,5 @@ table! { ...@@ -7,3 +24,5 @@ table! {
published -> Int4, published -> Int4,
} }
} }
allow_tables_to_appear_in_same_query!(server_settings, starboard_mappings, user_programs,);