235 lines
7.1 KiB
Python
235 lines
7.1 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
import discord
|
|
from discord.ext import commands, pages
|
|
|
|
from payphone.db import models
|
|
from payphone.bot import Payphone
|
|
from payphone.cogs import base
|
|
from payphone.cogs.admin import embeds, views
|
|
from payphone.core import checks, constants, formatting
|
|
|
|
|
|
class Admin(base.Cog):
|
|
"""Administration in Payphone."""
|
|
|
|
show_in_help = False
|
|
|
|
def __init__(self, bot: Payphone) -> None:
|
|
"""Initializes the Calls cog.
|
|
|
|
Args:
|
|
bot (Payphone): The bot instance.
|
|
"""
|
|
|
|
self.bot = bot
|
|
|
|
@commands.command(aliases=["mod"])
|
|
@commands.is_owner()
|
|
async def moderator(
|
|
self, ctx: commands.Context, user: discord.User, toggle: bool = True
|
|
) -> None:
|
|
"""Gives the user the Moderator role.
|
|
|
|
Options:
|
|
- `<user>`: The user to give the role.
|
|
- `[toggle]`: Whether to give or remove the role.
|
|
|
|
Permissions:
|
|
- Bot Owner
|
|
"""
|
|
|
|
instance, _ = await models.User.update_or_create(
|
|
id=user.id, defaults={"is_moderator": toggle}
|
|
)
|
|
await instance.set_cache(self.bot.redis)
|
|
|
|
await ctx.send(
|
|
f"Toggled the moderator role {'on' if toggle else 'off'} for {user.mention}."
|
|
)
|
|
|
|
@commands.command(aliases=["b"])
|
|
@checks.is_moderator()
|
|
async def ban(
|
|
self, ctx: commands.Context, user: discord.User | int | None = None
|
|
) -> None:
|
|
"""Bans the user from the bot.
|
|
|
|
Options:
|
|
- `[user]`: The user to ban or their ID. If none is provided, the author will be banned.
|
|
|
|
Permissions:
|
|
- Bot Moderator
|
|
"""
|
|
|
|
user_id = user if isinstance(user, int) else user.id if user else ctx.author.id
|
|
|
|
await self.bot.ban(user_id, None, None, ctx.author.id)
|
|
|
|
await ctx.send(f"<@!{user_id}> was banned from Payphone.")
|
|
|
|
@commands.command(aliases=["ub"])
|
|
@checks.is_moderator()
|
|
async def unban(
|
|
self, ctx: commands.Context, user: discord.User | None = None
|
|
) -> None:
|
|
"""Unbans the user from the bot.
|
|
|
|
Options:
|
|
- `[user]`: The user to unban. If none is provided, the author will be unbanned.
|
|
|
|
Permissions:
|
|
- Bot Moderator
|
|
"""
|
|
|
|
user = user or ctx.author
|
|
|
|
await self.bot.unban(user.id)
|
|
|
|
await ctx.send(f"{user.mention} was unbanned from Payphone.")
|
|
|
|
try:
|
|
await user.send("🔔 You were unbanned from Payphone!")
|
|
|
|
except discord.Forbidden:
|
|
pass
|
|
|
|
@commands.command(aliases=["sup"])
|
|
@commands.is_owner()
|
|
async def supporter(
|
|
self, ctx: commands.Context, user: discord.User, months: int = 1
|
|
) -> None:
|
|
"""Gives the user the Supporter role.
|
|
|
|
Options:
|
|
- `<user>`: The user to give the role.
|
|
- `[months]`: The number of months to give the role.
|
|
|
|
Permissions:
|
|
- Bot Owner
|
|
"""
|
|
|
|
instance, _ = await models.User.update_or_create(
|
|
id=user.id,
|
|
defaults={
|
|
"is_supporter_until": (
|
|
datetime.now(timezone.utc) + timedelta(days=30 * months)
|
|
if months > 0
|
|
else None
|
|
)
|
|
},
|
|
)
|
|
await instance.set_cache(self.bot.redis)
|
|
|
|
if months:
|
|
await ctx.send(
|
|
f"Gave the Supporter role to <@{user.id}> for {months} months."
|
|
)
|
|
|
|
else:
|
|
await ctx.send(f"Removed the Supporter role from <@{user.id}>.")
|
|
|
|
@commands.command()
|
|
@checks.is_moderator()
|
|
async def reports(self, ctx: commands.Context) -> None:
|
|
"""Shows the reports.
|
|
|
|
Permissions:
|
|
- Bot Moderator
|
|
"""
|
|
|
|
report = await models.Report.filter(resolved=False).first()
|
|
|
|
if report is None:
|
|
return await ctx.send("✅ No reports left, have this cookie 🍪 !!")
|
|
|
|
await ctx.send(
|
|
embed=embeds.report_embed(report), view=views.ReportView(self.bot, report)
|
|
)
|
|
|
|
@commands.command()
|
|
@checks.is_moderator()
|
|
async def bans(self, ctx: commands.Context, user: discord.User | int) -> None:
|
|
"""Shows all bans of a specific user.
|
|
|
|
Options:
|
|
- `<user>`: The user to show the bans of.
|
|
|
|
Permissions:
|
|
- Bot Moderator
|
|
"""
|
|
|
|
user_id = user if isinstance(user, int) else user.id
|
|
|
|
bans = (
|
|
await models.Ban.filter(user_id=user_id)
|
|
.prefetch_related("report", "created_by")
|
|
.order_by("-created_at")
|
|
)
|
|
|
|
if not bans:
|
|
await ctx.send(f"<@!{user_id}> has no bans.")
|
|
return
|
|
|
|
await pages.Paginator(
|
|
[
|
|
(
|
|
discord.Embed(
|
|
color=constants.PRIMARY_COLOR,
|
|
title=f"Ban #{ban.id}",
|
|
description=formatting.trim(ban.report.message_content, 500),
|
|
author=discord.EmbedAuthor(
|
|
name=ban.report.user_name,
|
|
icon_url=ban.report.user_avatar_url,
|
|
),
|
|
fields=[
|
|
discord.EmbedField(
|
|
name="Banned Until",
|
|
value=(
|
|
f"<t:{int(ban.expires_at.timestamp())}> (<t:{int(ban.expires_at.timestamp())}:R>)"
|
|
if ban.expires_at
|
|
else "Forever"
|
|
),
|
|
),
|
|
discord.EmbedField(
|
|
name="User Avatar",
|
|
value=f"[Click to open avatar URL](<{ban.report.user_avatar_url}>)",
|
|
),
|
|
]
|
|
+ (
|
|
[
|
|
discord.EmbedField(
|
|
name="Replying To",
|
|
value=f'"{formatting.trim(ban.report.reply_message_content)}" by {ban.report.reply_user_name}',
|
|
)
|
|
]
|
|
if ban.report.reply_message_content
|
|
else []
|
|
),
|
|
thumbnail=ban.report.user_avatar_url,
|
|
footer=discord.EmbedFooter(
|
|
text=f"Banned by {(await self.bot.get_or_fetch_user(ban.created_by.id)).display_name}"
|
|
),
|
|
)
|
|
if ban.report
|
|
else discord.Embed(
|
|
color=constants.PRIMARY_COLOR,
|
|
title=f"Ban #{ban.id}",
|
|
description=f"The user was banned manually by <@!{ban.created_by.id}> <t:{int(ban.created_at.timestamp())}:R>.",
|
|
)
|
|
)
|
|
for ban in bans
|
|
],
|
|
author_check=True,
|
|
).send(ctx)
|
|
|
|
|
|
def setup(bot: Payphone) -> None:
|
|
"""Loads the Admin cog into the bot.
|
|
|
|
Args:
|
|
bot (Payphone): The bot instance.
|
|
"""
|
|
|
|
bot.add_cog(Admin(bot))
|