Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ auto-report_stack-trace.txt

# For beta purposes
cogs/fun.py
cogs/images.py
cogs/globalban.py
cogs/globalban.i18n.json
cogs/handlers.py
Expand Down
137 changes: 137 additions & 0 deletions cogs/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
from logging import getLogger
from time import perf_counter
from typing import Literal, Optional

import discord
from discord import app_commands
from discord.ext import commands

from core import Context, MyClient, update_slash_localizations

logger = getLogger(__name__)


class Admin(commands.Cog):
def __init__(self, client: MyClient):
self.client: MyClient = client

@commands.hybrid_command(
hidden=True, name="reload", description="reload_specs-description", usage="reload_specs-usage"
)
@commands.is_owner()
@app_commands.describe(cog="reload_specs-args-cog-description")
@app_commands.rename(cog="reload_specs-args-cog-name")
async def reload(self, ctx: Context, cog: str):
try:
benchmark = perf_counter()
await self.client.reload_extension(f"cogs.{cog}")
end = perf_counter() - benchmark
await ctx.reply(content=f"Reloaded extension `{cog}` in **{end:.2f}s**")
logger.info(f"{ctx.author.name} reloaded {cog}.py")
except Exception as e:
await ctx.reply(content=f"Failed to reload extension `{cog}`: {e}")

@commands.hybrid_command(hidden=True, name="load", description="load_specs-description", usage="load_specs-usage")
@commands.is_owner()
@app_commands.describe(cog="load_specs-args-cog-description")
@app_commands.rename(cog="load_specs-args-cog-name")
async def load(self, ctx: Context, cog: str):
try:
benchmark = perf_counter()
await self.client.load_extension(f"cogs.{cog}")
end = perf_counter() - benchmark
await ctx.reply(content=f"Loaded extension `{cog}` in **{end:.2f}s**")
logger.info(f"{ctx.author.name} loaded {cog}.py")
except Exception as e:
await ctx.reply(content=f"Failed to load extension `{cog}`: {e}")

@commands.hybrid_command(
hidden=True, name="unload", description="unload_specs-description", usage="unload_specs-usage"
)
@commands.is_owner()
@app_commands.describe(cog="unload_specs-args-cog-description")
@app_commands.rename(cog="unload_specs-args-cog-name")
async def unload(self, ctx: Context, cog: str):
try:
benchmark = perf_counter()
await self.client.unload_extension(f"cogs.{cog}")
end = perf_counter() - benchmark
await ctx.reply(content=f"Unloaded extension `{cog}` in **{end:.2f}s**")
logger.info(f"{ctx.author.name} unloaded {cog}.py")
except Exception as e:
await ctx.reply(content=f"Failed to unload extension `{cog}`: {e}")

@commands.hybrid_command(
hidden=True, name="l10nreload", description="l10nreload_specs-description", usage="l10nreload_specs-usage"
)
@commands.is_owner()
@app_commands.describe(path="l10nreload_specs-args-path-description")
@app_commands.rename(path="l10nreload_specs-args-path-name")
async def l10nreload(self, ctx: Context, path: str = "./localization"):
ctx.bot.custom_response.load_localizations(path)
await ctx.reply(content="Reloaded localization files.")
logger.info(f"{ctx.author.name} reloaded localization files.")

@commands.hybrid_command(hidden=True, name="sync", description="sync_specs-description", usage="sync_specs-usage")
@commands.is_owner()
@app_commands.describe(
guilds="sync_specs-args-guilds-description",
scope="sync_specs-args-scope-description",
)
@app_commands.rename(guilds="sync_specs-args-guilds-name", scope="sync_specs-args-scope-name")
@app_commands.choices(
scope=[
app_commands.Choice(name="sync_specs-args-scope-local", value="~"),
app_commands.Choice(name="sync_specs-args-scope-global", value="*"),
app_commands.Choice(name="sync_specs-args-scope-resync", value="^"),
app_commands.Choice(name="sync_specs-args-scope-slash", value="/"),
]
)
async def sync(
self,
ctx: Context,
guilds: commands.Greedy[discord.Object] = None,
scope: Optional[Literal["~", "*", "^", "/"]] = None,
) -> None:
tree: discord.app_commands.CommandTree[ctx.bot] = ctx.bot.tree # type: ignore
benchmark = perf_counter()

if not guilds:
if scope == "~":
synced = await tree.sync(guild=ctx.guild)
elif scope == "*":
tree.copy_global_to(guild=ctx.guild)
synced = await tree.sync(guild=ctx.guild)
elif scope == "^":
tree.clear_commands(guild=ctx.guild)
await tree.sync(guild=ctx.guild)
synced = []
elif scope == "/":
update_slash_localizations()
await ctx.reply(content="Reloaded slash localizations")
return
else:
update_slash_localizations()
synced = await tree.sync()

end = perf_counter() - benchmark
await ctx.reply(
content=f"Synced **{len(synced)}** {'commands' if len(synced) != 1 else 'command'} {'globally' if scope is None else 'to the current guild'}, took **{end:.2f}s**"
)
else:
update_slash_localizations()
guilds_synced = 0
for guild in guilds:
try:
await tree.sync(guild=guild)
except discord.HTTPException:
pass
else:
guilds_synced += 1

end = perf_counter() - benchmark
await ctx.reply(content=f"Synced the tree to **{guilds_synced}/{len(guilds)}** guilds, took **{end:.2f}s**")


async def setup(client: MyClient):
await client.add_cog(Admin(client))
12 changes: 5 additions & 7 deletions cogs/afk.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from typing import TYPE_CHECKING, Optional
from typing import Optional

import discord
from discord import app_commands
from discord.ext import commands

from core import Context, MyClient
from helpers import CustomMember, CustomUser, regex

if TYPE_CHECKING:
from main import Context, MyClient


@app_commands.guild_only()
@commands.guild_only()
class AFK(commands.Cog):
def __init__(self, client: "MyClient"):
def __init__(self, client: MyClient):
self.client = client
self.custom_response = client.custom_response

Expand Down Expand Up @@ -86,7 +84,7 @@ async def answer_afk_reason(self, message: discord.Message) -> None:
@commands.hybrid_command(name="afk", description="afk_specs-description", usage="afk_specs-usage")
@app_commands.rename(reason="afk_specs-args-reason-name")
@app_commands.describe(reason="afk_specs-args-reason-description")
async def afk(self, ctx: "Context", reason: Optional[str] = None):
async def afk(self, ctx: Context, reason: Optional[str] = None):
if not reason:
reason = await self.custom_response("afk.dnd", ctx)

Expand Down Expand Up @@ -147,5 +145,5 @@ async def afk(self, ctx: "Context", reason: Optional[str] = None):
return await ctx.send("afk.on")


async def setup(client: "MyClient"):
async def setup(client: MyClient):
await client.add_cog(AFK(client))
12 changes: 4 additions & 8 deletions cogs/basic.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
from __future__ import annotations

from time import perf_counter
from typing import TYPE_CHECKING

from discord.ext import commands

if TYPE_CHECKING:
from main import Context, MyClient
from core import Context, MyClient


class Basic(commands.Cog, name="Basic"):
def __init__(self, client: "MyClient"):
def __init__(self, client: MyClient):
self.client = client

@commands.hybrid_command(name="ping", description="ping_specs-description")
async def ping(self, ctx: "Context"):
async def ping(self, ctx: Context):
# Database ping calculation
database_start = perf_counter()
await self.client.db.execute("SELECT 1")
Expand All @@ -23,5 +19,5 @@ async def ping(self, ctx: "Context"):
await ctx.send("ping", latency=float(self.client.latency), db=float(database))


async def setup(client: "MyClient"):
async def setup(client: MyClient):
await client.add_cog(Basic(client))
Loading