#!/bin/env python3 import asyncio, configparser from irctokens import build, Line from ircrobots import Bot as BaseBot from ircrobots import Server as BaseServer from ircrobots import ConnectionParams servers = [] match_players = {} 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(name): 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 any(channel in line.params[0] for channel in self.params.autojoin): 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 + '-' + chan]: 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 + '-' + chan]: await self.send(build("PRIVMSG", [chan, "ERROR: player already in list."])) else: match_players[self.name + '-' + chan].add(user) update_file(self.name + '-' + chan) 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 + '-' + chan]: await self.send(build("PRIVMSG", [chan, "ERROR: player isn't in list."])) else: match_players[self.name + '-' + chan].remove(user) update_file(self.name + '-' + chan) await self.send(build("PRIVMSG", [chan, "Removed from match list."])) elif line.params[1].startswith(".help"): await self.send(build("PRIVMSG", [chan, " .matchadd: Add to list; .matchdel: Remove from list; .matchmake: Ping everyone on list"])) 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(): config = configparser.ConfigParser() with open('bot.ini', 'r') as configfile: config.read_file(configfile) for section in config.sections(): if not config[section]['server']: print(f"ERROR: The section {section} on your bot.ini doesn't have a 'server' parameter.") exit(1) elif not config[section]['nickname']: print(f"ERROR: The section {section} has no nickname defined.") exit(1) elif not config[section]['channels']: print(f"ERROR: You didn't define any channels in section {section}.") exit(1) server = config[section]['server'] nickname = config[section]['nickname'] channels = set(config[section]['channels'].split(' ')) # servers.update([(section, {'server':server, 'nickname':nickname, 'channels':channels})]) servers.append({'name':section, 'opts':{'server':server, 'nickname':nickname, 'channels':channels}}) for channel in channels: match_players.update([(section + '-' + channel, set())]) # read_file() bot = Bot() for entry in servers: name = entry['name'] server = entry['opts']['server'] botnick = entry['opts']['nickname'] channels = entry['opts']['channels'] params = ConnectionParams(botnick, server, 6697, True) for channel in channels: params.autojoin.append(channel) read_file(name + '-' + channel) await bot.add_server(name, params) await bot.run() if __name__ == "__main__": asyncio.run(main())