refactor: use User over Member in moderation commands
This commit is contained in:
parent
9dce57f527
commit
e0fea8d23e
2 changed files with 39 additions and 34 deletions
|
@ -3,7 +3,7 @@ use crate::{consts::COLORS, Context};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use color_eyre::eyre::{eyre, Context as _, Result};
|
use color_eyre::eyre::{eyre, Context as _, Result};
|
||||||
use log::*;
|
use log::*;
|
||||||
use poise::serenity_prelude::{CacheHttp, Http, Member, Timestamp};
|
use poise::serenity_prelude::{CacheHttp, GuildId, Http, Timestamp, User};
|
||||||
|
|
||||||
type Fields<'a> = Vec<(&'a str, String, bool)>;
|
type Fields<'a> = Vec<(&'a str, String, bool)>;
|
||||||
|
|
||||||
|
@ -14,7 +14,8 @@ pub trait ModActionInfo {
|
||||||
async fn run_action(
|
async fn run_action(
|
||||||
&self,
|
&self,
|
||||||
http: impl CacheHttp + AsRef<Http>,
|
http: impl CacheHttp + AsRef<Http>,
|
||||||
user: &Member,
|
user: &User,
|
||||||
|
guild_id: &GuildId,
|
||||||
reason: String,
|
reason: String,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
@ -75,13 +76,8 @@ impl<T: ModActionInfo> ModAction<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// public facing message
|
/// public facing message
|
||||||
pub async fn reply(
|
pub async fn reply(&self, ctx: &Context<'_>, user: &User, dm_user: Option<bool>) -> Result<()> {
|
||||||
&self,
|
let mut resp = format!("{} {}!", self.data.description(), user.name);
|
||||||
ctx: &Context<'_>,
|
|
||||||
user: &Member,
|
|
||||||
dm_user: Option<bool>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let mut resp = format!("{} {}!", self.data.description(), user.user.name);
|
|
||||||
|
|
||||||
if dm_user.unwrap_or_default() {
|
if dm_user.unwrap_or_default() {
|
||||||
resp = format!("{resp} (user notified with direct message)");
|
resp = format!("{resp} (user notified with direct message)");
|
||||||
|
@ -92,12 +88,11 @@ impl<T: ModActionInfo> ModAction<T> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn dm_user(&self, ctx: &Context<'_>, user: &Member) -> Result<()> {
|
pub async fn dm_user(&self, ctx: &Context<'_>, user: &User, guild_id: &GuildId) -> Result<()> {
|
||||||
let guild = ctx.http().get_guild(*user.guild_id.as_u64()).await?;
|
let guild = ctx.http().get_guild(*guild_id.as_u64()).await?;
|
||||||
let title = format!("{} from {}!", self.data.description(), guild.name);
|
let title = format!("{} from {}!", self.data.description(), guild.name);
|
||||||
|
|
||||||
user.user
|
user.dm(ctx, |m| {
|
||||||
.dm(ctx, |m| {
|
|
||||||
m.embed(|e| {
|
m.embed(|e| {
|
||||||
e.title(title).color(COLORS["red"]);
|
e.title(title).color(COLORS["red"]);
|
||||||
|
|
||||||
|
@ -116,7 +111,7 @@ impl<T: ModActionInfo> ModAction<T> {
|
||||||
pub async fn handle(
|
pub async fn handle(
|
||||||
&self,
|
&self,
|
||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
user: &Member,
|
user: &User,
|
||||||
quiet: Option<bool>,
|
quiet: Option<bool>,
|
||||||
dm_user: Option<bool>,
|
dm_user: Option<bool>,
|
||||||
handle_reply: bool,
|
handle_reply: bool,
|
||||||
|
@ -127,13 +122,19 @@ impl<T: ModActionInfo> ModAction<T> {
|
||||||
ctx.defer().await?;
|
ctx.defer().await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let guild_id = ctx
|
||||||
|
.guild_id()
|
||||||
|
.ok_or_else(|| eyre!("Couldn't get GuildId for context!"))?;
|
||||||
let actual_reason = self.reason.clone().unwrap_or("".to_string());
|
let actual_reason = self.reason.clone().unwrap_or("".to_string());
|
||||||
|
|
||||||
if dm_user.unwrap_or_default() {
|
if dm_user.unwrap_or_default() {
|
||||||
self.dm_user(ctx, user).await?;
|
self.dm_user(ctx, user, &guild_id).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.data.run_action(ctx, user, actual_reason).await?;
|
self.data
|
||||||
|
.run_action(ctx, user, &guild_id, actual_reason)
|
||||||
|
.await?;
|
||||||
|
|
||||||
self.log_action(ctx).await?;
|
self.log_action(ctx).await?;
|
||||||
|
|
||||||
if handle_reply {
|
if handle_reply {
|
||||||
|
@ -167,12 +168,14 @@ impl ModActionInfo for Ban {
|
||||||
async fn run_action(
|
async fn run_action(
|
||||||
&self,
|
&self,
|
||||||
http: impl CacheHttp + AsRef<Http>,
|
http: impl CacheHttp + AsRef<Http>,
|
||||||
user: &Member,
|
user: &User,
|
||||||
|
guild_id: &GuildId,
|
||||||
reason: String,
|
reason: String,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
debug!("Banning user {user} with reason: \"{reason}\"");
|
debug!("Banning user {user} with reason: \"{reason}\"");
|
||||||
|
|
||||||
user.ban_with_reason(http, self.purge_messages_days, reason)
|
guild_id
|
||||||
|
.ban_with_reason(http, user, self.purge_messages_days, reason)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -199,7 +202,8 @@ impl ModActionInfo for Timeout {
|
||||||
async fn run_action(
|
async fn run_action(
|
||||||
&self,
|
&self,
|
||||||
http: impl CacheHttp + AsRef<Http>,
|
http: impl CacheHttp + AsRef<Http>,
|
||||||
user: &Member,
|
user: &User,
|
||||||
|
guild_id: &GuildId,
|
||||||
reason: String,
|
reason: String,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
todo!()
|
todo!()
|
||||||
|
@ -221,12 +225,13 @@ impl ModActionInfo for Kick {
|
||||||
async fn run_action(
|
async fn run_action(
|
||||||
&self,
|
&self,
|
||||||
http: impl CacheHttp + AsRef<Http>,
|
http: impl CacheHttp + AsRef<Http>,
|
||||||
user: &Member,
|
user: &User,
|
||||||
|
guild_id: &GuildId,
|
||||||
reason: String,
|
reason: String,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
debug!("Kicked user {user} with reason: \"{reason}\"");
|
debug!("Kicked user {user} with reason: \"{reason}\"");
|
||||||
|
|
||||||
user.kick_with_reason(http, &reason).await?;
|
guild_id.kick_with_reason(http, user, &reason).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::Context;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
use color_eyre::eyre::{eyre, Result};
|
use color_eyre::eyre::{eyre, Result};
|
||||||
use poise::serenity_prelude::{ArgumentConvert, ChannelId, GuildId, Member};
|
use poise::serenity_prelude::{ArgumentConvert, ChannelId, GuildId, User};
|
||||||
|
|
||||||
pub mod actions;
|
pub mod actions;
|
||||||
use actions::{Ban, Kick, ModAction};
|
use actions::{Ban, Kick, ModAction};
|
||||||
|
@ -40,7 +40,7 @@ where
|
||||||
)]
|
)]
|
||||||
pub async fn ban_user(
|
pub async fn ban_user(
|
||||||
ctx: Context<'_>,
|
ctx: Context<'_>,
|
||||||
#[description = "User to ban"] user: Member,
|
#[description = "User to ban"] user: User,
|
||||||
#[description = "Reason to ban"] reason: Option<String>,
|
#[description = "Reason to ban"] reason: Option<String>,
|
||||||
#[description = "Number of days to purge their messages from (defaults to 0)"]
|
#[description = "Number of days to purge their messages from (defaults to 0)"]
|
||||||
purge_messages_days: Option<u8>,
|
purge_messages_days: Option<u8>,
|
||||||
|
@ -84,7 +84,7 @@ pub async fn mass_ban(
|
||||||
.ok_or_else(|| eyre!("Couldn't get GuildId!"))?;
|
.ok_or_else(|| eyre!("Couldn't get GuildId!"))?;
|
||||||
|
|
||||||
let dmd = purge_messages_days.unwrap_or(0);
|
let dmd = purge_messages_days.unwrap_or(0);
|
||||||
let users: Vec<Member> = split_argument(&ctx, Some(gid), None, users).await?;
|
let users: Vec<User> = split_argument(&ctx, Some(gid), None, users).await?;
|
||||||
|
|
||||||
for user in &users {
|
for user in &users {
|
||||||
let action = ModAction {
|
let action = ModAction {
|
||||||
|
@ -114,7 +114,7 @@ pub async fn mass_ban(
|
||||||
)]
|
)]
|
||||||
pub async fn kick_user(
|
pub async fn kick_user(
|
||||||
ctx: Context<'_>,
|
ctx: Context<'_>,
|
||||||
#[description = "User to kick"] user: Member,
|
#[description = "User to kick"] user: User,
|
||||||
#[description = "Reason to kick"] reason: Option<String>,
|
#[description = "Reason to kick"] reason: Option<String>,
|
||||||
#[description = "If true, the reply from the bot will be ephemeral"] quiet: Option<bool>,
|
#[description = "If true, the reply from the bot will be ephemeral"] quiet: Option<bool>,
|
||||||
#[description = "If true, the affected user will be sent a DM"] dm_user: Option<bool>,
|
#[description = "If true, the affected user will be sent a DM"] dm_user: Option<bool>,
|
||||||
|
@ -148,7 +148,7 @@ pub async fn mass_kick(
|
||||||
let gid = ctx
|
let gid = ctx
|
||||||
.guild_id()
|
.guild_id()
|
||||||
.ok_or_else(|| eyre!("Couldn't get GuildId!"))?;
|
.ok_or_else(|| eyre!("Couldn't get GuildId!"))?;
|
||||||
let users: Vec<Member> = split_argument(&ctx, Some(gid), None, users).await?;
|
let users: Vec<User> = split_argument(&ctx, Some(gid), None, users).await?;
|
||||||
|
|
||||||
for user in &users {
|
for user in &users {
|
||||||
let action = ModAction {
|
let action = ModAction {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue