leaguefacts
This commit is contained in:
		
							
								
								
									
										56
									
								
								bot.py
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								bot.py
									
									
									
									
									
								
							| @@ -1,9 +1,11 @@ | |||||||
| #!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||||
|  |  | ||||||
| from discord.utils import setup_logging | from discord.utils import setup_logging | ||||||
|  | from typing import Optional | ||||||
| import argparse | import argparse | ||||||
| import discord | import discord | ||||||
| import logging | import logging | ||||||
|  | import random | ||||||
| import re | import re | ||||||
| import shutil | import shutil | ||||||
| import subprocess as sp | import subprocess as sp | ||||||
| @@ -39,16 +41,49 @@ def _update_db(): | |||||||
|  |  | ||||||
|  |  | ||||||
| class BotClient(discord.Client): | class BotClient(discord.Client): | ||||||
|  |     def __init__( | ||||||
|  |         self, | ||||||
|  |         leaguefacts: Optional[list[str]] = None, | ||||||
|  |         intents: discord.Intents = discord.Intents.default(), | ||||||
|  |         **options, | ||||||
|  |     ): | ||||||
|  |         super(discord.Client, self).__init__(intents, options) | ||||||
|  |         self._leaguefacts = leaguefacts or [] | ||||||
|  |  | ||||||
|     async def on_ready(self): |     async def on_ready(self): | ||||||
|         _log.info(f"ready as {self.user}") |         _log.info(f"ready as {self.user}") | ||||||
|  |  | ||||||
|     async def on_message(self, message: discord.Message): |     async def on_message(self, message: discord.Message): | ||||||
|         content = message.content |         content = message.content | ||||||
|         if re.match("https://replay.pokemonshowdown.com/dl-.*", content): |         if self.is_replay(message): | ||||||
|             _log.info(f"Recognised {content} as a League game") |             await self.on_replay(message) | ||||||
|             _write_game(content) |         elif self.is_leaguefact(message): | ||||||
|  |             await self.on_leaguefact(message) | ||||||
|  |  | ||||||
|  |     async def is_replay(self, message: discord.Message) -> bool: | ||||||
|  |         if re.match("https://replay.pokemonshowdown.com/dl-.*", message.content): | ||||||
|  |             return True | ||||||
|  |         return False | ||||||
|  |  | ||||||
|  |     async def on_replay(self, message: discord.Message): | ||||||
|  |         _log.info(f"Recognised {message.content} as a League game") | ||||||
|  |         _write_game(message.content) | ||||||
|         _update_db() |         _update_db() | ||||||
|  |  | ||||||
|  |     async def is_leaguefact(self, message: discord.Message) -> bool: | ||||||
|  |         return message.content.lower() == "notleaguefact" | ||||||
|  |  | ||||||
|  |     async def on_leaguefact(self, message: discord.Message): | ||||||
|  |         fact = self._select_leaguefact() | ||||||
|  |         if fact: | ||||||
|  |             message.channel.send(fact) | ||||||
|  |  | ||||||
|  |     def _select_leaguefact(self) -> Optional[str]: | ||||||
|  |         if not self._leaguefacts: | ||||||
|  |             return None | ||||||
|  |         random.seed() | ||||||
|  |         return random.choice(self._leaguefacts) | ||||||
|  |  | ||||||
|  |  | ||||||
| def main(): | def main(): | ||||||
|     parser = argparse.ArgumentParser() |     parser = argparse.ArgumentParser() | ||||||
| @@ -59,12 +94,25 @@ def main(): | |||||||
|         default="token", |         default="token", | ||||||
|         help="file containing Discord API token", |         help="file containing Discord API token", | ||||||
|     ) |     ) | ||||||
|  |     parser.add_argument( | ||||||
|  |         "-f", | ||||||
|  |         "--facts", | ||||||
|  |         metavar="FILE", | ||||||
|  |         default="facts.txt", | ||||||
|  |         help="file containing leagefacts", | ||||||
|  |     ) | ||||||
|  |  | ||||||
|     args = parser.parse_args() |     args = parser.parse_args() | ||||||
|  |  | ||||||
|  |     facts = [] | ||||||
|  |     with open(args.facts) as f: | ||||||
|  |         for line in f: | ||||||
|  |             facts.append(line) | ||||||
|  |  | ||||||
|     intents = discord.Intents.default() |     intents = discord.Intents.default() | ||||||
|     intents.message_content = True |     intents.message_content = True | ||||||
|     client = BotClient(intents=intents) |     client = BotClient(leaguefacts=facts, intents=intents) | ||||||
|  |  | ||||||
|     with open(args.token_file) as f: |     with open(args.token_file) as f: | ||||||
|         token = f.read().strip() |         token = f.read().strip() | ||||||
|         client.run(token, log_handler=None) |         client.run(token, log_handler=None) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user