Migrate admin/warning, ref #91
This commit is contained in:
parent
07a3eac703
commit
6a39b1e8d9
1 changed files with 51 additions and 73 deletions
|
@ -1,12 +1,15 @@
|
||||||
"""J.A.R.V.I.S. WarningCog."""
|
"""J.A.R.V.I.S. WarningCog."""
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from ButtonPaginator import Paginator
|
from dis_snek import InteractionContext, Permissions, Snek
|
||||||
from discord import User
|
from dis_snek.ext.paginators import Paginator
|
||||||
from discord.ext.commands import Bot
|
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 Warning
|
from jarvis.db.models import Warning
|
||||||
from jarvis.utils import build_embed
|
from jarvis.utils import build_embed
|
||||||
|
@ -18,35 +21,29 @@ from jarvis.utils.permissions import admin_or_permissions
|
||||||
class WarningCog(CacheCog):
|
class WarningCog(CacheCog):
|
||||||
"""J.A.R.V.I.S. WarningCog."""
|
"""J.A.R.V.I.S. WarningCog."""
|
||||||
|
|
||||||
def __init__(self, bot: Bot):
|
def __init__(self, bot: Snek):
|
||||||
super().__init__(bot)
|
super().__init__(bot)
|
||||||
|
|
||||||
@cog_ext.cog_slash(
|
@slash_command(name="warn", description="Warn a user")
|
||||||
name="warn",
|
@slash_option(
|
||||||
description="Warn a user",
|
name="user", description="User to warn", option_type=OptionTypes.USER, required=True
|
||||||
options=[
|
)
|
||||||
create_option(
|
@slash_option(
|
||||||
name="user",
|
|
||||||
description="User to warn",
|
|
||||||
option_type=6,
|
|
||||||
required=True,
|
|
||||||
),
|
|
||||||
create_option(
|
|
||||||
name="reason",
|
name="reason",
|
||||||
description="Reason for warning",
|
description="Reason for warning",
|
||||||
option_type=3,
|
option_type=OptionTypes.STRING,
|
||||||
required=True,
|
required=True,
|
||||||
),
|
)
|
||||||
create_option(
|
@slash_option(
|
||||||
name="duration",
|
name="duration",
|
||||||
description="Duration of warning in hours, default 24",
|
description="Duration of warning in hours, default 24",
|
||||||
option_type=4,
|
option_type=OptionTypes.INTEGER,
|
||||||
required=False,
|
required=False,
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
@admin_or_permissions(manage_guild=True)
|
@admin_or_permissions(Permissions.MANAGE_GUILD)
|
||||||
async def _warn(self, ctx: SlashContext, user: User, reason: str, duration: int = 24) -> None:
|
async def _warn(
|
||||||
|
self, ctx: InteractionContext, user: User, reason: str, duration: int = 24
|
||||||
|
) -> None:
|
||||||
if len(reason) > 100:
|
if len(reason) > 100:
|
||||||
await ctx.send("Reason must be < 100 characters", hidden=True)
|
await ctx.send("Reason must be < 100 characters", hidden=True)
|
||||||
return
|
return
|
||||||
|
@ -72,37 +69,29 @@ class WarningCog(CacheCog):
|
||||||
fields=fields,
|
fields=fields,
|
||||||
)
|
)
|
||||||
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_url,
|
||||||
)
|
)
|
||||||
embed.set_footer(text=f"{user.name}#{user.discriminator} | {user.id}")
|
embed.set_footer(text=f"{user.name}#{user.discriminator} | {user.id}")
|
||||||
|
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
@cog_ext.cog_slash(
|
@slash_command(name="warnings", description="Get count of user warnings")
|
||||||
name="warnings",
|
@slash_option(
|
||||||
description="Get count of user warnings",
|
name="user", description="User to view", option_type=OptionTypes.USER, required=True
|
||||||
options=[
|
)
|
||||||
create_option(
|
@slash_option(
|
||||||
name="user",
|
|
||||||
description="User to view",
|
|
||||||
option_type=6,
|
|
||||||
required=True,
|
|
||||||
),
|
|
||||||
create_option(
|
|
||||||
name="active",
|
name="active",
|
||||||
description="View only active",
|
description="View active only",
|
||||||
option_type=4,
|
option_type=OptionTypes.INTEGER,
|
||||||
required=False,
|
required=False,
|
||||||
choices=[
|
choices=[
|
||||||
create_choice(name="Yes", value=1),
|
SlashCommandChoice(name="Yes", value=1),
|
||||||
create_choice(name="No", value=0),
|
SlashCommandChoice(name="No", value=0),
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@admin_or_permissions(manage_guild=True)
|
@admin_or_permissions(Permissions.MANAGE_GUILD)
|
||||||
async def _warnings(self, ctx: SlashContext, user: User, active: bool = 1) -> None:
|
async def _warnings(self, ctx: InteractionContext, user: User, active: bool = 1) -> None:
|
||||||
active = bool(active)
|
active = bool(active)
|
||||||
exists = self.check_cache(ctx, user_id=user.id, active=active)
|
exists = self.check_cache(ctx, user_id=user.id, active=active)
|
||||||
if exists:
|
if exists:
|
||||||
|
@ -128,7 +117,7 @@ class WarningCog(CacheCog):
|
||||||
description=f"{warnings.count()} total | 0 currently active",
|
description=f"{warnings.count()} total | 0 currently active",
|
||||||
fields=[],
|
fields=[],
|
||||||
)
|
)
|
||||||
embed.set_author(name=user.name, icon_url=user.avatar_url)
|
embed.set_author(name=user.username, icon_url=user.avatar_url)
|
||||||
embed.set_thumbnail(url=ctx.guild.icon_url)
|
embed.set_thumbnail(url=ctx.guild.icon_url)
|
||||||
pages.append(embed)
|
pages.append(embed)
|
||||||
else:
|
else:
|
||||||
|
@ -137,7 +126,7 @@ class WarningCog(CacheCog):
|
||||||
admin = ctx.guild.get_member(warn.admin)
|
admin = ctx.guild.get_member(warn.admin)
|
||||||
admin_name = "||`[redacted]`||"
|
admin_name = "||`[redacted]`||"
|
||||||
if admin:
|
if admin:
|
||||||
admin_name = f"{admin.name}#{admin.discriminator}"
|
admin_name = f"{admin.username}#{admin.discriminator}"
|
||||||
fields.append(
|
fields.append(
|
||||||
Field(
|
Field(
|
||||||
name=warn.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"),
|
name=warn.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"),
|
||||||
|
@ -154,11 +143,11 @@ class WarningCog(CacheCog):
|
||||||
fields=fields[i : i + 5],
|
fields=fields[i : i + 5],
|
||||||
)
|
)
|
||||||
embed.set_author(
|
embed.set_author(
|
||||||
name=user.name + "#" + user.discriminator,
|
name=user.username + "#" + user.discriminator,
|
||||||
icon_url=user.avatar_url,
|
icon_url=user.avatar_url,
|
||||||
)
|
)
|
||||||
embed.set_thumbnail(url=ctx.guild.icon_url)
|
embed.set_thumbnail(url=ctx.guild.icon_url)
|
||||||
embed.set_footer(text=f"{user.name}#{user.discriminator} | {user.id}")
|
embed.set_footer(text=f"{user.username}#{user.discriminator} | {user.id}")
|
||||||
pages.append(embed)
|
pages.append(embed)
|
||||||
else:
|
else:
|
||||||
fields = []
|
fields = []
|
||||||
|
@ -181,24 +170,13 @@ class WarningCog(CacheCog):
|
||||||
fields=fields[i : i + 5],
|
fields=fields[i : i + 5],
|
||||||
)
|
)
|
||||||
embed.set_author(
|
embed.set_author(
|
||||||
name=user.name + "#" + user.discriminator,
|
name=user.username + "#" + user.discriminator,
|
||||||
icon_url=user.avatar_url,
|
icon_url=user.avatar_url,
|
||||||
)
|
)
|
||||||
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(bot=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,
|
||||||
|
@ -210,4 +188,4 @@ class WarningCog(CacheCog):
|
||||||
"paginator": paginator,
|
"paginator": paginator,
|
||||||
}
|
}
|
||||||
|
|
||||||
await paginator.start()
|
await paginator.send(ctx)
|
||||||
|
|
Loading…
Add table
Reference in a new issue