modbot/main.py

100 lines
2.8 KiB
Python
Raw Normal View History

2021-08-02 20:31:20 -05:00
#!/usr/bin/env python3
2021-08-02 21:13:12 -05:00
# modbot is copyright rndusr/randomuser 2021
# This file is part of modbot.
# modbot is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public
# License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any
# later version.
# modbot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero
# General Public License along with modbot. If not, see
# <https://www.gnu.org/licenses/>.
2021-08-02 20:31:20 -05:00
import asyncio
from importlib import import_module as mod_include
2021-08-02 21:36:04 -05:00
import os
import sys
2021-08-02 20:31:20 -05:00
from irctokens import build, Line
from ircrobots import Bot as BaseBot
from ircrobots import Server as BaseServer
from ircrobots import ConnectionParams
SERVERS = [
("beep", "beepboop.systems")
]
EVENTS = [
"cmd",
"privmsg",
"topic",
"mode",
"kick",
"join",
"part",
"invite",
]
PREFIX = '*'
class Server(BaseServer):
handlers = {i: [] for i in EVENTS}
states = {}
async def line_read(self, line: Line):
print(f"{self.name} < {line.format()}")
if line.command == "001":
await self.send(build("JOIN", ["#testchannel"]))
2021-08-02 20:43:02 -05:00
self.load_mod("default")
2021-08-02 20:31:20 -05:00
self.event_handle(line)
def load_mod(self, name):
mod = mod_include(name)
for i in EVENTS:
if hasattr(mod, i):
self.handlers[i].append(getattr(mod, i))
self.states[name] = None
def event_get(self, line: Line):
if line.command.lower() in EVENTS[2:]:
return line.command.lower()
elif line.command.lower() == "privmsg":
if line.params[1][0] == PREFIX: return "cmd"
else: return "privmsg"
else:
# something has gone horribly, horribly wrong
return None
def event_handle(self, line: Line):
event = self.event_get(line)
if event == None: return False
try:
for i in self.handlers[event]:
2021-08-02 20:43:02 -05:00
i(line, self)
2021-08-02 20:31:20 -05:00
except IndexError: return False
return True
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():
bot = Bot()
for name, host in SERVERS:
params = ConnectionParams("BitBotNewTest", host, 6667, False)
await bot.add_server(name, params)
await bot.run()
if __name__ == "__main__":
2021-08-02 21:36:04 -05:00
os.chdir("mods")
sys.path.append(os.getcwd())
2021-08-02 20:31:20 -05:00
asyncio.run(main())