Explorar el Código

Add support for command aliases

pull/2/head
Jérôme Deuchnord hace 3 años
padre
commit
28383daf7d
Se han modificado 3 ficheros con 23 adiciones y 10 borrados
  1. +2
    -1
      .gitignore
  2. +15
    -2
      _twitchbot/config.py
  3. +6
    -7
      _twitchbot/twitchbot.py

+ 2
- 1
.gitignore Ver fichero

@@ -1,2 +1,3 @@
config.json
docker-compose.yml
docker-compose.yml
__pycache__

+ 15
- 2
_twitchbot/config.py Ver fichero

@@ -19,19 +19,22 @@ import json

from os import environ
from enum import Enum
from typing import Union


class Command:
name: str
message: str
aliases: [str]

def __init__(self, name: str, message: str):
def __init__(self, name: str, message: str, aliases: [str] = []):
self.name = name
self.message = message
self.aliases = aliases

@classmethod
def from_dict(cls, params: dict):
return Command(params['name'], params['message'])
return Command(params['name'], params['message'], params.get('aliases', []))


class TimerStrategy(Enum):
@@ -114,6 +117,16 @@ class Config:
Timer.from_dict(params.get('timer', {}))
)

def find_command(self, command: str) -> Union[None, Command]:
if command.startswith(self.command_prefix):
command = command[1:]

for c in self.commands:
if c.name == command or command in c.aliases:
return c

return None


def get_config(file_path: str):
with open(file_path, 'r') as config_file:


+ 6
- 7
_twitchbot/twitchbot.py Ver fichero

@@ -17,7 +17,7 @@

import irc3

from .config import get_config, TimerStrategy
from .config import TimerStrategy
from random import shuffle
from datetime import datetime, timedelta

@@ -57,12 +57,11 @@ class TwitchBot:

@irc3.event(irc3.rfc.PRIVMSG)
def on_msg(self, target, mask, data, **_):
print(target)
for command in self.config.commands:
if ('%s ' % data.lower()).startswith('%s%s ' % (self.config.command_prefix, command.name.lower())):
print('%s: %s%s' % (mask, self.config.command_prefix, command.name))
self.bot.privmsg(target, self._parse_variables(command.message, mask))
break
command = self.config.find_command(data.lower().split(' ')[0])

if command is not None:
print('%s: %s%s' % (mask, self.config.command_prefix, command.name))
self.bot.privmsg(target, self._parse_variables(command.message, mask))

self.nb_messages_since_timer += 1
self.play_timer()


Cargando…
Cancelar
Guardar