Migrate admin/ban cog
This commit is contained in:
parent
e7227540d4
commit
7e4823a731
1 changed files with 79 additions and 133 deletions
|
@ -2,30 +2,32 @@
|
|||
import re
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from ButtonPaginator import Paginator
|
||||
from discord import User
|
||||
from discord.ext import commands
|
||||
from discord.utils import find
|
||||
from discord_slash import SlashContext, cog_ext
|
||||
from discord_slash.model import ButtonStyle
|
||||
from discord_slash.utils.manage_commands import create_choice, create_option
|
||||
from dis_snek import InteractionContext, Permissions, Snake
|
||||
from dis_snek.ext.paginators import Paginator
|
||||
from dis_snek.models.discord.embed import EmbedField
|
||||
from dis_snek.models.discord.user import User
|
||||
from dis_snek.models.snek.application_commands import (
|
||||
OptionTypes,
|
||||
SlashCommandChoice,
|
||||
slash_command,
|
||||
slash_option,
|
||||
)
|
||||
|
||||
from jarvis.db.models import Ban, Unban
|
||||
from jarvis.utils import build_embed
|
||||
from jarvis.utils import build_embed, find
|
||||
from jarvis.utils.cachecog import CacheCog
|
||||
from jarvis.utils.field import Field
|
||||
from jarvis.utils.permissions import admin_or_permissions
|
||||
|
||||
|
||||
class BanCog(CacheCog):
|
||||
"""J.A.R.V.I.S. BanCog."""
|
||||
|
||||
def __init__(self, bot: commands.Bot):
|
||||
def __init__(self, bot: Snake):
|
||||
super().__init__(bot)
|
||||
|
||||
async def discord_apply_ban(
|
||||
self,
|
||||
ctx: SlashContext,
|
||||
ctx: InteractionContext,
|
||||
reason: str,
|
||||
user: User,
|
||||
duration: int,
|
||||
|
@ -37,7 +39,7 @@ class BanCog(CacheCog):
|
|||
await ctx.guild.ban(user, reason=reason)
|
||||
_ = Ban(
|
||||
user=user.id,
|
||||
username=user.name,
|
||||
username=user.username,
|
||||
discrim=user.discriminator,
|
||||
reason=reason,
|
||||
admin=ctx.author.id,
|
||||
|
@ -54,20 +56,20 @@ class BanCog(CacheCog):
|
|||
)
|
||||
|
||||
embed.set_author(
|
||||
name=user.nick if user.nick else user.name,
|
||||
icon_url=user.avatar_url,
|
||||
name=user.display_name,
|
||||
icon_url=user.avatar,
|
||||
)
|
||||
embed.set_thumbnail(url=user.avatar_url)
|
||||
embed.set_footer(text=f"{user.name}#{user.discriminator} | {user.id}")
|
||||
embed.set_thumbnail(url=user.avatar)
|
||||
embed.set_footer(text=f"{user.username}#{user.discriminator} | {user.id}")
|
||||
|
||||
await ctx.send(embed=embed)
|
||||
|
||||
async def discord_apply_unban(self, ctx: SlashContext, user: User, reason: str) -> None:
|
||||
async def discord_apply_unban(self, ctx: InteractionContext, user: User, reason: str) -> None:
|
||||
"""Apply a Discord unban."""
|
||||
await ctx.guild.unban(user, reason=reason)
|
||||
_ = Unban(
|
||||
user=user.id,
|
||||
username=user.name,
|
||||
username=user.username,
|
||||
discrim=user.discriminator,
|
||||
guild=ctx.guild.id,
|
||||
admin=ctx.author.id,
|
||||
|
@ -77,55 +79,34 @@ class BanCog(CacheCog):
|
|||
embed = build_embed(
|
||||
title="User Unbanned",
|
||||
description=f"<@{user.id}> was unbanned",
|
||||
fields=[Field(name="Reason", value=reason)],
|
||||
fields=[EmbedField(name="Reason", value=reason)],
|
||||
)
|
||||
embed.set_author(
|
||||
name=user.name,
|
||||
icon_url=user.avatar_url,
|
||||
name=user.username,
|
||||
icon_url=user.avatar,
|
||||
)
|
||||
embed.set_thumbnail(url=user.avatar_url)
|
||||
embed.set_footer(text=f"{user.name}#{user.discriminator} | {user.id}")
|
||||
embed.set_thumbnail(url=user.avatar)
|
||||
embed.set_footer(text=f"{user.username}#{user.discriminator} | {user.id}")
|
||||
await ctx.send(embed=embed)
|
||||
|
||||
@cog_ext.cog_slash(
|
||||
name="ban",
|
||||
description="Ban a user",
|
||||
options=[
|
||||
create_option(
|
||||
name="user",
|
||||
description="User to ban",
|
||||
option_type=6,
|
||||
required=True,
|
||||
),
|
||||
create_option(
|
||||
name="reason",
|
||||
description="Ban reason",
|
||||
required=True,
|
||||
option_type=3,
|
||||
),
|
||||
create_option(
|
||||
@slash_command(name="ban", description="Ban a user")
|
||||
@slash_option(name="user", description="User to ban", option_type=OptionTypes.USER, required=True)
|
||||
@slash_option(name="reason", description="Ban reason", option_type=OptionTypes.STRING, required=True)
|
||||
@slash_option(
|
||||
name="btype",
|
||||
description="Ban type",
|
||||
option_type=3,
|
||||
required=False,
|
||||
option_type=OptionTypes.STRING,
|
||||
required=True,
|
||||
choices=[
|
||||
create_choice(value="perm", name="Permanent"),
|
||||
create_choice(value="temp", name="Temporary"),
|
||||
create_choice(value="soft", name="Soft"),
|
||||
],
|
||||
),
|
||||
create_option(
|
||||
name="duration",
|
||||
description="Ban duration in hours if temporary",
|
||||
required=False,
|
||||
option_type=4,
|
||||
),
|
||||
SlashCommandChoice(name="Permanent", value="perm"),
|
||||
SlashCommandChoice(name="Temporary", value="temp"),
|
||||
SlashCommandChoice(name="Soft", value="soft"),
|
||||
],
|
||||
)
|
||||
@admin_or_permissions(ban_members=True)
|
||||
@admin_or_permissions(Permissions.BAN_MEMBERS)
|
||||
async def _ban(
|
||||
self,
|
||||
ctx: SlashContext,
|
||||
ctx: InteractionContext,
|
||||
user: User = None,
|
||||
reason: str = None,
|
||||
btype: str = "perm",
|
||||
|
@ -160,10 +141,10 @@ class BanCog(CacheCog):
|
|||
if mtype == "temp":
|
||||
user_message += f"\nDuration: {duration} hours"
|
||||
|
||||
fields = [Field(name="Type", value=mtype)]
|
||||
fields = [EmbedField(name="Type", value=mtype)]
|
||||
|
||||
if mtype == "temp":
|
||||
fields.append(Field(name="Duration", value=f"{duration} hour(s)"))
|
||||
fields.append(EmbedField(name="Duration", value=f"{duration} hour(s)"))
|
||||
|
||||
user_embed = build_embed(
|
||||
title=f"You have been banned from {ctx.guild.name}",
|
||||
|
@ -173,7 +154,7 @@ class BanCog(CacheCog):
|
|||
|
||||
user_embed.set_author(
|
||||
name=ctx.author.name + "#" + ctx.author.discriminator,
|
||||
icon_url=ctx.author.avatar_url,
|
||||
icon_url=ctx.author.avatar,
|
||||
)
|
||||
user_embed.set_thumbnail(url=ctx.guild.icon_url)
|
||||
|
||||
|
@ -190,7 +171,7 @@ class BanCog(CacheCog):
|
|||
if mtype == "soft":
|
||||
await ctx.guild.unban(user, reason="Ban was softban")
|
||||
|
||||
fields.append(Field(name="DM Sent?", value=str(not send_failed)))
|
||||
fields.append(EmbedField(name="DM Sent?", value=str(not send_failed)))
|
||||
if btype != "temp":
|
||||
duration = None
|
||||
active = True
|
||||
|
@ -199,28 +180,13 @@ class BanCog(CacheCog):
|
|||
|
||||
await self.discord_apply_ban(ctx, reason, user, duration, active, fields, mtype)
|
||||
|
||||
@cog_ext.cog_slash(
|
||||
name="unban",
|
||||
description="Unban a user",
|
||||
options=[
|
||||
create_option(
|
||||
name="user",
|
||||
description="User to unban",
|
||||
option_type=3,
|
||||
required=True,
|
||||
),
|
||||
create_option(
|
||||
name="reason",
|
||||
description="Unban reason",
|
||||
required=True,
|
||||
option_type=3,
|
||||
),
|
||||
],
|
||||
)
|
||||
@admin_or_permissions(ban_members=True)
|
||||
@slash_command(name="unban", description="Unban a user")
|
||||
@slash_option(name="user", description="User to unban", option_type=OptionTypes.STRING, required=True)
|
||||
@slash_option(name="reason", description="Unban reason", option_type=OptionTypes.STRING, required=True)
|
||||
@admin_or_permissions(Permissions.BAN_MEMBERS)
|
||||
async def _unban(
|
||||
self,
|
||||
ctx: SlashContext,
|
||||
ctx: InteractionContext,
|
||||
user: str,
|
||||
reason: str,
|
||||
) -> None:
|
||||
|
@ -236,24 +202,24 @@ class BanCog(CacheCog):
|
|||
bans = await ctx.guild.bans()
|
||||
|
||||
# Try to get ban information out of Discord
|
||||
if re.match("^[0-9]{1,}$", user): # User ID
|
||||
if re.match(r"^[0-9]{1,}$", user): # User ID
|
||||
user = int(user)
|
||||
discord_ban_info = find(lambda x: x.user.id == user, bans)
|
||||
else: # User name
|
||||
if re.match("#[0-9]{4}$", user): # User name has discrim
|
||||
if re.match(r"#[0-9]{4}$", user): # User name has discrim
|
||||
user, discrim = user.split("#")
|
||||
if discrim:
|
||||
discord_ban_info = find(
|
||||
lambda x: x.user.name == user and x.user.discriminator == discrim,
|
||||
lambda x: x.user.username == user and x.user.discriminator == discrim,
|
||||
bans,
|
||||
)
|
||||
else:
|
||||
results = [x for x in filter(lambda x: x.user.name == user, bans)]
|
||||
results = [x for x in filter(lambda x: x.user.username == user, bans)]
|
||||
if results:
|
||||
if len(results) > 1:
|
||||
active_bans = []
|
||||
for ban in bans:
|
||||
active_bans.append("{0} ({1}): {2}".format(ban.user.name, ban.user.id, ban.reason))
|
||||
active_bans.append("{0} ({1}): {2}".format(ban.user.username, ban.user.id, ban.reason))
|
||||
ab_message = "\n".join(active_bans)
|
||||
message = f"More than one result. Please use one of the following IDs:\n```{ab_message}\n```"
|
||||
await ctx.send(message)
|
||||
|
@ -299,37 +265,28 @@ class BanCog(CacheCog):
|
|||
).save()
|
||||
await ctx.send("Unable to find user in Discord, " + "but removed entry from database.")
|
||||
|
||||
@cog_ext.cog_subcommand(
|
||||
base="bans",
|
||||
name="list",
|
||||
description="List bans",
|
||||
options=[
|
||||
create_option(
|
||||
name="type",
|
||||
@slash_command(name="bans", description="User bans", sub_cmd_name="list", sub_cmd_description="List bans")
|
||||
@slash_option(
|
||||
name="btype",
|
||||
description="Ban type",
|
||||
option_type=4,
|
||||
option_type=OptionTypes.INTEGER,
|
||||
required=False,
|
||||
choices=[
|
||||
create_choice(value=0, name="All"),
|
||||
create_choice(value=1, name="Permanent"),
|
||||
create_choice(value=2, name="Temporary"),
|
||||
create_choice(value=3, name="Soft"),
|
||||
],
|
||||
),
|
||||
create_option(
|
||||
name="active",
|
||||
description="Active bans",
|
||||
option_type=4,
|
||||
required=False,
|
||||
choices=[
|
||||
create_choice(value=1, name="Yes"),
|
||||
create_choice(value=0, name="No"),
|
||||
],
|
||||
),
|
||||
SlashCommandChoice(name="All", value=0),
|
||||
SlashCommandChoice(name="Permanent", value=1),
|
||||
SlashCommandChoice(name="Temporary", value=2),
|
||||
SlashCommandChoice(name="Soft", value=3),
|
||||
],
|
||||
)
|
||||
@admin_or_permissions(ban_members=True)
|
||||
async def _bans_list(self, ctx: SlashContext, type: int = 0, active: int = 1) -> None:
|
||||
@slash_option(
|
||||
name="active",
|
||||
description="Active bans",
|
||||
option_type=OptionTypes.INTEGER,
|
||||
required=False,
|
||||
choices=[SlashCommandChoice(name="Yes", value=1), SlashCommandChoice(name="No", value=0)],
|
||||
)
|
||||
@admin_or_permissions(Permissions.BAN_MEMBERS)
|
||||
async def _bans_list(self, ctx: InteractionContext, type: int = 0, active: int = 1) -> None:
|
||||
active = bool(active)
|
||||
exists = self.check_cache(ctx, type=type, active=active)
|
||||
if exists:
|
||||
|
@ -351,9 +308,9 @@ class BanCog(CacheCog):
|
|||
for ban in bans:
|
||||
if not ban.username:
|
||||
user = await self.bot.fetch_user(ban.user)
|
||||
ban.username = user.name if user else "[deleted user]"
|
||||
ban.username = user.username if user else "[deleted user]"
|
||||
fields.append(
|
||||
Field(
|
||||
EmbedField(
|
||||
name=f"Username: {ban.username}#{ban.discrim}",
|
||||
value=(
|
||||
f"Date: {ban.created_at.strftime('%d-%m-%Y')}\n"
|
||||
|
@ -370,8 +327,8 @@ class BanCog(CacheCog):
|
|||
for ban in bans:
|
||||
if ban.user.id not in db_bans:
|
||||
fields.append(
|
||||
Field(
|
||||
name=f"Username: {ban.user.name}#" + f"{ban.user.discriminator}",
|
||||
EmbedField(
|
||||
name=f"Username: {ban.user.username}#" + f"{ban.user.discriminator}",
|
||||
value=(
|
||||
f"Date: [unknown]\n"
|
||||
f"User ID: {ban.user.id}\n"
|
||||
|
@ -403,18 +360,7 @@ class BanCog(CacheCog):
|
|||
embed.set_thumbnail(url=ctx.guild.icon_url)
|
||||
pages.append(embed)
|
||||
|
||||
paginator = Paginator(
|
||||
bot=self.bot,
|
||||
ctx=ctx,
|
||||
embeds=pages,
|
||||
only=ctx.author,
|
||||
timeout=60 * 5, # 5 minute timeout
|
||||
disable_after_timeout=True,
|
||||
use_extend=len(pages) > 2,
|
||||
left_button_style=ButtonStyle.grey,
|
||||
right_button_style=ButtonStyle.grey,
|
||||
basic_buttons=["◀", "▶"],
|
||||
)
|
||||
paginator = Paginator.create_from_embeds(self.bot, *pages, timeout=300)
|
||||
|
||||
self.cache[hash(paginator)] = {
|
||||
"guild": ctx.guild.id,
|
||||
|
@ -426,4 +372,4 @@ class BanCog(CacheCog):
|
|||
"paginator": paginator,
|
||||
}
|
||||
|
||||
await paginator.start()
|
||||
await paginator.send(ctx)
|
||||
|
|
Loading…
Add table
Reference in a new issue