async def on_reset(): message_string = get_reset_message(datetime.datetime.utcnow()) for guild in client.guilds: active_channel = config.get_guild(guild).active_channel channel = guild.get_channel(active_channel) if channel is not None and channel.permissions_for(guild.me).send_messages: await channel.send(message_string)
async def get_config(message, args): guild = client.get_guild(util.safe_int(args.strip(), 0)) if guild is None: if message.guild is None: await message.channel.send("No configuration for private channels") return guild = message.guild config_json = json.dumps(config.get_guild(guild).get_dict(), indent=2, sort_keys=True) await message.channel.send(f"```json\n{config_json}\n```")
async def set_active_channel(message, args): """ Sets this channel as the bot's "active channel", the location where the bot sends reset messages and news. Use `channel none` to disable reset messages and reminders. """ new_config = config.get_guild(message.guild) if args.strip().lower() == "none": new_config.active_channel = 0 await config.set_guild(message.guild, new_config) logger.info(f"Active channel for guild {message.guild.id} disabled") await message.channel.send( "Active channel has been disabled, I won't post reset messages anymore!" ) else: new_config.active_channel = message.channel.id await config.set_guild(message.guild, new_config) logger.info( f"Active channel for guild {message.guild.id} set to {message.channel.id}" ) await message.channel.send( "Active channel has been updated, I'll post reset messages in here from now on!" )
async def set_prefix(message, args): """ Sets the bot's prefix for this server. The prefix is the character (or characters) that come before every command. e.g. running `prefix $` means that to use the `help` command, you now have to type `$help`. The default prefix is `!!`, and you can always reset the prefix by mentioning the bot along with the words `reset prefix` """ new_prefix = args.strip() if len(new_prefix) > 250: await message.channel.send("That's too long! Try something shorter.") return if not new_prefix: await message.channel.send( "You need to choose something to set as the prefix!") return if new_prefix == client.user.mention: new_prefix += " " guild = message.guild new_config = config.get_guild(guild) new_config.token = new_prefix await config.set_guild(guild, new_config) logger.info(f'Prefix for guild {guild.id} set to "{new_prefix}"') await message.channel.send(f"Prefix has been set to `{new_prefix}`")
async def before_reset(): for guild in client.guilds: active_channel = config.get_guild(guild).active_channel channel = guild.get_channel(active_channel) if channel is not None and channel.permissions_for(guild.me).send_messages: await channel.trigger_typing()
async def add_config(guild: discord.Guild): logger.info(f"Joined guild {guild.id} ({guild.name})") config.get_guild(guild)
async def check_news(reschedule): if reschedule: now = datetime.datetime.utcnow() next_check = now.replace(minute=5 * math.floor(now.minute / 5), second=1, microsecond=0) + datetime.timedelta(minutes=5) time_delta = (next_check - now).total_seconds() asyncio.get_event_loop().call_later( time_delta, lambda: asyncio.ensure_future(check_news(True))) if time_delta < 240: return async with aiohttp.ClientSession() as session: list_base_url = "https://dragalialost.com/api/index.php?" \ "format=json&type=information&action=information_list&lang=en_us&priority_lower_than=" response_json = await get_api_json_response(session, list_base_url) if not response_json: logger.error("Could not retrieve article list") return query_result = response_json["data"] wc = config.get_writeable() stored_ids = wc.news_ids stored_time = wc.news_update_time if not stored_ids and not stored_time: wc.news_ids = query_result["new_article_list"] wc.news_update_time = math.ceil(time.time()) logger.info( f"Regenerated article history, time = {wc.news_update_time}, IDs = {wc.news_ids}" ) await config.set_writeable(wc) return # new posts new_article_ids = list(reversed(query_result["new_article_list"])) new_article_ids = [i for i in new_article_ids if i not in stored_ids] new_stored_ids = query_result["new_article_list"].copy() or stored_ids # updated posts updated_articles = sorted(query_result["update_article_list"], key=lambda d: d["update_time"]) updated_article_ids = [ d["id"] for d in updated_articles if d["update_time"] > stored_time ] updated_article_ids = [ i for i in updated_article_ids if i not in new_article_ids ] new_stored_time = updated_articles[-1][ "update_time"] if updated_articles else stored_time # filter blacklisted articles article_blacklist = config.get_global( "general")["news_article_blacklist"] news_articles = [ i for i in new_article_ids + updated_article_ids if i not in article_blacklist ] if len(news_articles) >= 10: # too many news items, post a generic notification embeds = [ discord.Embed( title="New news posts are available", url="https://dragalialost.com/en/news/", description= f"{len(news_articles)} new news posts are available! Click the link above to read them.", color=get_news_colour()).set_author( name="Dragalia Lost News", icon_url=news_icon) ] else: # generate embeds from articles embeds = [] for article_id in news_articles: article_embed = await get_article_embed( session, article_id, article_id in updated_article_ids) if article_embed: embeds.append(article_embed) # update config wc.news_ids = new_stored_ids wc.news_update_time = new_stored_time if wc.news_ids != stored_ids or wc.news_update_time != stored_time: await config.set_writeable(wc) # post articles if embeds: for guild in client.guilds: active_channel = config.get_guild(guild).active_channel channel = guild.get_channel(active_channel) if channel is not None and channel.permissions_for( guild.me).send_messages: asyncio.ensure_future( exec_in_order([channel.send(embed=e) for e in embeds]))