Add logging to admin commands, more documentation, fix negative purge (thanks @GiggleButts)

This commit is contained in:
Zeva Rose 2021-06-30 20:25:18 -06:00
parent aad00865de
commit c1ecc83953

View file

@ -2,6 +2,7 @@ import jarvis
from discord import User from discord import User
from discord.ext import commands from discord.ext import commands
from discord_slash import cog_ext, SlashContext from discord_slash import cog_ext, SlashContext
from discord_slash.utils.manage_commands import create_option, create_choice
from jarvis.utils.db import DBManager from jarvis.utils.db import DBManager
from datetime import datetime from datetime import datetime
@ -18,43 +19,114 @@ class AdminCog(commands.Cog):
config = jarvis.config.get_config() config = jarvis.config.get_config()
self.db = DBManager(config.mongo).mongo self.db = DBManager(config.mongo).mongo
async def _ban(self, ctx: SlashContext, user: User = None, reason=None): async def _ban(
if not user or user == ctx.message.author: self,
ctx,
user: User = None,
type: str = "perm",
reason: str = None,
length: int = 1,
):
if not user or user == ctx.author:
await ctx.send("You cannot ban yourself.") await ctx.send("You cannot ban yourself.")
if not reason: if not reason:
reason = ( reason = (
"Mr. Stark is displeased with your presence. Please leave." "Mr. Stark is displeased with your presence. Please leave."
) )
guild_name = ctx.message.guild.name
await user.send( mtype = type
f"You have been banned from {guild_name}. Reason:\n{reason}" if mtype == "perm":
mtype = "perma"
guild_name = ctx.guild.name
user_message = (
f"You have been {mtype}banned from {guild_name}."
+ " Reason:\n{reason}"
) )
if mtype == "temp":
user_message += f"\nDuration: {length} days"
await user.send(user_message)
await ctx.guild.ban(user, reason=reason) await ctx.guild.ban(user, reason=reason)
if mtype == "soft":
await ctx.guild.unban(user, reason="Ban was softban")
await ctx.send( await ctx.send(
f"{user.name} has been banned from {guild_name}." f"{user.name} has been {mtype}banned from {guild_name}."
+ f"Reason:\n{reason}" + f"Reason:\n{reason}"
) )
if type != "temp":
length = None
active = True
if type == "soft":
active = False
self.db.jarvis.bans.insert_one(
{
"user": user.id,
"reason": reason,
"admin": ctx.author.id,
"time": datetime.now(),
"guild": ctx.guild.id,
"type": type,
"length": length,
"active": active,
}
)
@cog_ext.cog_slash( @cog_ext.cog_slash(
name="ban", description="Ban a user", guild_ids=[578757004059738142] name="ban",
description="Ban a user",
guild_ids=[578757004059738142],
options=[
create_option(
name="user",
description="User to ban",
option_type=6,
required=True,
),
create_option(
name="type",
description="Ban type",
option_type=3,
required=False,
choices=[
create_choice("Permanent", "perm"),
create_choice("Temporary", "temp"),
create_choice("Soft", "soft"),
],
),
create_option(
name="reason",
description="Ban reason",
required=False,
option_type=3,
),
create_option(
name="duration",
description="Ban duration in days if temporary",
required=False,
option_type=4,
),
],
) )
@commands.has_permissions(ban_members=True) @commands.has_permissions(ban_members=True)
async def _ban_slash(self, ctx, user: User = None, reason=None): async def _ban_slash(
await self._ban(ctx, user, reason) self,
ctx,
user: User = None,
type: str = "perm",
reason: str = None,
length: int = 1,
):
await self._ban(ctx, user, type, reason, length)
@commands.command(name="ban") async def _kick(self, ctx, user: User, reason=None):
@commands.has_permissions(ban_members=True) if not user or user == ctx.author:
async def _ban_pref(self, ctx, user: User = None, reason=None):
await self._ban(ctx, user, reason)
async def _kick(self, ctx, user: User = None, reason=None):
if not user or user == ctx.message.author:
await ctx.send("You cannot kick yourself.") await ctx.send("You cannot kick yourself.")
if not reason: if not reason:
reason = ( reason = (
"Mr. Stark is displeased with your presence. Please leave." "Mr. Stark is displeased with your presence. Please leave."
) )
guild_name = ctx.message.guild.name guild_name = ctx.guild.name
try: try:
await user.send( await user.send(
f"You have been kicked from {guild_name}. Reason:\n{reason}" f"You have been kicked from {guild_name}. Reason:\n{reason}"
@ -66,20 +138,40 @@ class AdminCog(commands.Cog):
f"{user.name} has been kicked from {guild_name}." f"{user.name} has been kicked from {guild_name}."
+ f"Reason:\n{reason}" + f"Reason:\n{reason}"
) )
self.db.jarvis.kicks.insert_one(
@commands.command(name="kick") {
@commands.has_permissions(kick_members=True) "user": user.id,
async def _kick_pref(self, ctx, user: User = None, reason=None): "reason": reason,
await self._kick(ctx, user, reason) "admin": ctx.authod.id,
"time": datetime.now(),
"guild": ctx.guild.id,
}
)
@cog_ext.cog_slash( @cog_ext.cog_slash(
name="kick", description="Kick a user", guild_ids=[578757004059738142] name="kick",
description="Kick a user",
guild_ids=[578757004059738142],
options=[
create_option(
name="user",
description="User to kick",
option_type=6,
required=True,
),
create_option(
name="reason",
description="Kick reason",
required=False,
option_type=3,
),
],
) )
@commands.has_permissions(kick_members=True) @commands.has_permissions(kick_members=True)
async def _kick_slash(self, ctx, user: User = None, reason=None): async def _kick_slash(self, ctx, user: User, reason=None):
await self._kick(ctx, user, reason) await self._kick(ctx, user, reason)
async def _purge(self, ctx, amount: int = 30): async def _purge(self, ctx, amount: int = 10):
channel = ctx.channel channel = ctx.channel
messages = [] messages = []
async for message in channel.history(limit=amount + 1): async for message in channel.history(limit=amount + 1):
@ -99,9 +191,20 @@ class AdminCog(commands.Cog):
name="purge", name="purge",
description="Purge messages from channel", description="Purge messages from channel",
guild_ids=[578757004059738142], guild_ids=[578757004059738142],
options=[
create_option(
name="amount",
description="Amount of messages to purge",
required=False,
option_type=4,
)
],
) )
@commands.has_permissions(manage_messages=True) @commands.has_permissions(manage_messages=True)
async def _purge_slash(self, ctx, amount: int = 30): async def _purge_slash(self, ctx, amount: int = 10):
if amount < 1:
await ctx.send("Amount must be >= 1")
return
await ctx.defer() await ctx.defer()
await self._purge(ctx, amount) await self._purge(ctx, amount)