clove/proxyBot
src/commands/log/mod.rs
// src/commands/log/mod.rs
use serenity::all::{
Channel, ChannelId, ChannelType, Context, GuildId, Mentionable, Message, Permissions,
};
use crate::Bot;
use crate::settings::ProxyLogging;
impl Bot {
pub(super) async fn log_config(
&self,
ctx: &Context,
message: &Message,
guild_id: GuildId,
args: &[String],
) -> anyhow::Result<String> {
let guild = guild_id.to_partial_guild(&ctx.http).await?;
if message.author.id != self.owner {
let member = guild_id.member(ctx, message.author.id).await?;
if !guild
.member_permissions(&member)
.contains(Permissions::MANAGE_GUILD)
{
return Ok("Only the proxyBot owner or someone with Manage Server can configure proxy logging.".into());
}
}
let update = match args
.iter()
.map(String::as_str)
.collect::<Vec<_>>()
.as_slice()
{
[] | ["channel"] | ["cleanup"] => {
let current = self
.settings
.lock()
.await
.proxy_logs
.get(&guild_id)
.copied()
.unwrap_or_default();
let channel = current
.channel
.map(|channel| channel.mention().to_string())
.unwrap_or_else(|| "off".into());
let cleanup = if current.cleanup { "on" } else { "off" };
return Ok(format!("Proxy logging: {channel}\nLog cleanup: {cleanup}"));
}
["channel", "-clear" | "off"] => LogUpdate::Channel(None),
["channel", channel] => {
let id = channel
.strip_prefix("<#")
.and_then(|mention| mention.strip_suffix('>'))
.unwrap_or(channel);
let Ok(channel_id) = id.parse::<ChannelId>() else {
return Ok("Give me a text channel mention or ID.".into());
};
let Some(Channel::Guild(channel)) = channel_id.to_channel(ctx).await.ok() else {
return Ok("I couldn't open that channel.".into());
};
if channel.guild_id != guild_id
|| !matches!(channel.kind, ChannelType::Text | ChannelType::News)
{
return Ok("Choose a text or announcement channel in this server.".into());
}
let bot_id = ctx.cache.current_user().id;
let bot = guild_id.member(ctx, bot_id).await?;
let required = Permissions::VIEW_CHANNEL | Permissions::SEND_MESSAGES;
if !guild.user_permissions_in(&channel, &bot).contains(required) {
return Ok("I need View Channel and Send Messages in that channel.".into());
}
LogUpdate::Channel(Some(channel_id))
}
["cleanup", "on"] => LogUpdate::Cleanup(true),
["cleanup", "off"] => LogUpdate::Cleanup(false),
_ => return Ok(
"Use `d$log channel #channel`, `d$log channel -clear`, or `d$log cleanup on|off`."
.into(),
),
};
self.update_settings(|settings| {
settings.guilds.entry(guild_id).or_default();
let config = settings.proxy_logs.entry(guild_id).or_default();
match update {
LogUpdate::Channel(channel) => config.channel = channel,
LogUpdate::Cleanup(enabled) => config.cleanup = enabled,
}
if *config == ProxyLogging::default() {
settings.proxy_logs.remove(&guild_id);
}
})
.await?;
Ok(match update {
LogUpdate::Channel(Some(channel)) => {
format!("Proxy logging is now set to {}.", channel.mention())
}
LogUpdate::Channel(None) => "Proxy logging is now off.".into(),
LogUpdate::Cleanup(true) => "Log cleanup is now on.".into(),
LogUpdate::Cleanup(false) => "Log cleanup is now off.".into(),
})
}
}
enum LogUpdate {
Channel(Option<ChannelId>),
Cleanup(bool),
}