Skip to content

Commit

Permalink
refactor(database): implement prisma in Rust (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmyna committed Feb 6, 2025
1 parent d35ce7c commit 36d2971
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@ config/default/permissions.json
# Added by cargo

/target
/src/prisma.rs
3 changes: 3 additions & 0 deletions src/bin/prisma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
prisma_client_rust_cli::run();
}
4 changes: 3 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::BotData;

pub mod moderation;
pub mod conf;
pub mod util;

pub fn get_commands() -> Vec<poise::Command<(), crate::handlers::error::BotErr>> {
pub fn get_commands() -> Vec<poise::Command<BotData, crate::handlers::error::BotErr>> {
vec![
util::ping::ping(),
moderation::kick::kick(),
Expand Down
5 changes: 3 additions & 2 deletions src/commands/util/ping.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use poise::Context;

use crate::handlers::error::BotErr;
use crate::{handlers::error::BotErr, BotData};

/// Check the heartbeat of the gateway
#[poise::command(slash_command)]
pub async fn ping(ctx: Context<'_, (), BotErr>) -> Result<(), BotErr> {
pub async fn ping(ctx: Context<'_, BotData, BotErr>) -> Result<(), BotErr> {
let ping = ctx.ping().await;

ctx.say(format!("The current gateway heartbeat is {}ms!", ping.as_millis())).await?;
Expand Down
28 changes: 21 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ use handlers::error::BotErr;
use poise::serenity_prelude as serenity;

use serenity::all::{ApplicationId, ClientBuilder, GatewayIntents, GuildId};
use std::env;
use std::{
env,
sync::Arc,
};
use tokio::sync::Mutex;

mod commands;
mod handlers;
#[allow(warnings, unused)]
mod prisma;
mod utils;

pub struct Data;
pub struct BotData {
pub prisma: Arc<Mutex<prisma::PrismaClient>>,
}

#[tokio::main]
async fn main() {
Expand All @@ -33,7 +41,11 @@ async fn main() {
env::remove_var("TOKEN");
env::remove_var("CLIENT_ID");

let framework: poise::Framework<(), BotErr> = poise::Framework::builder()
let prisma_client = Arc::new(Mutex::new(
prisma::PrismaClient::_builder().build().await.unwrap(),
));

let framework = poise::Framework::<BotData, BotErr>::builder()
.options(poise::FrameworkOptions {
commands: commands::get_commands(),
prefix_options: poise::PrefixFrameworkOptions {
Expand All @@ -53,7 +65,7 @@ async fn main() {
})
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
//poise::builtins::register_globally(ctx, &framework.options().commands).await?;

// dev
poise::builtins::register_in_guild(
Expand All @@ -63,17 +75,19 @@ async fn main() {
)
.await?;

Ok(())
Ok(BotData {
prisma: prisma_client,
})
})
})
.build();

let mut client = ClientBuilder::new(&token, intents)
let mut discord_client = ClientBuilder::new(&token, intents)
.framework(framework)
.application_id(app_id)
.status(serenity::OnlineStatus::Online)
.await
.expect("Error creating client");

client.start().await.unwrap()
discord_client.start().await.unwrap()
}

0 comments on commit 36d2971

Please sign in to comment.