Compare commits
No commits in common. "c3df316aa47e1a2234d5af5855d109d3739d8015" and "145f220a071266b8cd56e76e2b4c94c18e9524ad" have entirely different histories.
c3df316aa4
...
145f220a07
3
.gitignore
vendored
3
.gitignore
vendored
@ -8,6 +8,5 @@ tmp/
|
|||||||
build
|
build
|
||||||
htmlcov
|
htmlcov
|
||||||
|
|
||||||
*.db
|
data.db
|
||||||
cache/
|
cache/
|
||||||
token
|
|
||||||
|
@ -11,8 +11,8 @@ fuck.
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ ./index.py -h
|
$ ./main.py -h
|
||||||
usage: hhirlstats [-h] [-v] [-C] [-t FILE] [-o FILE] replay [replay ...]
|
usage: hhirlstats [-h] [-v] [-C] [-o FILE] replay [replay ...]
|
||||||
|
|
||||||
extracts stats from a Showdown replay
|
extracts stats from a Showdown replay
|
||||||
|
|
||||||
@ -23,8 +23,6 @@ options:
|
|||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-v, --verbose add debugging info (default: None)
|
-v, --verbose add debugging info (default: None)
|
||||||
-C, --no-cache fetch replays instead of using cache (default: False)
|
-C, --no-cache fetch replays instead of using cache (default: False)
|
||||||
-t FILE, --teams FILE
|
|
||||||
JSON file defining players to teams (default: teams.json)
|
|
||||||
-o FILE, --output FILE
|
-o FILE, --output FILE
|
||||||
output data file (default: data.db)
|
output data file (default: data.db)
|
||||||
```
|
```
|
||||||
|
73
bot.py
73
bot.py
@ -1,73 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
from discord.utils import setup_logging
|
|
||||||
import argparse
|
|
||||||
import discord
|
|
||||||
import logging
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import subprocess as sp
|
|
||||||
|
|
||||||
discord.utils.setup_logging()
|
|
||||||
_log = logging.getLogger("statbot")
|
|
||||||
|
|
||||||
_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)
|
|
||||||
os.rename(_DB, _DB_DEST)
|
|
||||||
except:
|
|
||||||
_log.exception(f"failed updating db")
|
|
||||||
|
|
||||||
|
|
||||||
class BotClient(discord.Client):
|
|
||||||
async def on_ready(self):
|
|
||||||
_log.info(f"ready as {self.user}")
|
|
||||||
|
|
||||||
async def on_message(self, message: discord.Message):
|
|
||||||
content = message.content
|
|
||||||
if re.match("https://replay.pokemonshowdown.com/dl-.*", content):
|
|
||||||
_log.info(f"Recognised {content} as a League game")
|
|
||||||
_write_game(content)
|
|
||||||
_update_db()
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument(
|
|
||||||
"-t",
|
|
||||||
"--token-file",
|
|
||||||
metavar="FILE",
|
|
||||||
default="token",
|
|
||||||
help="file containing Discord API token",
|
|
||||||
)
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
intents = discord.Intents.default()
|
|
||||||
intents.message_content = True
|
|
||||||
client = BotClient(intents=intents)
|
|
||||||
with open(args.token_file) as f:
|
|
||||||
token = f.read().strip()
|
|
||||||
client.run(token, log_handler=None)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
@ -13,7 +13,6 @@
|
|||||||
buildInputs =
|
buildInputs =
|
||||||
let
|
let
|
||||||
python = pkgs.python3.withPackages (ps: [
|
python = pkgs.python3.withPackages (ps: [
|
||||||
ps.discordpy
|
|
||||||
ps.mypy
|
ps.mypy
|
||||||
ps.requests
|
ps.requests
|
||||||
ps.types-requests
|
ps.types-requests
|
||||||
|
@ -493,7 +493,7 @@ def fetch(replay: str, cache: bool = True) -> Replay:
|
|||||||
return Replay(**data) # type: ignore
|
return Replay(**data) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main(args):
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog=APP,
|
prog=APP,
|
||||||
description="extracts stats from a Showdown replay",
|
description="extracts stats from a Showdown replay",
|
||||||
@ -524,7 +524,7 @@ def main():
|
|||||||
)
|
)
|
||||||
parser.add_argument("replay", nargs="+", help="replay ID or URL")
|
parser.add_argument("replay", nargs="+", help="replay ID or URL")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args(args)
|
||||||
if args.verbose and args.verbose > 1:
|
if args.verbose and args.verbose > 1:
|
||||||
LOG.setLevel(logging.TRACE)
|
LOG.setLevel(logging.TRACE)
|
||||||
elif args.verbose:
|
elif args.verbose:
|
||||||
@ -570,4 +570,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main(sys.argv[1:])
|
Loading…
Reference in New Issue
Block a user