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