2023-04-07 10:01:57 +10:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from discord.utils import setup_logging
|
2023-04-08 09:50:32 +10:00
|
|
|
from typing import Optional
|
2023-04-07 10:01:57 +10:00
|
|
|
import argparse
|
|
|
|
import discord
|
|
|
|
import logging
|
2023-04-08 16:53:23 +10:00
|
|
|
import os.path
|
2023-04-08 09:50:32 +10:00
|
|
|
import random
|
2023-04-07 10:01:57 +10:00
|
|
|
import re
|
2023-04-07 10:13:11 +10:00
|
|
|
import shutil
|
2023-04-07 10:01:57 +10:00
|
|
|
import subprocess as sp
|
|
|
|
|
|
|
|
discord.utils.setup_logging()
|
2023-04-08 16:53:23 +10:00
|
|
|
_log = logging.getLogger(__name__)
|
2023-04-07 10:01:57 +10:00
|
|
|
|
|
|
|
_GAMES = "games.txt"
|
|
|
|
_DB = "holy-heck2.db"
|
|
|
|
_DB_DEST = f"/var/lib/grafana/{_DB}"
|
|
|
|
|
|
|
|
|
|
|
|
def _write_game(content: str):
|
|
|
|
try:
|
|
|
|
with open(_GAMES, "a") as f:
|
|
|
|
f.write(content)
|
|
|
|
f.write("\n")
|
|
|
|
except:
|
|
|
|
_log.exception(f"failed writing game {content}")
|
|
|
|
|
|
|
|
|
|
|
|
def _update_db():
|
|
|
|
try:
|
|
|
|
games = []
|
|
|
|
with open(_GAMES) as f:
|
|
|
|
for line in f:
|
|
|
|
games.append(line.strip())
|
|
|
|
sp.run(["./index.py", "-o", _DB] + games)
|
2023-04-07 10:13:11 +10:00
|
|
|
shutil.move(_DB, _DB_DEST)
|
2023-04-07 10:13:45 +10:00
|
|
|
_log.info("updated db")
|
2023-04-07 10:01:57 +10:00
|
|
|
except:
|
|
|
|
_log.exception(f"failed updating db")
|
|
|
|
|
|
|
|
|
|
|
|
class BotClient(discord.Client):
|
2023-04-08 09:50:32 +10:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-04-08 16:53:23 +10:00
|
|
|
intents: discord.Intents,
|
2023-04-08 09:50:32 +10:00
|
|
|
leaguefacts: Optional[list[str]] = None,
|
|
|
|
):
|
2023-04-08 16:53:23 +10:00
|
|
|
super().__init__(intents=intents)
|
2023-04-08 09:50:32 +10:00
|
|
|
self._leaguefacts = leaguefacts or []
|
|
|
|
|
2023-04-07 10:01:57 +10:00
|
|
|
async def on_ready(self):
|
|
|
|
_log.info(f"ready as {self.user}")
|
|
|
|
|
|
|
|
async def on_message(self, message: discord.Message):
|
|
|
|
content = message.content
|
2023-04-08 09:50:32 +10:00
|
|
|
if self.is_replay(message):
|
|
|
|
await self.on_replay(message)
|
|
|
|
elif self.is_leaguefact(message):
|
|
|
|
await self.on_leaguefact(message)
|
|
|
|
|
2023-04-08 16:53:23 +10:00
|
|
|
def is_replay(self, message: discord.Message) -> bool:
|
2023-04-08 09:50:32 +10:00
|
|
|
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()
|
|
|
|
|
2023-04-08 16:53:23 +10:00
|
|
|
def is_leaguefact(self, message: discord.Message) -> bool:
|
2023-04-08 16:54:47 +10:00
|
|
|
return message.content.lower() == "leaguefacts"
|
2023-04-08 09:50:32 +10:00
|
|
|
|
|
|
|
async def on_leaguefact(self, message: discord.Message):
|
|
|
|
fact = self._select_leaguefact()
|
|
|
|
if fact:
|
2023-04-08 16:53:23 +10:00
|
|
|
await message.channel.send(f"Did you know? {fact}")
|
|
|
|
else:
|
|
|
|
await message.channel.send("There are no league facts.")
|
2023-04-08 09:50:32 +10:00
|
|
|
|
|
|
|
def _select_leaguefact(self) -> Optional[str]:
|
|
|
|
if not self._leaguefacts:
|
|
|
|
return None
|
|
|
|
random.seed()
|
|
|
|
return random.choice(self._leaguefacts)
|
2023-04-07 10:01:57 +10:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
"-t",
|
|
|
|
"--token-file",
|
|
|
|
metavar="FILE",
|
|
|
|
default="token",
|
|
|
|
help="file containing Discord API token",
|
|
|
|
)
|
2023-04-08 09:50:32 +10:00
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--facts",
|
|
|
|
metavar="FILE",
|
|
|
|
default="facts.txt",
|
|
|
|
help="file containing leagefacts",
|
|
|
|
)
|
2023-04-07 10:01:57 +10:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2023-04-08 09:50:32 +10:00
|
|
|
facts = []
|
2023-04-08 16:53:23 +10:00
|
|
|
if os.path.exists(args.facts):
|
|
|
|
with open(args.facts) as f:
|
|
|
|
for line in f:
|
|
|
|
facts.append(line)
|
2023-04-08 09:50:32 +10:00
|
|
|
|
2023-04-07 10:01:57 +10:00
|
|
|
intents = discord.Intents.default()
|
|
|
|
intents.message_content = True
|
2023-04-08 09:50:32 +10:00
|
|
|
client = BotClient(leaguefacts=facts, intents=intents)
|
|
|
|
|
2023-04-07 10:01:57 +10:00
|
|
|
with open(args.token_file) as f:
|
|
|
|
token = f.read().strip()
|
|
|
|
client.run(token, log_handler=None)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|