summaryrefslogtreecommitdiff
path: root/LibreGameMatch.py
blob: 66a8b136284695780d24cc79e2eb538e496031a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python3

# Bot to keep track of people who play a specific game
# Public domain

import miniirc
import sys
import json
import time
import os
from pprint import pprint

with open("lgmdata.json", "r") as f:
    data = json.load(f)

try:
    pprint(data["games"])
except KeyError:
    data["games"] = {}


assert miniirc.ver >= (1, 8, 1), "This bot requires miniirc >= v1.8.1."

nick = "LibreGameMatch"
ident = "game"
realname = "Player tracker and pinger for LibreGameNight"
identity = None
# identity = '<username> <password>'
debug = True
channels = ["#libregaming-matchmaking", "#botwar"]
prefix = "."

ip = "irc.libera.chat"
port = 6697

print("Welcome to {}!".format(nick), file=sys.stderr)
irc = miniirc.IRC(
    ip,
    port,
    nick,
    channels,
    ident=ident,
    realname=realname,
    ns_identity=identity,
    debug=debug and (lambda line: print(repr(line))),
    auto_connect=False,
)

@irc.Handler("PRIVMSG", colon=False)
def handle_privmsg(irc, hostmask, args):
    channel = args[0]
    if not channel.startswith("#"):
        irc.notice(hostmask[0], "Error: This bot does not work with private messages.  You MUST use this bot in a channel.")
        return
    text = args[-1].split(" ")
    cmd = text[0].lower()
    if not cmd.startswith(prefix):
        return
    cmd = cmd[len(prefix) :]
    if cmd == "help":
        irc.msg(channel, f"{hostmask[0]}: \x02{nick}\x02 is a utility to track players games, and when requested by a member thereof, highlights all players of the said game.")
        irc.msg(channel, f"{hostmask[0]}: Available commands: help, join, match, players, and games.  All commands MUST be called in a channel with '.' (ASCII dot) as its prefix.")
    elif cmd == "join":
        try:
            data['games'][text[1].title()][hostmask[0]] = True
        except KeyError:
            data['games'][text[1].title()] = {hostmask[0]: True} # just use a dict... solves the multiple instances at the same time
            irc.msg(channel, f"{hostmask[0]}: You have created and joined the {text[1].title()} player list.")
        else:
            irc.msg(channel, f"{hostmask[0]}: You have joined the {text[1].title()} player list.")
        print(json.dumps(data, indent="\t", sort_keys=True))
    elif cmd == "part":
        try:
            irc.msg(channel, f"{hostmask[0]}: You have parted the {text[1].title()} player list.")
        except KeyError:
            irc.msg(channel, f"{hostmask[0]}: You are not in the {text[1].title()} player list, or the player list does not exist.")
        finally:
            del data["games"][text[1].title()][hostmask[0]]
    elif cmd == "match":
        irc.msg(channel, f"{', '.join([p for p in data['games'][text[1].title()] if p != hostmask[0]])}: Anyone ready for {text[1].title()}?  {hostmask[0]} has called a game.")
    elif cmd == "players":
        irc.notice(hostmask[0], f"[{channel}] These are the players in {text[1].title()}: {', '.join([p for p in data['games'][text[1].title()]])}.")
    elif cmd == "games":
        irc.msg(channel, f"{hostmask[0]}: The following games are available: {', '.join([g for g in data['games']])}.")
    else:
        if not (cmd == "" or "." in cmd):
            irc.notice(hostmask[0], f"[{channel}] Invalid {nick} command: {cmd}.  Use the .help command for more information.")


if __name__ == "__main__":
    irc.connect()
    try:
        irc.wait_until_disconnected()
        #while True:
        #    time.sleep(999999999999999999999999) # much better
    except KeyboardInterrupt:
        with open("lgmdata.tmp.json", "w") as f:
            json.dump(data, f, indent="\t", sort_keys=True)
        os.replace("lgmdata.tmp.json", "lgmdata.json")
        irc.disconnect()