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:
parent
9038d244f2
commit
354a6da9ae
@ -1,4 +1,9 @@
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from enum import Enum, auto
|
||||
|
||||
import twitch.chat
|
||||
|
||||
from twitch_script import Twitch_Module
|
||||
|
||||
|
||||
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
|
||||
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.n_args = n_args
|
||||
self.command_type = command_type
|
||||
|
||||
def honk(self):
|
||||
print("Success! %s" % self.command)
|
||||
# no touch!
|
||||
def get_args(self, text: str) -> list:
|
||||
return text.split(" ")[0:self.n_args + 1]
|
||||
|
||||
# no touch!
|
||||
def get_command(self) -> str:
|
||||
return self.command
|
||||
|
||||
@abstractmethod
|
||||
def do_command(self, fulltext):
|
||||
def do_command(self, bot: Twitch_Module, twitch_message: twitch.chat.Message):
|
||||
pass
|
||||
|
||||
17
commands/implemented/command_test.py
Normal file
17
commands/implemented/command_test.py
Normal 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")
|
||||
@ -1,15 +1,22 @@
|
||||
from abc import ABCMeta
|
||||
|
||||
import twitch.chat
|
||||
|
||||
from commands.command_base import AbstractCommand
|
||||
from twitch_script import Twitch_Module
|
||||
|
||||
|
||||
class CommandTTS(AbstractCommand, metaclass=ABCMeta):
|
||||
|
||||
command = "!tts"
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(CommandTTS.command)
|
||||
|
||||
def do_command(self, fulltext):
|
||||
print(fulltext)
|
||||
super().__init__(CommandTTS.command, n_args=1, command_type=AbstractCommand.CommandType.TWITCH)
|
||||
|
||||
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
|
||||
|
||||
@ -3,11 +3,12 @@ import importlib.util
|
||||
import inspect
|
||||
import os
|
||||
import sys
|
||||
from typing import Dict
|
||||
|
||||
from commands.command_base import AbstractCommand
|
||||
|
||||
|
||||
def load_commands() -> dict:
|
||||
def load_commands() -> Dict[str, AbstractCommand]:
|
||||
commands = compile_and_load()
|
||||
return commands
|
||||
|
||||
@ -27,7 +28,7 @@ def compile_and_load_file(path: str) -> (str, AbstractCommand):
|
||||
return "", None
|
||||
|
||||
|
||||
def compile_and_load() -> dict:
|
||||
def compile_and_load() -> Dict[str, AbstractCommand]:
|
||||
dic = {}
|
||||
implementations = get_implementations_dir()
|
||||
for dirName, subdirList, fileList in os.walk(implementations):
|
||||
@ -69,4 +70,5 @@ def check_dir(path: str) -> str:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
load_commands()
|
||||
cmds = load_commands()
|
||||
cmds["!tts"].do_command("!tts let's do some shit")
|
||||
|
||||
@ -7,8 +7,11 @@ import twitch.chat
|
||||
import config as config
|
||||
import db
|
||||
import tts
|
||||
import commands.loader as command_loader
|
||||
|
||||
import credentials
|
||||
from commands.command_base import AbstractCommand
|
||||
|
||||
|
||||
class Twitch_Module():
|
||||
def __init__(self):
|
||||
@ -18,7 +21,7 @@ class Twitch_Module():
|
||||
|
||||
self.db_manager: db.db_module = db.db_module()
|
||||
self.chat: twitch.Chat
|
||||
|
||||
self.commands = command_loader.load_commands()
|
||||
self.tts_enabled: bool = False
|
||||
self.tts_whitelist_enabled: bool = False
|
||||
self.links_allowed: bool = True
|
||||
@ -65,21 +68,26 @@ class Twitch_Module():
|
||||
|
||||
def eval_commands(self, message: twitch.chat.Message):
|
||||
containsURL: bool = self.contains_url(message)
|
||||
command_text = message.text[0:message.text.index(' ')]
|
||||
|
||||
if message.text.startswith('!tts start'):
|
||||
print("tts activated on #" + message.channel)
|
||||
self.send_message("tts activated")
|
||||
self.tts_enabled = True
|
||||
command = self.commands[command_text]
|
||||
if command is not None and command.command_type is not AbstractCommand.CommandType.NONE:
|
||||
command.do_command(self, message)
|
||||
|
||||
if message.text.startswith('!tts stop'):
|
||||
print("tts deactivated on #" + message.channel)
|
||||
self.send_message("tts deactivated")
|
||||
self.tts_enabled = True
|
||||
|
||||
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('!tts start'):
|
||||
# print("tts activated on #" + message.channel)
|
||||
# self.send_message("tts activated")
|
||||
# self.tts_enabled = True
|
||||
#
|
||||
# if message.text.startswith('!tts stop'):
|
||||
# print("tts deactivated on #" + message.channel)
|
||||
# self.send_message("tts deactivated")
|
||||
# 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'):
|
||||
try:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user