A RedBot cog for sending notifications when there are new founderless regions in NationStates.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 line
7.7 KiB

  1. from redbot.core import commands, checks, Config
  2. from redbot.core.utils.chat_formatting import pagify
  3. import discord
  4. import asyncio
  5. import requests
  6. import xml.etree.cElementTree as et
  7. import time
  8. import datetime
  9. import gzip
  10. R_HEADERS = {'User-Agent': '<Nation: Haku>'}
  11. RATE_LIMIT = 0.7
  12. def canonicalize(in_str):
  13. return in_str.lower().replace(' ', '_')
  14. def get_founderless_regions():
  15. """Return the list of founderless regions."""
  16. time.sleep(RATE_LIMIT)
  17. r = requests.get('https://www.nationstates.net/cgi-bin/api.cgi?q=regionsbytag;tags=founderless',
  18. headers=R_HEADERS)
  19. tree = et.fromstring(r.text)
  20. return [canonicalize(region) for region in tree[0].text.split(',')]
  21. def download_region_dump():
  22. """Download the latest region dump from the NS API."""
  23. time.sleep(RATE_LIMIT)
  24. r = requests.get('https://www.nationstates.net/pages/regions.xml.gz',
  25. headers=R_HEADERS)
  26. with open('regions.xml.gz', 'wb') as f:
  27. f.write(r.content)
  28. with gzip.open('regions.xml.gz', 'rb') as f_in:
  29. with open('regions.xml', 'wb') as f_out:
  30. f_out.write(f_in.read())
  31. def get_region_endos(regions):
  32. """Using the region dump, return a Dict with the number of endos for each region."""
  33. tree = et.parse('regions.xml')
  34. endo_dict = dict()
  35. root = tree.getroot()
  36. for region in root:
  37. if region[0].text in regions:
  38. region_name = canonicalize(region[0].text)
  39. endo_dict[region_name] = int(region[5].text) - 1
  40. return endo_dict
  41. class FounderlessNotify(commands.Cog):
  42. """Notifies when a region changes in founderless status."""
  43. def __init__(self, bot, *args, **kwargs):
  44. super().__init__(*args, **kwargs)
  45. self.bot = bot
  46. self.config = Config.get_conf(self, identifier=82732344, force_registration=True)
  47. default_global_settings = {'update_time': 0, 'previous_founderless': [], 'notify_channel': 0}
  48. self.config.register_global(**default_global_settings)
  49. self.bg_loop_task = asyncio.create_task(self.bg_loop())
  50. def cog_unload(self):
  51. if self.bg_loop_task:
  52. self.bg_loop_task.cancel()
  53. @commands.command()
  54. @checks.is_owner()
  55. async def is_task_running(self, ctx):
  56. """Check if the main loop is running."""
  57. if self.bg_loop_task:
  58. await ctx.send('True')
  59. else:
  60. await ctx.send('False')
  61. @commands.command()
  62. @checks.is_owner()
  63. async def start_task(self, ctx):
  64. """Start the main loop if it is not running."""
  65. if self.bg_loop_task:
  66. await ctx.send('Task is already running')
  67. else:
  68. self.bg_loop_task = asyncio.create_task(self.bg_loop())
  69. @commands.command()
  70. @checks.is_owner()
  71. async def force_check(self, ctx, update_type):
  72. """Force perform a check in the differences of founderless regions."""
  73. if update_type.lower() == 'major' or update_type.lower() == 'minor':
  74. await self.update_founderless(update_type)
  75. else:
  76. return
  77. async def update_founderless(self, update_type):
  78. """Check for the differences in the founderless regions and then output that to a channel."""
  79. channel_id = await self.config.notify_channel()
  80. channel = self.bot.get_channel(channel_id)
  81. await channel.send(f'Beginning {update_type} check...')
  82. new_founderless_regions = get_founderless_regions()
  83. previous_founderless_regions = await self.config.previous_founderless()
  84. # A region is now founderless if it is in the current list, but wasn't in the previous list
  85. now_founderless = list()
  86. for region in new_founderless_regions:
  87. if region in previous_founderless_regions:
  88. continue
  89. now_founderless.append(region)
  90. print(now_founderless)
  91. # Get the region endos for both lists
  92. now_founderless_endos = get_region_endos(now_founderless)
  93. # Create an ordered set with each region's name and delegate endorsement level
  94. out = list()
  95. for region in now_founderless:
  96. try:
  97. region_endos = now_founderless_endos[region]
  98. except KeyError:
  99. region_endos = -1
  100. out.append((region_endos, region))
  101. print(now_founderless_endos["the_north_pacific"])
  102. # Sort the output by delegate endos in descending order
  103. out.sort()
  104. out.reverse()
  105. # Prep output
  106. message_content = "The following regions are now **Founderless**:\n"
  107. for region in out:
  108. message_content += f"https://www.nationstates.net/region={region[1]} ({region[0]})\n"
  109. for page in pagify(message_content):
  110. await channel.send(page)
  111. await self.config.previous_founderless.set(new_founderless_regions)
  112. async def bg_loop(self):
  113. """Main background loop."""
  114. while True:
  115. # Only check once every 5 minutes
  116. await asyncio.sleep(300)
  117. current_time = datetime.datetime.utcnow()
  118. major_time = await self.config.update_time()
  119. minor_time = major_time + 12
  120. # Major Update
  121. if current_time.hour == (major_time + 2):
  122. download_region_dump()
  123. await self.update_founderless('major')
  124. await asyncio.sleep(3600)
  125. # Minor Update
  126. elif current_time.hour == (minor_time + 1):
  127. await self.update_founderless('minor')
  128. await asyncio.sleep(3600)
  129. @commands.command()
  130. @checks.is_owner()
  131. async def update_channel(self, ctx):
  132. """Set the notification channel."""
  133. await self.config.notify_channel.set(ctx.channel.id)
  134. await ctx.send(f'Founderless notify channel set to {ctx.channel.mention}')
  135. @commands.command()
  136. @checks.is_owner()
  137. async def update_time(self, ctx, update):
  138. """Set the update time."""
  139. update = int(update)
  140. await self.config.update_time.set(update)
  141. await ctx.send(f'Major update set to: {await self.config.update_time()}:00 UTC'
  142. f'\nMinor update set to: {await self.config.update_time() + 12}:00 UTC')
  143. @commands.command()
  144. @checks.is_owner()
  145. async def get_settings(self, ctx):
  146. """Get the current settings from config."""
  147. stored_channel = await self.config.notify_channel()
  148. major_time = await self.config.update_time()
  149. minor_time = major_time + 12
  150. founderless = await self.config.previous_founderless()
  151. channel = ctx.guild.get_channel(stored_channel)
  152. await ctx.send(f'Current Channel: {channel.mention}\nCurrent Major Update Time: {major_time}:00 UTC\n'
  153. f'Current Minor Update Time: {minor_time}:00 UTC\nCurrent # Founderless: {len(founderless)}')
  154. @commands.command()
  155. @checks.is_owner()
  156. async def force_update_founderless(self, ctx):
  157. """Manually update the founderless region list."""
  158. founderless_regions = get_founderless_regions()
  159. await self.config.previous_founderless.set(founderless_regions)
  160. await ctx.send(f'Manually updated founderless regions list. There are now '
  161. f'{len(founderless_regions)} founderless regions.')
  162. @commands.command()
  163. @checks.is_owner()
  164. async def clear_founderless(self, ctx):
  165. """Clear the cached founderless regions."""
  166. await self.config.previous_founderless.set([])
  167. await ctx.send("Cleared cached founderless regions.")
  168. @commands.command()
  169. @checks.is_owner()
  170. async def force_download_dump(self, ctx):
  171. """Manually download the region dump."""
  172. await ctx.send('Downloading region dump...')
  173. download_region_dump()
  174. await ctx.send('Region dump successfully updated.')