implemented a few doodads on the AbstractCommand class to make integration easier.

Modified do_command to receive the Twitch_Module class from twitch_script.py and the twitch.chat.Message from the bot

implemented the loader in the bot
This commit is contained in:
dtookey 2020-10-02 13:40:04 -04:00
parent 9038d244f2
commit 7c870caa92
5 changed files with 76 additions and 27 deletions

View File

@ -1,4 +1,9 @@
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from enum import Enum, auto
import twitch.chat
from twitch_script import Twitch_Module
class AbstractCommand(metaclass=ABCMeta): class AbstractCommand(metaclass=ABCMeta):
@ -10,15 +15,25 @@ class AbstractCommand(metaclass=ABCMeta):
Generally, it would be advisable to define the command (something like !so, !tts, !songrequest) as a variable of the Generally, it would be advisable to define the command (something like !so, !tts, !songrequest) as a variable of the
class and to then call super().__init__(command) class and to then call super().__init__(command)
""" """
def __init__(self, command: str):
class CommandType(Enum):
NONE = auto()
TWITCH = auto()
DISCORD = auto()
def __init__(self, command: str, n_args: int = 0, command_type=CommandType.NONE):
self.command = command self.command = command
self.n_args = n_args
self.command_type = command_type
def honk(self): # no touch!
print("Success! %s" % self.command) def get_args(self, text: str) -> list:
return text.split(" ")[0:self.n_args + 1]
# no touch!
def get_command(self) -> str: def get_command(self) -> str:
return self.command return self.command
@abstractmethod @abstractmethod
def do_command(self, fulltext): def do_command(self, bot: Twitch_Module, twitch_message: twitch.chat.Message):
pass pass

View File

@ -0,0 +1,17 @@
from abc import ABCMeta
import twitch.chat
from commands.command_base import AbstractCommand
from twitch_script import Twitch_Module
class CommandTest(AbstractCommand, metaclass=ABCMeta):
command = "!test"
def __init__(self):
super().__init__(CommandTest.command, command_type=AbstractCommand.CommandType.TWITCH)
def do_command(self, bot: Twitch_Module, twitch_message: twitch.chat.Message):
print("!test Detected")
bot.chat.send("test acknowledged")

View File

@ -1,15 +1,22 @@
from abc import ABCMeta from abc import ABCMeta
import twitch.chat
from commands.command_base import AbstractCommand from commands.command_base import AbstractCommand
from twitch_script import Twitch_Module
class CommandTTS(AbstractCommand, metaclass=ABCMeta): class CommandTTS(AbstractCommand, metaclass=ABCMeta):
command = "!tts" command = "!tts"
def __init__(self): def __init__(self):
super().__init__(CommandTTS.command) super().__init__(CommandTTS.command, n_args=1, command_type=AbstractCommand.CommandType.TWITCH)
def do_command(self, fulltext):
print(fulltext)
def do_command(self, bot: Twitch_Module, twitch_message: twitch.chat.Message):
args = self.get_args(twitch_message.text)
if args[1] == "start":
bot.send_message("tts activated on #%s" % twitch_message.channel)
bot.tts_enabled = True
elif args[1] == "stop":
bot.send_message("tts deactivated")
bot.tts_enabled = False

View File

@ -3,11 +3,12 @@ import importlib.util
import inspect import inspect
import os import os
import sys import sys
from typing import Dict
from commands.command_base import AbstractCommand from commands.command_base import AbstractCommand
def load_commands() -> dict: def load_commands() -> Dict[str, AbstractCommand]:
commands = compile_and_load() commands = compile_and_load()
return commands return commands
@ -27,7 +28,7 @@ def compile_and_load_file(path: str) -> (str, AbstractCommand):
return "", None return "", None
def compile_and_load() -> dict: def compile_and_load() -> Dict[str, AbstractCommand]:
dic = {} dic = {}
implementations = get_implementations_dir() implementations = get_implementations_dir()
for dirName, subdirList, fileList in os.walk(implementations): for dirName, subdirList, fileList in os.walk(implementations):
@ -69,4 +70,5 @@ def check_dir(path: str) -> str:
if __name__ == "__main__": if __name__ == "__main__":
load_commands() cmds = load_commands()
cmds["!tts"].do_command("!tts let's do some shit")

View File

@ -7,8 +7,11 @@ import twitch.chat
import config as config import config as config
import db import db
import tts import tts
import commands.loader as command_loader
import credentials import credentials
from commands.command_base import AbstractCommand
class Twitch_Module(): class Twitch_Module():
def __init__(self): def __init__(self):
@ -18,7 +21,7 @@ class Twitch_Module():
self.db_manager: db.db_module = db.db_module() self.db_manager: db.db_module = db.db_module()
self.chat: twitch.Chat self.chat: twitch.Chat
self.commands = command_loader.load_commands()
self.tts_enabled: bool = False self.tts_enabled: bool = False
self.tts_whitelist_enabled: bool = False self.tts_whitelist_enabled: bool = False
self.links_allowed: bool = True self.links_allowed: bool = True
@ -64,22 +67,27 @@ class Twitch_Module():
self.tts_message(message) self.tts_message(message)
def eval_commands(self, message: twitch.chat.Message): def eval_commands(self, message: twitch.chat.Message):
containsURL: bool = self.contains_url(message) # containsURL: bool = self.contains_url(message)
command_text = message.text[0:message.text.index(' ')]
if message.text.startswith('!tts start'): command = self.commands[command_text]
print("tts activated on #" + message.channel) if command is not None and command.command_type is AbstractCommand.CommandType.TWITCH:
self.send_message("tts activated") command.do_command(self, message)
self.tts_enabled = True
if message.text.startswith('!tts stop'): # if message.text.startswith('!tts start'):
print("tts deactivated on #" + message.channel) # print("tts activated on #" + message.channel)
self.send_message("tts deactivated") # self.send_message("tts activated")
self.tts_enabled = True # self.tts_enabled = True
#
if message.text.startswith('!test'): # if message.text.startswith('!tts stop'):
print("!test Detected") # print("tts deactivated on #" + message.channel)
message.chat.send("test acknowledged") # self.send_message("tts deactivated")
# message.chat.send(f'@{message.user().display_name}, you have {message.user().view_count} views.') # self.tts_enabled = False
#
# if message.text.startswith('!test'):
# print("!test Detected")
# message.chat.send("test acknowledged")
# # message.chat.send(f'@{message.user().display_name}, you have {message.user().view_count} views.')
if message.text.startswith('!roll'): if message.text.startswith('!roll'):
try: try: