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.ext import commands
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 datetime import datetime
@ -18,68 +19,159 @@ class AdminCog(commands.Cog):
config = jarvis.config.get_config()
self.db = DBManager(config.mongo).mongo
async def _ban(self, ctx: SlashContext, user: User = None, reason=None):
if not user or user == ctx.message.author:
async def _ban(
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.")
if not reason:
reason = (
"Mr. Stark is displeased with your presence. Please leave."
)
guild_name = ctx.message.guild.name
await user.send(
f"You have been banned from {guild_name}. Reason:\n{reason}"
)
await ctx.guild.ban(user, reason=reason)
await ctx.send(
f"{user.name} has been banned from {guild_name}."
+ f"Reason:\n{reason}"
)
mtype = type
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)
if mtype == "soft":
await ctx.guild.unban(user, reason="Ban was softban")
await ctx.send(
f"{user.name} has been {mtype}banned from {guild_name}."
+ 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(
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)
async def _ban_slash(self, ctx, user: User = None, reason=None):
await self._ban(ctx, user, reason)
async def _ban_slash(
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")
@commands.has_permissions(ban_members=True)
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:
async def _kick(self, ctx, user: User, reason=None):
if not user or user == ctx.author:
await ctx.send("You cannot kick yourself.")
if not reason:
reason = (
"Mr. Stark is displeased with your presence. Please leave."
)
guild_name = ctx.message.guild.name
try:
await user.send(
f"You have been kicked from {guild_name}. Reason:\n{reason}"
)
except Exception:
await ctx.send("Unable to message user.")
await ctx.guild.kick(user, reason=reason)
await ctx.send(
f"{user.name} has been kicked from {guild_name}."
+ f"Reason:\n{reason}"
guild_name = ctx.guild.name
try:
await user.send(
f"You have been kicked from {guild_name}. Reason:\n{reason}"
)
@commands.command(name="kick")
@commands.has_permissions(kick_members=True)
async def _kick_pref(self, ctx, user: User = None, reason=None):
await self._kick(ctx, user, reason)
except Exception:
await ctx.send("Unable to message user.")
await ctx.guild.kick(user, reason=reason)
await ctx.send(
f"{user.name} has been kicked from {guild_name}."
+ f"Reason:\n{reason}"
)
self.db.jarvis.kicks.insert_one(
{
"user": user.id,
"reason": reason,
"admin": ctx.authod.id,
"time": datetime.now(),
"guild": ctx.guild.id,
}
)
@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)
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)
async def _purge(self, ctx, amount: int = 30):
async def _purge(self, ctx, amount: int = 10):
channel = ctx.channel
messages = []
async for message in channel.history(limit=amount + 1):
@ -99,9 +191,20 @@ class AdminCog(commands.Cog):
name="purge",
description="Purge messages from channel",
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)
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 self._purge(ctx, amount)