-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from TheXer/udalost
Udalost
- Loading branch information
Showing
12 changed files
with
231 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from typing import Literal, Optional, TYPE_CHECKING | ||
|
||
import discord | ||
from discord.ext import commands | ||
from discord.ext.commands import Greedy, Context | ||
|
||
if TYPE_CHECKING: | ||
from src.jachym import Jachym | ||
|
||
|
||
class SyncSlashCommands(commands.Cog): | ||
def __init__(self, bot: "Jachym"): | ||
self.bot = bot | ||
|
||
@commands.command() | ||
@commands.guild_only() | ||
@commands.is_owner() | ||
async def sync( | ||
self, | ||
ctx: Context, | ||
guilds: Greedy[discord.Guild], | ||
spec: Optional[Literal["-", "*", "^"]] = None) -> None: | ||
""" | ||
A command to sync all slash commands to servers user requires. Works like this: | ||
!sync | ||
global sync - syncs all slash commands with all guilds | ||
!sync - | ||
sync current guild | ||
!sync * | ||
copies all global app commands to current guild and syncs | ||
!sync ^ | ||
clears all commands from the current guild target and syncs (removes guild commands) | ||
!sync id_1 id_2 | ||
syncs guilds with id 1 and 2 | ||
Args: | ||
ctx: commands.Context | ||
guilds: Greedy[discord.Object] | ||
spec: Optional[Literal] | ||
Returns: Synced slash command | ||
""" | ||
|
||
if not guilds: | ||
if spec == "-": | ||
synced = await self.bot.tree.sync(guild=ctx.guild) | ||
elif spec == "*": | ||
self.bot.tree.copy_global_to(guild=ctx.guild) | ||
synced = await self.bot.tree.sync(guild=ctx.guild) | ||
elif spec == "^": | ||
self.bot.tree.clear_commands(guild=ctx.guild) | ||
await self.bot.tree.sync(guild=ctx.guild) | ||
synced = [] | ||
else: | ||
synced = await self.bot.tree.sync() | ||
|
||
await ctx.send( | ||
f"Synced {len(synced)} commands {'globally' if spec is None else 'to the current guild.'}" | ||
) | ||
return | ||
|
||
ret = 0 | ||
for guild in guilds: | ||
try: | ||
await self.bot.tree.sync(guild=guild) | ||
except discord.HTTPException: | ||
pass | ||
else: | ||
ret += 1 | ||
|
||
await ctx.send(f"Synced the tree to {ret}/{len(guilds)}.") | ||
|
||
|
||
async def setup(bot): | ||
await bot.add_cog(SyncSlashCommands(bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
python-dotenv==0.17.1 | ||
|
||
aiomysql>=0.0.22 | ||
aiomysql>=0.0.22 | ||
pytest>=7.3.1 | ||
loguru>=0.7.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
import time | ||
from functools import wraps | ||
|
||
from loguru import logger | ||
|
||
def timeit(func): | ||
|
||
def timeit(func: callable): | ||
@wraps(func) | ||
async def async_wrapper(*args, **kwargs): | ||
print(f"{func.__name__} starting...") | ||
logger.info(f"{func.__name__} starting...") | ||
start = time.time() | ||
result = await func(*args, **kwargs) | ||
duration = time.time() - start | ||
print(f'{func.__name__} took {duration:.2f} seconds') | ||
|
||
logger.info(f'{func.__name__} took {duration:.2f} seconds') | ||
return result | ||
|
||
return async_wrapper |
Oops, something went wrong.