Skip to content

Commit

Permalink
some qol things
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicTheDev committed Dec 16, 2024
1 parent e86155f commit 173995c
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 81 deletions.
8 changes: 8 additions & 0 deletions background/tasks/background_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ async def guilds_store(self):

self.bot.SERVER_MAP = {g.id : g for shard in self.bot.SHARD_DATA for g in shard.servers}

channel = self.bot.get_channel(937528942661877851)
if channel is not None:
number_of_servers = len(list(self.bot.SERVER_MAP.keys()))
new_channel_name = f'ClashKing: {number_of_servers} Servers'
if new_channel_name != channel.name:
await channel.edit(name=new_channel_name)


@guilds_store.before_loop
async def before_guilds_store(self):
await self.bot.wait_until_ready()
Expand Down
2 changes: 2 additions & 0 deletions commands/clan/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from disnake.ext import commands

from discord import autocomplete, options
from utility.discord_utils import user_command

from .utils import *

Expand All @@ -9,6 +10,7 @@ class ClanCommands(commands.Cog, name='Clan Commands'):
def __init__(self, bot: CustomClient):
self.bot = bot

@user_command()
@commands.slash_command(name='clan')
async def clan(self, ctx: disnake.ApplicationCommandInteraction):
await ctx.response.defer()
Expand Down
4 changes: 3 additions & 1 deletion commands/clan/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

@register_button('clandetailed', parser='_:clan')
async def detailed_clan_board(bot: CustomClient, clan: coc.Clan, server: disnake.Guild, embed_color: disnake.Color):
db_clan = await bot.clan_db.find_one({'$and': [{'tag': clan.tag}, {'server': server.id}]})
db_clan = None
if server:
db_clan = await bot.clan_db.find_one({'$and': [{'tag': clan.tag}, {'server': server.id}]})

clan_legend_ranking = await bot.clan_leaderboard_db.find_one({'tag': clan.tag})

Expand Down
10 changes: 7 additions & 3 deletions commands/components/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async def button_logic(
bot: CustomClient,
guild: disnake.Guild,
locale: disnake.Locale,
ctx: disnake.MessageInteraction = None,
ctx: disnake.MessageInteraction | None = None,
autoboard: bool = False,
):
split_data = button_data.split(':')
Expand Down Expand Up @@ -57,10 +57,14 @@ async def button_logic(
elif name == 'custom_player':
data = await bot.getPlayer(player_tag=data, custom=True)
elif name == 'discord_user':
data = await ctx.guild.getch_member(data)
ctx: disnake.ApplicationCommandInteraction
if ctx.guild:
data = await ctx.guild.getch_member(data)
else:
data = ctx.user
hold_kwargs[name] = data

embed_color = await bot.ck_client.get_server_embed_color(server_id=guild.id)
embed_color = await bot.ck_client.get_server_embed_color(server_id=None if not guild else guild.id)
hold_kwargs['embed_color'] = embed_color
hold_kwargs = {key: hold_kwargs[key] for key in inspect.getfullargspec(function).args}
embed = await function(**hold_kwargs)
Expand Down
5 changes: 3 additions & 2 deletions commands/dev/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import random
from classes.bot import CustomClient
from discord import convert, autocomplete
from utility.constants import EMBED_COLOR_CLASS

class OwnerCommands(commands.Cog):
def __init__(self, bot: CustomClient):
Expand Down Expand Up @@ -135,8 +136,8 @@ async def test(self, ctx: ApplicationCommandInteraction):
pass


@dev.sub_command(name='autoboard-limit', description="Set a new autoboard limit for a server")
async def autoboard_limit(self, ctx: ApplicationCommandInteraction,
@dev.sub_command(name='automation-limit', description="Set a new autoboard limit for a server")
async def automation_limit(self, ctx: ApplicationCommandInteraction,
server: disnake.Guild =commands.Param(converter=convert.server, autocomplete=autocomplete.all_server),
new_limit: int = commands.Param()):
await ctx.response.defer()
Expand Down
2 changes: 2 additions & 0 deletions commands/legends/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from discord import autocomplete, convert, options
from exceptions.CustomExceptions import PlayerNotInLegends
from utility.constants import POSTER_LIST
from utility.discord_utils import user_command

from .utils import (
legend_buckets,
Expand All @@ -23,6 +24,7 @@ class Legends(commands.Cog):
def __init__(self, bot: CustomClient):
self.bot = bot

@user_command()
@commands.slash_command(
name=disnake.Localized('legends', key='legends-name'),
description=disnake.Localized(key='legends-description'),
Expand Down
4 changes: 4 additions & 0 deletions commands/player/commands.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import disnake
from disnake.ext import commands
from disnake.ext.commands import dm_only

from classes.bot import CustomClient
from classes.player.stats import StatsPlayer
from discord.options import autocomplete, convert
from exceptions.CustomExceptions import *
from utility.player_pagination import button_pagination
from utility.search import search_results
from utility.discord_utils import user_command
from .utils import player_accounts, to_do_embed


Expand All @@ -17,13 +19,15 @@ class PlayerCommands(commands.Cog, name='Player Commands'):
def __init__(self, bot: CustomClient):
self.bot = bot

@user_command()
@commands.slash_command(
name=disnake.Localized('player', key='player-name'),
description=disnake.Localized(key='player-description'),
)
async def player(self, ctx: disnake.ApplicationCommandInteraction):
await ctx.response.defer()


@player.sub_command(
name=disnake.Localized('lookup', key='lookup-name'),
description='Lookup a player or discord user',
Expand Down
43 changes: 31 additions & 12 deletions commands/ticketing/click.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from datetime import datetime
from datetime import datetime
from typing import List

import coc
import disnake
import pytz
from disnake.ext import commands

import pendulum as pend
from classes.bot import CustomClient
from classes.player.stats import StatsPlayer
from classes.tickets import LOG_TYPE, OpenTicket, TicketPanel
Expand Down Expand Up @@ -201,14 +200,36 @@ def check(res):
)
await ctx.channel.delete()

panel_settings = await self.bot.tickets.find_one(
{
'$and': [
{'server_id': ctx.guild.id},
{'name': ctx.data.custom_id.split('_')[0]},
]
}
)
def is_unix_timestamp(timestamp: str) -> bool:
"""
Check if a string is a valid Unix timestamp.
Args:
timestamp (str): The string to check.
Returns:
bool: True if the string is a valid Unix timestamp, False otherwise.
"""
try:
# Ensure the string can be converted to an integer
ts_int = int(timestamp)

# Check if the timestamp is within a reasonable range for Unix time
pend.from_timestamp(ts_int)
return True
except (ValueError, OverflowError):
return False

panel_settings = None
if is_unix_timestamp(ctx.data.custom_id.split("_")[-1]):
panel_settings = await self.bot.tickets.find_one(
{
'$and': [
{'server_id': ctx.guild.id},
{'name': ctx.data.custom_id.split('_')[0]},
]
}
)
if panel_settings is not None:
member = await ctx.guild.getch_member(ctx.user.id)
panel = TicketPanel(bot=self.bot, panel_settings=panel_settings)
Expand Down Expand Up @@ -320,14 +341,12 @@ def check(res):
else:
await res.response.send_message(content='Done!', components=[], ephemeral=True)

print(button.questions)
if button.questions:
if message:
await message.delete()
(message, questionaire_embed) = await ask_questions(bot=self.bot, ctx=ctx, questions=button.questions)
embeds.append(questionaire_embed)

print("here again")
channels = await open_ticket(
bot=self.bot,
ticket_panel=panel,
Expand Down
6 changes: 6 additions & 0 deletions discord/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ async def category(self, ctx: disnake.ApplicationCommandInteraction, query: str)

async def clan(self, ctx: disnake.ApplicationCommandInteraction, query: str):
if ctx.guild is None:
last_record = await self.bot.command_stats.find_one(
{"$and" : [{"user" : ctx.user.id}, {"server": {"$ne": None}}]},
sort=[("time", -1)]
)
guild_id = 0
if last_record:
guild_id = last_record.get("server")
else:
guild_id = ctx.guild.id
if ctx.filled_options.get('family') is not None:
Expand Down
Loading

0 comments on commit 173995c

Please sign in to comment.