async def register_slash_commands(self): guild_id = CONFIG.get('slash_command_guild_id') existing_commands = await get_all_commands(self.user.id, TOKEN, guild_id=guild_id) new_command_names = [c['function'] for c in COMMAND_REGISTRY] for command in existing_commands: if command['name'] not in new_command_names or CONFIG.get( 'deregister_slash_commands'): log.debug(f'Deregistering slash command {command["name"]}...') await remove_slash_command(self.user.id, TOKEN, guild_id, command['id']) if not CONFIG.get('register_slash_commands'): return for command in COMMAND_REGISTRY: if 'description' not in command: continue if command['function'] in [c['name'] for c in existing_commands]: continue log.debug(f'Registering slash command {command["function"]}...') await add_slash_command(self.user.id, bot_token=TOKEN, guild_id=guild_id, cmd_name=command['function'], description=command['description'], options=command.get('options', []))
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) log.debug( f'--------------------------- Starting {self.BOT_NAME} v{self.VERSION} --------------------------' ) self.expander = TeamExpander() self.tower_data = TowerOfDoomData(self.my_emojis) self.prefix = models.Prefix(CONFIG.get('default_prefix')) self.language = models.Language(CONFIG.get('default_language')) self.subscriptions = models.Subscriptions() self.views = Views(emojis={})
async def task_check_for_data_updates(discord_client): filenames = LANG_FILES + [ 'World.json', 'User.json', 'Campaign.json', 'Soulforge.json' ] now = datetime.datetime.now() modified_files = [] for filename in filenames: file_path = GameAssets.path(filename) try: modification_time = datetime.datetime.fromtimestamp( os.path.getmtime(file_path)) except FileNotFoundError: continue modified = now - modification_time <= datetime.timedelta( seconds=CONFIG.get('file_update_check_seconds')) if modified: modified_files.append(filename) if modified_files: log.debug( f'Game file modification detected, reloading {", ".join(modified_files)}.' ) await asyncio.sleep(5) lock = asyncio.Lock() async with lock: try: old_expander = discord_client.expander del discord_client.expander discord_client.expander = TeamExpander() update_translations() except Exception as e: log.error('Could not update game file. Stacktrace follows.') log.exception(e) discord_client.expander = old_expander
async def soulforge_preview(self, message, lang, search_term, release_date=None, switch=None, **kwargs): if switch is None: switch = CONFIG.get('default_news_platform') == 'switch' async with message.channel.typing(): if message.interaction_id: await self.send_slash_command_result( message, content=None, embed=None, response_type=InteractionResponseType.PONG.value) start = time.time() weapon_data = self.expander.get_soulforge_weapon_image_data( search_term, release_date, switch, lang) if not weapon_data: e = discord.Embed( title= f'Weapon search for `{search_term}` did not yield any result', description=':(', color=self.BLACK) return await self.answer(message, e) image_data = graphic_soulforge_preview.render_all(weapon_data) result = discord.File(image_data, f'soulforge_{release_date}.png') duration = time.time() - start log.debug(f'Soulforge generation took {duration:0.2f} seconds.') await message.channel.send(file=result)
async def campaign_preview(self, message, lang, switch=None, team_code=None, **kwargs): switch = switch or CONFIG.get('default_news_platform') == 'switch' async with message.channel.typing(): if hasattr(message, 'interaction_id') and message.interaction_id: await self.send_slash_command_result( message, response_type=InteractionResponseType.MESSAGE.value, content='Please stand by ...', embed=None) start = time.time() campaign_data = self.expander.get_campaign_tasks(lang) campaign_data['switch'] = switch campaign_data['team'] = None if team_code: campaign_data['team'] = self.expander.get_team_from_message( team_code, lang) image_data = graphic_campaign_preview.render_all(campaign_data) result = discord.File( image_data, f'campaign_{campaign_data["start_date"]}.png') duration = time.time() - start log.debug(f'Soulforge generation took {duration:0.2f} seconds.') await message.channel.send(file=result)
async def news_subscribe(self, message, platform, **kwargs): if not platform: platform = CONFIG.get('default_news_platform') await self.subscriptions.add(message.guild, message.channel, platform) e = self.generate_response( 'News management', self.WHITE, f'News for {platform.title()}', f'Channel {message.channel.name} is now subscribed and will receive future news.' ) await self.answer(message, e)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) log.debug( f'--------------------------- Starting {self.BOT_NAME} v{self.VERSION} --------------------------' ) self.expander = TeamExpander() self.tower_data = TowerOfDoomData(self.my_emojis) self.prefix = models.Prefix(CONFIG.get('default_prefix')) self.language = models.Language(CONFIG.get('default_language')) self.subscriptions = models.Subscriptions() self.views = Views(emojis={}) self.pet_rescues = [] self.pet_rescue_config: PetRescueConfig = None token = CONFIG.get('dbl_token') self.dbl_client = None self.server_status_cache = {'last_updated': datetime.datetime.min} if token: self.dbl_client = dbl.DBLClient(self, token)
async def register_slash_commands(self): log.debug('Deregistering all slash commands...') guild_id = CONFIG.get('slash_command_guild_id') for command in await get_all_commands(self.user.id, TOKEN, guild_id=guild_id): await remove_slash_command(self.user.id, TOKEN, guild_id, command['id']) if not CONFIG.get('register_slash_commands'): return log.debug(f'Registering slash commands...') for command in COMMAND_REGISTRY: if 'description' not in command: continue await add_slash_command(self.user.id, bot_token=TOKEN, guild_id=guild_id, cmd_name=command['function'], description=command['description'], options=command.get('options', []))
async def wrapper(*args, **kwargs): self = args[0] message = kwargs['message'] is_special = message.author.id in CONFIG.get('special_users') if not await self.is_owner(message) and not is_special: e = discord.Embed(title='Owner command', color=self.RED) e.add_field(name='Error', value=f'Only the bot owner has permission to use this command.') await self.answer(message, e) return await function(*args, **kwargs)
import asyncio import datetime import os from discord.ext import tasks from base_bot import log from configurations import CONFIG from game_assets import GameAssets from jobs.news_downloader import NewsDownloader from search import TeamExpander, update_translations from translations import LANG_FILES @tasks.loop(minutes=CONFIG.get('news_check_interval_minutes'), reconnect=False) async def task_check_for_news(discord_client): lock = asyncio.Lock() async with lock: try: downloader = NewsDownloader() downloader.process_news_feed() await discord_client.show_latest_news() except Exception as e: log.error('Could not update news. Stacktrace follows.') log.exception(e) @tasks.loop(seconds=CONFIG.get('file_update_check_seconds')) async def task_check_for_data_updates(discord_client): filenames = LANG_FILES + [ 'World.json', 'User.json', 'Campaign.json', 'Soulforge.json'
async def update_base_emojis(self): guilds = [CONFIG.get('base_guild'), CONFIG.get('base_guild_2')] for guild in guilds: await self.fetch_emojis_from_guild(guild)
def __init__(self): self.filename = CONFIG.get('database') self.conn = None self.cursor = None self.connect()
async def update_base_emojis(self): for guild_id in CONFIG.get('base_guilds'): await self.fetch_emojis_from_guild(guild_id)
def load(filename): path = os.path.join(CONFIG.get('game_assets_folder'), filename) with open(path, encoding='utf8') as f: return json.load(f)
def exists(filename): path = os.path.join(CONFIG.get('game_assets_folder'), filename) return os.path.exists(path)
def path(filename): return os.path.join(CONFIG.get('game_assets_folder'), filename)