Initial commit

This commit is contained in:
Ultracoolguy 2020-11-15 10:53:28 -04:00
commit 196c455817
3 changed files with 102 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
playerlist.txt

5
README.md Normal file
View File

@ -0,0 +1,5 @@
## matchmaking-bot
A simple Python bot for IRC channels that want matchmaking capabilities.
Idk what else to say :/

96
bot.py Executable file
View File

@ -0,0 +1,96 @@
#!/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 = [ ("freenode", "irc.freenode.net") ]
match_players = set()
botnick = "matchmaking-bot"
chan = "#among-sus"
def update_file():
file = open("playerlist.txt", "w")
file.seek(0)
file.truncate()
for player in match_players:
file.write(player + '\n')
def read_file():
file = open("playerlist.txt", "r")
for player in file:
match_players.add(player[:-1]) #Remove newline
class Server(BaseServer):
async def line_read(self, line: Line):
print(f"{self.name} < {line.format()}")
if line.command == "001":
print(f"connected to {self.isupport.network}")
await self.send(build("JOIN", [chan]))
elif "PRIVMSG" in line.format() and chan in line.format():
user = line.source.split('!')[0]
if line.params[1].startswith(".matchmake"):
channel = self.channels[line.params[0]]
ping = ""
for player in match_players:
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"):
channel = self.channels[line.params[0]]
if user in match_players:
await self.send(build("PRIVMSG", [chan, "ERROR: player already in list."]))
else:
match_players.add(user)
update_file()
await self.send(build("PRIVMSG", [chan, "Added to the match list."]))
elif line.params[1].startswith(".matchdel"):
channel = self.channels[line.params[0]]
user = line.source.split('!')[0]
if user not in match_players:
await self.send(build("PRIVMSG", [chan, "ERROR: player isn't in list."]))
else:
match_players.remove(user)
update_file()
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:"])) #The ordering of this isn't actually what gets sent. Fix later
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:
params = ConnectionParams(botnick, host, 6697, True)
await bot.add_server(name, params)
await bot.run()
if __name__ == "__main__":
asyncio.run(main())