Read configuration from bot.ini from now on
This makes the bot channel agnostic and allows for someone to use it without modifying code. Add bot.in.example for an example and update README.md to reflect changes.
This commit is contained in:
parent
c0a4d8b7b3
commit
f2c35a8c77
17
README.md
17
README.md
|
@ -2,4 +2,19 @@
|
|||
|
||||
A simple Python bot for IRC channels that want matchmaking capabilities.
|
||||
|
||||
Idk what else to say :/
|
||||
## Dependencies
|
||||
|
||||
You just need configparser and irctokens. You can install them doing:
|
||||
```
|
||||
pip install configparser irctokens
|
||||
```
|
||||
You can add `--user` if you don't want to install them system wide.
|
||||
|
||||
## Installation
|
||||
|
||||
Just `./bot.py` lol.
|
||||
|
||||
## Configuration
|
||||
|
||||
The bot uses a bot.ini for configurating the servers for connecting. Look at
|
||||
bot.ini.example for an example(you know, like the extension says).
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
[host]
|
||||
server = chat.freenode.invalid
|
||||
nickname = matchmaking-bot
|
||||
channels = #matchmaking-test
|
75
bot.py
75
bot.py
|
@ -1,16 +1,12 @@
|
|||
#!/bin/env python3
|
||||
import asyncio
|
||||
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 = (dict([("freenode", "irc.freenode.net"),
|
||||
("tildeverse", "irc.tilde.chat")]))
|
||||
|
||||
match_players = {"freenode": set(), "tildeverse": set()}
|
||||
|
||||
botnick = "matchmaking-bot"
|
||||
servers = []
|
||||
match_players = {}
|
||||
|
||||
def update_file(name):
|
||||
file = open(f"playerlist-{name}.txt", "w")
|
||||
|
@ -19,23 +15,22 @@ def update_file(name):
|
|||
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()
|
||||
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 self.params.autojoin[0] in line.params[0]:
|
||||
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]:
|
||||
for player in match_players[self.name + '-' + chan]:
|
||||
pfold = self.casefold(player)
|
||||
if pfold in channel.users:
|
||||
ping += f"{channel.users[pfold].nickname} "
|
||||
|
@ -45,20 +40,20 @@ class Server(BaseServer):
|
|||
|
||||
elif line.params[1].startswith(".matchadd"):
|
||||
user = line.source.split('!')[0]
|
||||
if user in match_players[self.name]:
|
||||
if user in match_players[self.name + '-' + chan]:
|
||||
await self.send(build("PRIVMSG", [chan, "ERROR: player already in list."]))
|
||||
else:
|
||||
match_players[self.name].add(user)
|
||||
update_file(self.name)
|
||||
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]:
|
||||
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].remove(user)
|
||||
update_file(self.name)
|
||||
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"):
|
||||
|
@ -76,14 +71,38 @@ class Bot(BaseBot):
|
|||
return Server(self, name)
|
||||
|
||||
async def main():
|
||||
read_file()
|
||||
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}.")
|
||||
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 name, host in SERVERS.items():
|
||||
params = ConnectionParams(botnick, host, 6697, True)
|
||||
if name == "freenode":
|
||||
params.autojoin = ["#among-sus"]
|
||||
elif name == "tildeverse":
|
||||
params.autojoin = ["#sus"]
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue