Merge branch 'dev'
This commit is contained in:
@@ -55,7 +55,7 @@ async fn display_leaderboard(ctx: Context<'_>, t: LeaderboardType) -> Result<(),
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// DIsplay users with the top highest balances
|
/// Display users with the top highest balances
|
||||||
#[poise::command(slash_command, prefix_command)]
|
#[poise::command(slash_command, prefix_command)]
|
||||||
pub async fn tokens(ctx: Context<'_>, count: Option<usize>) -> Result<(), Error> {
|
pub async fn tokens(ctx: Context<'_>, count: Option<usize>) -> Result<(), Error> {
|
||||||
let count = count.unwrap_or(10);
|
let count = count.unwrap_or(10);
|
||||||
|
|||||||
@@ -82,7 +82,10 @@ pub async fn color(ctx: Context<'_>,
|
|||||||
|
|
||||||
let user = ctx.author();
|
let user = ctx.author();
|
||||||
|
|
||||||
let role = super::edit_role(ctx, user.id, guild, EditRole::new().colour(color), &ctx.data().database).await?;
|
let mut tx = ctx.data().database.begin().await?;
|
||||||
|
let role = super::edit_role(ctx, user.id, guild, EditRole::new().colour(color), &mut *tx).await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
common::no_ping_reply(&ctx, format!("{}'s color has been updated.", guild.role(ctx, role).await?)).await?;
|
common::no_ping_reply(&ctx, format!("{}'s color has been updated.", guild.role(ctx, role).await?)).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
use crate::common::{Context, Error};
|
use crate::common::{Context, Error};
|
||||||
use sqlx::{PgExecutor, Row};
|
use sqlx::{PgConnection, Row};
|
||||||
use poise::serenity_prelude::{EditRole, GuildId, Permissions, RoleId, UserId};
|
use poise::serenity_prelude::{EditRole, GuildId, Permissions, RoleId, UserId};
|
||||||
|
|
||||||
mod register;
|
mod register;
|
||||||
@@ -27,10 +27,9 @@ pub async fn role(_ctx: Context<'_>) -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Edit a user's personal role, creates it with some default values if it doesn't exist.
|
/// Edit a user's personal role, creates it with some default values if it doesn't exist.
|
||||||
pub async fn edit_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit: EditRole<'a>, db: E) -> Result<RoleId, Error>
|
pub async fn edit_role(ctx: Context<'_>, user: UserId, guild: GuildId, edit: EditRole<'_>, db: &mut PgConnection) -> Result<RoleId, Error>
|
||||||
where E: PgExecutor<'a> + Clone,
|
|
||||||
{
|
{
|
||||||
if let Some(role) = get_user_role(user, guild, db.clone()).await? {
|
if let Some(role) = get_user_role(user, guild, db).await? {
|
||||||
guild.role(ctx, role).await?.edit(ctx, edit).await?;
|
guild.role(ctx, role).await?.edit(ctx, edit).await?;
|
||||||
Ok(role)
|
Ok(role)
|
||||||
} else {
|
} else {
|
||||||
@@ -38,8 +37,12 @@ pub async fn edit_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, ed
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit: EditRole<'a>, db: E) -> Result<RoleId, Error>
|
async fn create_role(
|
||||||
where E: PgExecutor<'a> + Clone,
|
ctx: Context<'_>,
|
||||||
|
user: UserId,
|
||||||
|
guild: GuildId,
|
||||||
|
edit: EditRole<'_>,
|
||||||
|
db: &mut PgConnection) -> Result<RoleId, Error>
|
||||||
{
|
{
|
||||||
let def = EditRole::new()
|
let def = EditRole::new()
|
||||||
.name(user.to_user(ctx).await?.name)
|
.name(user.to_user(ctx).await?.name)
|
||||||
@@ -63,9 +66,7 @@ async fn create_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a row concerning a user's self role from the database
|
/// Remove a row concerning a user's self role from the database
|
||||||
pub async fn remove_user_role<'a, E>(user: UserId, guild: GuildId, db: E) -> Result<(), Error>
|
pub async fn remove_user_role(user: UserId, guild: GuildId, db: &mut PgConnection) -> Result<(), Error> {
|
||||||
where E: PgExecutor<'a>,
|
|
||||||
{
|
|
||||||
sqlx::query("DELETE FROM selfroles WHERE userid = $1 AND guildid = $2")
|
sqlx::query("DELETE FROM selfroles WHERE userid = $1 AND guildid = $2")
|
||||||
.bind(user.get() as i64)
|
.bind(user.get() as i64)
|
||||||
.bind(guild.get() as i64)
|
.bind(guild.get() as i64)
|
||||||
@@ -74,9 +75,7 @@ pub async fn remove_user_role<'a, E>(user: UserId, guild: GuildId, db: E) -> Res
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn remove_role<'a, E>(role: RoleId, guild: GuildId, db: E) -> Result<(), Error>
|
pub async fn remove_role(role: RoleId, guild: GuildId, db: &mut PgConnection) -> Result<(), Error> {
|
||||||
where E: PgExecutor<'a>
|
|
||||||
{
|
|
||||||
sqlx::query("DELETE FROM selfroles WHERE roleid = $1 AND guildid = $2")
|
sqlx::query("DELETE FROM selfroles WHERE roleid = $1 AND guildid = $2")
|
||||||
.bind(role.get() as i64)
|
.bind(role.get() as i64)
|
||||||
.bind(guild.get() as i64)
|
.bind(guild.get() as i64)
|
||||||
@@ -86,9 +85,7 @@ where E: PgExecutor<'a>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Replace a user's custom role with a new one
|
/// Replace a user's custom role with a new one
|
||||||
pub async fn update_user_role<'a, E>(user: UserId, guild: GuildId, role: RoleId, db: E) -> Result<(), Error>
|
pub async fn update_user_role(user: UserId, guild: GuildId, role: RoleId, db: &mut PgConnection) -> Result<(), Error> {
|
||||||
where E: PgExecutor<'a>,
|
|
||||||
{
|
|
||||||
sqlx::query("INSERT INTO selfroles (userid, guildid, roleid) VALUES($1, $2, $3) ON CONFLICT (userid, guildid) DO UPDATE SET roleid = EXCLUDED.roleid")
|
sqlx::query("INSERT INTO selfroles (userid, guildid, roleid) VALUES($1, $2, $3) ON CONFLICT (userid, guildid) DO UPDATE SET roleid = EXCLUDED.roleid")
|
||||||
.bind(user.get() as i64)
|
.bind(user.get() as i64)
|
||||||
.bind(guild.get() as i64)
|
.bind(guild.get() as i64)
|
||||||
@@ -99,9 +96,7 @@ pub async fn update_user_role<'a, E>(user: UserId, guild: GuildId, role: RoleId,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get a user's personal role id from the database
|
/// Get a user's personal role id from the database
|
||||||
pub async fn get_user_role<'a, E>(user: UserId, guild: GuildId, db: E) -> Result<Option<RoleId>, Error>
|
pub async fn get_user_role(user: UserId, guild: GuildId, db: &mut PgConnection) -> Result<Option<RoleId>, Error> {
|
||||||
where E: PgExecutor<'a>,
|
|
||||||
{
|
|
||||||
match sqlx::query("SELECT roleid FROM selfroles WHERE userid = $1 AND guildid = $2")
|
match sqlx::query("SELECT roleid FROM selfroles WHERE userid = $1 AND guildid = $2")
|
||||||
.bind(user.get() as i64)
|
.bind(user.get() as i64)
|
||||||
.bind(guild.get() as i64)
|
.bind(guild.get() as i64)
|
||||||
|
|||||||
@@ -15,8 +15,12 @@ pub async fn name(ctx: Context<'_>, #[rest] name: String) -> Result<(), Error> {
|
|||||||
|
|
||||||
let user = ctx.author();
|
let user = ctx.author();
|
||||||
|
|
||||||
let role = super::edit_role(ctx, user.id, guild, EditRole::new().name(name), &ctx.data().database).await?;
|
let mut tx = ctx.data().database.begin().await?;
|
||||||
|
let role = super::edit_role(ctx, user.id, guild, EditRole::new().name(name), &mut *tx).await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
common::no_ping_reply(&ctx, format!("{} has been updated.", guild.role(ctx, role).await?)).await?;
|
common::no_ping_reply(&ctx, format!("{} has been updated.", guild.role(ctx, role).await?)).await?;
|
||||||
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ async fn event_handler(
|
|||||||
match event {
|
match event {
|
||||||
serenity::FullEvent::Message { new_message: message } => {
|
serenity::FullEvent::Message { new_message: message } => {
|
||||||
if message.author.bot { return Ok(()) }
|
if message.author.bot { return Ok(()) }
|
||||||
|
println!("{} in #{}: {}", message.author, message.channel_id, message.content);
|
||||||
}
|
}
|
||||||
serenity::FullEvent::GuildMemberRemoval { guild_id, user, .. } => {
|
serenity::FullEvent::GuildMemberRemoval { guild_id, user, .. } => {
|
||||||
let mut tx = data.database.begin().await?;
|
let mut tx = data.database.begin().await?;
|
||||||
@@ -57,7 +58,7 @@ async fn event_handler(
|
|||||||
async fn get_prefix(ctx: PartialContext<'_, Data, Error>) -> Result<Option<String>, Error> {
|
async fn get_prefix(ctx: PartialContext<'_, Data, Error>) -> Result<Option<String>, Error> {
|
||||||
let guild = match ctx.guild_id {
|
let guild = match ctx.guild_id {
|
||||||
Some(guild) => guild,
|
Some(guild) => guild,
|
||||||
None => return Ok(None),
|
None => return Ok(ctx.data.prefix.clone()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let db = &ctx.data.database;
|
let db = &ctx.data.database;
|
||||||
@@ -91,7 +92,7 @@ async fn main() -> Result<(), Error> {
|
|||||||
|
|
||||||
let framework = poise::Framework::builder()
|
let framework = poise::Framework::builder()
|
||||||
.options(poise::FrameworkOptions {
|
.options(poise::FrameworkOptions {
|
||||||
commands: commands::commands(),
|
commands: commands::commands().await,
|
||||||
prefix_options: poise::PrefixFrameworkOptions {
|
prefix_options: poise::PrefixFrameworkOptions {
|
||||||
dynamic_prefix: Some(|ctx| Box::pin(get_prefix(ctx))),
|
dynamic_prefix: Some(|ctx| Box::pin(get_prefix(ctx))),
|
||||||
edit_tracker: Some(Arc::new(
|
edit_tracker: Some(Arc::new(
|
||||||
|
|||||||
Reference in New Issue
Block a user