more commands. fixed an issue with text only commands

This commit is contained in:
2021-09-11 00:14:23 +01:00
parent 471948bed3
commit 9b5333dc87
18 changed files with 562 additions and 897 deletions

View File

@ -6,15 +6,18 @@ pub mod user_data;
use std::sync::Arc;
use guild_data::GuildData;
use serenity::{
async_trait,
model::id::{GuildId, UserId},
model::id::{ChannelId, GuildId, UserId},
prelude::Context,
};
use tokio::sync::RwLock;
use crate::{consts::DEFAULT_PREFIX, models::user_data::UserData, GuildDataCache, SQLPool};
use crate::{
consts::DEFAULT_PREFIX,
models::{channel_data::ChannelData, guild_data::GuildData, user_data::UserData},
GuildDataCache, SQLPool,
};
#[async_trait]
pub trait CtxData {
@ -23,12 +26,17 @@ pub trait CtxData {
guild_id: G,
) -> Result<Arc<RwLock<GuildData>>, sqlx::Error>;
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String;
async fn user_data<U: Into<UserId> + Send + Sync>(
&self,
user_id: U,
) -> Result<UserData, Box<dyn std::error::Error + Sync + Send>>;
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String;
async fn channel_data<C: Into<ChannelId> + Send + Sync>(
&self,
channel_id: C,
) -> Result<ChannelData, Box<dyn std::error::Error + Sync + Send>>;
}
#[async_trait]
@ -41,13 +49,7 @@ impl CtxData for Context {
let guild = guild_id.to_guild_cached(&self.cache).unwrap();
let guild_cache = self
.data
.read()
.await
.get::<GuildDataCache>()
.cloned()
.unwrap();
let guild_cache = self.data.read().await.get::<GuildDataCache>().cloned().unwrap();
let x = if let Some(guild_data) = guild_cache.get(&guild_id) {
Ok(guild_data.clone())
@ -70,6 +72,14 @@ impl CtxData for Context {
x
}
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String {
if let Some(guild_id) = guild_id {
self.guild_data(guild_id).await.unwrap().read().await.prefix.clone()
} else {
DEFAULT_PREFIX.clone()
}
}
async fn user_data<U: Into<UserId> + Send + Sync>(
&self,
user_id: U,
@ -82,17 +92,15 @@ impl CtxData for Context {
UserData::from_user(&user, &self, &pool).await
}
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String {
if let Some(guild_id) = guild_id {
self.guild_data(guild_id)
.await
.unwrap()
.read()
.await
.prefix
.clone()
} else {
DEFAULT_PREFIX.clone()
}
async fn channel_data<C: Into<ChannelId> + Send + Sync>(
&self,
channel_id: C,
) -> Result<ChannelData, Box<dyn std::error::Error + Sync + Send>> {
let channel_id = channel_id.into();
let pool = self.data.read().await.get::<SQLPool>().cloned().unwrap();
let channel = channel_id.to_channel_cached(&self).unwrap();
ChannelData::from_channel(&channel, &pool).await
}
}