Browse Source

Add support for commands on timer

pull/2/head
Jérôme Deuchnord 3 years ago
parent
commit
66f82a2875
3 changed files with 37 additions and 23 deletions
  1. +33
    -17
      _twitchbot/config.py
  2. +4
    -5
      _twitchbot/twitchbot.py
  3. +0
    -1
      bot.py

+ 33
- 17
_twitchbot/config.py View File

@@ -37,7 +37,7 @@ class Command:
@classmethod @classmethod
def from_dict(cls, params: dict): def from_dict(cls, params: dict):
return Command( return Command(
params['name'],
params.get('name'),
params['message'], params['message'],
params.get('aliases', []), params.get('aliases', []),
params.get('disabled', False) params.get('disabled', False)
@@ -53,27 +53,32 @@ class Timer:
time_between: int time_between: int
msgs_between: int msgs_between: int
strategy: TimerStrategy strategy: TimerStrategy
messages: [str]
pool: [Command]


def __init__( def __init__(
self, self,
time_between: int = 10, time_between: int = 10,
msgs_between: int = 10, msgs_between: int = 10,
strategy: TimerStrategy = TimerStrategy.ROUND_ROBIN, strategy: TimerStrategy = TimerStrategy.ROUND_ROBIN,
messages: [str] = None
pool: [Command] = None
): ):
self.time_between = time_between self.time_between = time_between
self.msgs_between = msgs_between self.msgs_between = msgs_between
self.strategy = strategy self.strategy = strategy
self.messages = messages if messages else []
self.pool = pool if pool else []


@classmethod @classmethod
def from_dict(cls, param: dict): def from_dict(cls, param: dict):
pool = []

for c in param.get('pool', []):
pool.append(Command.from_dict(c))

return Timer( return Timer(
time_between=param.get('between', {}).get('time', 10), time_between=param.get('between', {}).get('time', 10),
msgs_between=param.get('between', {}).get('messages', 10), msgs_between=param.get('between', {}).get('messages', 10),
strategy=TimerStrategy(param.get('strategy', 'round-robin')), strategy=TimerStrategy(param.get('strategy', 'round-robin')),
messages=param.get('messages', [])
pool=pool
) )




@@ -86,13 +91,13 @@ class Config:
timer: Timer timer: Timer


def __init__( def __init__(
self,
channel: str,
nickname: str,
token: str,
command_prefix: str,
commands: [Command],
timer: Timer
self,
channel: str,
nickname: str,
token: str,
command_prefix: str,
commands: [Command],
timer: Timer
): ):
self.nickname = nickname self.nickname = nickname
self.channel = channel self.channel = channel
@@ -103,21 +108,32 @@ class Config:


@classmethod @classmethod
def from_dict(cls, params: dict, token: str): def from_dict(cls, params: dict, token: str):
timer = Timer.from_dict(params.get('timer', {}))

commands_prefix = params.get('command_prefix', '!') commands_prefix = params.get('command_prefix', '!')
commands = [] commands = []


help_command = Command("help", "Voici les commandes disponibles : ") help_command = Command("help", "Voici les commandes disponibles : ")


for command in params.get('commands', []): for command in params.get('commands', []):
c = Command.from_dict(command)
command = Command.from_dict(command)


if c.disabled:
if command.disabled:
continue continue


commands.append(c)
help_command.message = "%s %s%s" % (help_command.message, commands_prefix, c.name)
commands.append(command)


for command in timer.pool:
if command.name is None:
continue

commands.append(command)

# Generate help command
if params.get('help', True): if params.get('help', True):
for command in commands:
help_command.message = "%s %s%s" % (help_command.message, commands_prefix, command.name)

commands.append(help_command) commands.append(help_command)


return Config( return Config(
@@ -126,7 +142,7 @@ class Config:
token, token,
commands_prefix, commands_prefix,
commands, commands,
Timer.from_dict(params.get('timer', {}))
timer
) )


def find_command(self, command: str) -> Union[None, Command]: def find_command(self, command: str) -> Union[None, Command]:


+ 4
- 5
_twitchbot/twitchbot.py View File

@@ -67,10 +67,9 @@ class TwitchBot:
self.play_timer() self.play_timer()


def play_timer(self): def play_timer(self):
print(self.messages_stack)
if not self.messages_stack: if not self.messages_stack:
print('Filling the timer messages stack in') print('Filling the timer messages stack in')
self.messages_stack = self.config.timer.messages.copy()
self.messages_stack = self.config.timer.pool.copy()
if self.config.timer.strategy == TimerStrategy.SHUFFLE: if self.config.timer.strategy == TimerStrategy.SHUFFLE:
print('Shuffle!') print('Shuffle!')
shuffle(self.messages_stack) shuffle(self.messages_stack)
@@ -79,10 +78,10 @@ class TwitchBot:
datetime.now() < self.last_timer_date + timedelta(minutes=self.config.timer.time_between): datetime.now() < self.last_timer_date + timedelta(minutes=self.config.timer.time_between):
return return


message = self.messages_stack.pop(0)
command = self.messages_stack.pop(0)


print("Timer: %s" % message)
self.bot.privmsg('#%s' % self.config.channel, message)
print("Timer: %s" % command.message)
self.bot.privmsg('#%s' % self.config.channel, command.message)


self.nb_messages_since_timer = 0 self.nb_messages_since_timer = 0
self.last_timer_date = datetime.now() self.last_timer_date = datetime.now()


+ 0
- 1
bot.py View File

@@ -32,7 +32,6 @@ def main() -> int:
args = get_arguments() args = get_arguments()


twitchbot.config = get_config(args.config) twitchbot.config = get_config(args.config)
print(twitchbot.config.timer.messages)
bot = irc3.IrcBot.from_config({ bot = irc3.IrcBot.from_config({
'nick': twitchbot.config.nickname, 'nick': twitchbot.config.nickname,
'password': twitchbot.config.token, 'password': twitchbot.config.token,


Loading…
Cancel
Save