Formatting

This commit is contained in:
Firepup Sixfifty 2023-11-07 20:30:35 -06:00
parent 1242aed9b3
commit d3f4e88934
Signed by: Firepup650
GPG key ID: 7C92E2ABBBFAB9BA
3 changed files with 41 additions and 18 deletions

37
bot.py
View file

@ -10,22 +10,28 @@ from time import sleep
from importlib import reload from importlib import reload
import random as r import random as r
def mfind(message: str, find: list, usePrefix: bool = True) -> bool: def mfind(message: str, find: list, usePrefix: bool = True) -> bool:
if usePrefix: if usePrefix:
return any(message[: len(match) + 1] == conf.prefix + match for match in find) return any(message[: len(match) + 1] == conf.prefix + match for match in find)
else: else:
return any(message[: len(match)] == match for match in find) return any(message[: len(match)] == match for match in find)
class bot: class bot:
def __init__(self, server: str): def __init__(self, server: str):
self.gmode = False self.gmode = False
self.server = server self.server = server
self.nicklen = 30 self.nicklen = 30
self.address = conf.servers[server]["address"] self.address = conf.servers[server]["address"]
self.port = conf.servers[server]["port"] if "port" in conf.servers[server] else 6667 self.port = (
conf.servers[server]["port"] if "port" in conf.servers[server] else 6667
)
self.channels = conf.servers[server]["channels"] self.channels = conf.servers[server]["channels"]
self.interval = ( self.interval = (
conf.servers[server]["interval"] if "interval" in conf.servers[server] else 50 conf.servers[server]["interval"]
if "interval" in conf.servers[server]
else 50
) )
self.__version__ = conf.__version__ self.__version__ = conf.__version__
self.nick = "FireBot" self.nick = "FireBot"
@ -148,7 +154,7 @@ class bot:
sender = msg.split("!", 1)[0][1:] sender = msg.split("!", 1)[0][1:]
message = msg.split("PRIVMSG", 1)[1].split(":", 1)[1].strip() message = msg.split("PRIVMSG", 1)[1].split(":", 1)[1].strip()
kind = msg.split("\x01")[1].split(" ", 1)[0] kind = msg.split("\x01")[1].split(" ", 1)[0]
self.log(f"Responding to CTCP \"{kind}\" from {sender}") self.log(f'Responding to CTCP "{kind}" from {sender}')
if kind == "VERSION": if kind == "VERSION":
self.notice( self.notice(
f"\x01VERSION FireBot {conf.__version__} (https://git.amcforum.wiki/Firepup650/fire-ircbot)\x01", f"\x01VERSION FireBot {conf.__version__} (https://git.amcforum.wiki/Firepup650/fire-ircbot)\x01",
@ -174,7 +180,7 @@ class bot:
"\x01CLIENTINFO ACTION VERSION USERINFO SOURCE FINGER\x01", sender, True "\x01CLIENTINFO ACTION VERSION USERINFO SOURCE FINGER\x01", sender, True
) )
return True return True
self.log(f"Unknown CTCP \"{kind}\"", "WARN") self.log(f'Unknown CTCP "{kind}"', "WARN")
return False return False
def msg(self, msg: str, target: str) -> None: def msg(self, msg: str, target: str) -> None:
@ -203,7 +209,9 @@ class bot:
self.log("Starting connection..") self.log("Starting connection..")
self.connect() self.connect()
if "pass" in conf.servers[self.server]: if "pass" in conf.servers[self.server]:
self.msg(f"IDENTIFY FireBot {conf.servers[self.server]['pass']}", "NickServ") self.msg(
f"IDENTIFY FireBot {conf.servers[self.server]['pass']}", "NickServ"
)
sleep(0.5) sleep(0.5)
for chan in self.channels: for chan in self.channels:
self.join(chan, "null", False) self.join(chan, "null", False)
@ -266,14 +274,22 @@ class bot:
for cmd in cmds.data: for cmd in cmds.data:
triggers = [cmd] triggers = [cmd]
triggers.extend(cmds.data[cmd]["aliases"]) triggers.extend(cmds.data[cmd]["aliases"])
triggers = list(call.replace("$BOTNICK", self.nick.lower()) for call in triggers) triggers = list(
call.replace("$BOTNICK", self.nick.lower())
for call in triggers
)
if mfind( if mfind(
message.lower(), message.lower(),
triggers, triggers,
cmds.data[cmd]["prefix"], cmds.data[cmd]["prefix"],
): ):
if ("admin" in cmds.data[cmd] and cmds.data[cmd]["admin"]) and name not in self.adminnames: if (
self.msg(f"Sorry {name}, you don't have permission to use {cmd.strip()}.", chan) "admin" in cmds.data[cmd] and cmds.data[cmd]["admin"]
) and name not in self.adminnames:
self.msg(
f"Sorry {name}, you don't have permission to use {cmd.strip()}.",
chan,
)
else: else:
cmds.call[cmd](self, chan, name, message) cmds.call[cmd](self, chan, name, message)
handled = True handled = True
@ -296,7 +312,10 @@ class bot:
self.__version__ = conf.__version__ self.__version__ = conf.__version__
self.msg("Reloaded config and commands", chan) self.msg("Reloaded config and commands", chan)
else: else:
self.msg(f"Sorry {name}, you don't have permission to use reload.", chan) self.msg(
f"Sorry {name}, you don't have permission to use reload.",
chan,
)
handled = True handled = True
if not handled and len(message.split("\x01")) == 3: if not handled and len(message.split("\x01")) == 3:
if not self.CTCP(message, name): if not self.CTCP(message, name):

View file

@ -2,6 +2,7 @@ from subprocess import run, PIPE
from config import npbase, su, decode_escapes from config import npbase, su, decode_escapes
import random as r import random as r
def goat(bot, chan: str, name: str, message: str) -> None: def goat(bot, chan: str, name: str, message: str) -> None:
bot.log("GOAT DETECTED") bot.log("GOAT DETECTED")
bot.msg("Hello Goat", chan) bot.msg("Hello Goat", chan)
@ -96,8 +97,8 @@ def quote(bot, chan: str, name: str, message: str) -> None:
def join(bot, chan: str, name: str, message: str) -> None: def join(bot, chan: str, name: str, message: str) -> None:
newchan = message.split(" ", 1)[1].strip() newchan = message.split(" ", 1)[1].strip()
bot.join(newchan, chan) bot.join(newchan, chan)
def eball(bot, chan: str, name: str, message: str) -> None: def eball(bot, chan: str, name: str, message: str) -> None:
@ -112,10 +113,10 @@ def eball(bot, chan: str, name: str, message: str) -> None:
def debug(bot, chan: str, name: str, message: str) -> None: def debug(bot, chan: str, name: str, message: str) -> None:
bot.msg(f"[DEBUG] VERSION={bot.__version__}", chan) bot.msg(f"[DEBUG] VERSION={bot.__version__}", chan)
bot.msg(f"[DEBUG] NICKLEN={bot.nicklen}", chan) bot.msg(f"[DEBUG] NICKLEN={bot.nicklen}", chan)
bot.msg(f"[DEBUG] ADMINS={bot.adminnames}", chan) bot.msg(f"[DEBUG] ADMINS={bot.adminnames}", chan)
bot.msg(f"[DEBUG] CHANNELS={bot.channels}", chan) bot.msg(f"[DEBUG] CHANNELS={bot.channels}", chan)
def raw(bot, chan: str, name: str, message: str) -> None: def raw(bot, chan: str, name: str, message: str) -> None:
@ -123,8 +124,8 @@ def raw(bot, chan: str, name: str, message: str) -> None:
def reboot(bot, chan: str, name: str, message: str) -> None: def reboot(bot, chan: str, name: str, message: str) -> None:
bot.send("QUIT :Rebooting\n") bot.send("QUIT :Rebooting\n")
bot.exit("Reboot") bot.exit("Reboot")
def sudo(bot, chan: str, name: str, message: str) -> None: def sudo(bot, chan: str, name: str, message: str) -> None:
@ -149,7 +150,7 @@ data = {
"!botlist": {"prefix": False, "aliases": []}, "!botlist": {"prefix": False, "aliases": []},
"bugs bugs bugs": {"prefix": False, "aliases": []}, "bugs bugs bugs": {"prefix": False, "aliases": []},
"hi $BOTNICK": {"prefix": False, "aliases": ["hello $BOTNICK"]}, "hi $BOTNICK": {"prefix": False, "aliases": ["hello $BOTNICK"]},
# [npbase, su] # [npbase, su]
"restart": {"prefix": True, "aliases": ["reboot"], "admin": True}, "restart": {"prefix": True, "aliases": ["reboot"], "admin": True},
"uptime": {"prefix": True, "aliases": []}, "uptime": {"prefix": True, "aliases": []},
"raw ": {"prefix": True, "aliases": ["cmd "], "admin": True}, "raw ": {"prefix": True, "aliases": ["cmd "], "admin": True},

View file

@ -2,6 +2,7 @@
from os import environ as env from os import environ as env
from dotenv import load_dotenv from dotenv import load_dotenv
import re import re
load_dotenv() load_dotenv()
__version__ = "v2.0.2" __version__ = "v2.0.2"
npbase = "\[\x0303last\.fm\x03\] [A-Za-z0-9_[\]{}\\|^]{1,MAX} (is listening|last listened) to: \x02.+ - .*\x02( \([0-9]+ plays\)( \[.*\])?)?" npbase = "\[\x0303last\.fm\x03\] [A-Za-z0-9_[\]{}\\|^]{1,MAX} (is listening|last listened) to: \x02.+ - .*\x02( \([0-9]+ plays\)( \[.*\])?)?"
@ -39,6 +40,8 @@ ESCAPE_SEQUENCE_RE = re.compile(
re.UNICODE | re.VERBOSE, re.UNICODE | re.VERBOSE,
) )
prefix = "." prefix = "."
def decode_escapes(s: str) -> str: def decode_escapes(s: str) -> str:
def decode_match(match): def decode_match(match):
return codecs.decode(match.group(0), "unicode-escape") return codecs.decode(match.group(0), "unicode-escape")