matchmaking-bot/bot.py

94 lines
3.5 KiB
Python
Executable File

#!/bin/env python3
import asyncio
from irctokens import build, Line
from ircrobots import Bot as BaseBot
from ircrobots import Server as BaseServer
from ircrobots import ConnectionParams
SERVERS = (dict([("freenode", "irc.freenode.net"),
("tildeverse", "irc.tilde.chat")]))
# match_players = set()
match_players = {"freenode": set(), "tildeverse": set()}
botnick = "matchmaking-bot"
def update_file(name):
file = open(f"playerlist-{name}.txt", "w")
file.seek(0)
file.truncate()
for player in match_players[name]:
file.write(player + '\n')
def read_file():
for name in SERVERS:
file = open(f"playerlist-{name}.txt", "r")
for player in file:
match_players[name].add(player[:-1]) #Remove newline
file.close()
class Server(BaseServer):
async def line_read(self, line: Line):
print(f"{self.name} < {line.format()}")
if "PRIVMSG" in line.command and self.params.autojoin[0] in line.params[0]:
chan = self.channels[line.params[0]].name
if line.params[1].startswith(".matchmake"):
channel = self.channels[line.params[0]]
ping = ""
for player in match_players[self.name]:
pfold = self.casefold(player)
if pfold in channel.users:
ping += f"{channel.users[pfold].nickname} "
await self.send(build("PRIVMSG", [chan, f"Pinging users: {ping}"]))
elif line.params[1].startswith(".matchadd"):
user = line.source.split('!')[0]
if user in match_players[self.name]:
await self.send(build("PRIVMSG", [chan, "ERROR: player already in list."]))
else:
match_players[self.name].add(user)
update_file(self.name)
await self.send(build("PRIVMSG", [chan, "Added to the match list."]))
elif line.params[1].startswith(".matchdel"):
user = line.source.split('!')[0]
if user not in match_players[self.name]:
await self.send(build("PRIVMSG", [chan, "ERROR: player isn't in list."]))
else:
match_players[self.name].remove(user)
update_file(self.name)
await self.send(build("PRIVMSG", [chan, "Removed from match list."]))
elif line.params[1].startswith(".help"):
await self.send(build("PRIVMSG", [chan, "Matchmaking bot commands:"]))
await self.send(build("PRIVMSG", [chan, " .matchadd: Add yourself to the match list"]))
await self.send(build("PRIVMSG", [chan, " .matchdel: Remove yourself from the match list"]))
await self.send(build("PRIVMSG", [chan, " .matchmake: Ping everyone on match list"]))
await self.send(build("PRIVMSG", [chan, " .help: Display this message"]))
async def line_send(self, line: Line):
print(f"{self.name} > {line.format()}")
class Bot(BaseBot):
def create_server(self, name: str):
return Server(self, name)
async def main():
read_file()
bot = Bot()
for name, host in SERVERS.items():
params = ConnectionParams(botnick, host, 6697, True)
if name == "freenode":
params.autojoin = ["#among-sus"]
elif name == "tildeverse":
params.autojoin = ["#sus"]
await bot.add_server(name, params)
await bot.run()
if __name__ == "__main__":
asyncio.run(main())