Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit 9431f83

Browse files
author
DirectiveAthena
committed
Fix: scheduled_tasks
1 parent 1a9d610 commit 9431f83

File tree

4 files changed

+54
-62
lines changed

4 files changed

+54
-62
lines changed

src/AthenaTwitchBot/decorators/scheduled_task.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,12 @@ def wrapper(*args, **kwargs):
4848
channels.append(TwitchChannel(channel))
4949
elif isinstance(channel, TwitchChannel):
5050
channels.append(c)
51-
else:
52-
return NotImplemented
53-
else:
54-
return NotImplemented
5551

5652
wrapper.tsk = ScheduledTask(
5753
delay=convert_time_to_seconds(delay,to_int=True),
5854
wait_before=wait_before,
5955
callback=wrapper,
60-
channels=channels
56+
channels=channels if channels else None
6157
)
6258
return wrapper
6359
return decorator

src/AthenaTwitchBot/models/outputs/output_twitch.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ async def undefined(self,**kwargs):
3434

3535
async def write(self, transport: asyncio.transports.Transport, context:TwitchContext, **kwargs):
3636
if context.output_text is not None:
37-
transport.write(format_message(f"{PRIVMSG} {context.channel} :{context.output_text}"))
37+
transport.write(
38+
format_message(
39+
f"{PRIVMSG} {context.channel} :{context.output_text}"
40+
))
3841

3942
async def reply(self, transport: asyncio.transports.Transport, context:TwitchContext, **kwargs):
4043
if context.output_text is not None and context.message_tags.message_id != EMPTY_STR:
@@ -43,7 +46,5 @@ async def reply(self, transport: asyncio.transports.Transport, context:TwitchCon
4346
f"@reply-parent-msg-id={context.message_tags.message_id} {PRIVMSG} {context.channel} :{context.output_text}"
4447
))
4548

46-
47-
4849
async def scheduled_task(self,transport: asyncio.transports.Transport, context:TwitchContext, **kwargs):
4950
pass

src/AthenaTwitchBot/models/twitch_bot_protocol.py

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,17 @@ async def scheduled_task_coro(self, tsk:ScheduledTask, channel:TwitchChannel):
5353
tags=None,
5454
user_name_str=self.bot.nickname,
5555
channel_str=channel,
56-
text=tuple()
56+
text=tuple(),
57+
raw_irc=[""]
5758
)
5859
if tsk.wait_before: # the wait_before attribute handles if we sleep wait_before or after the task has been called
5960
await asyncio.sleep(tsk.delay)
6061
tsk.callback(self=self.bot,context=context)
6162
else:
6263
tsk.callback(self=self.bot,context=context)
6364
await asyncio.sleep(tsk.delay)
64-
self.output_handler(
65-
callback=output_scheduled_task,
66-
# below this point is all **kwargs
67-
context=context
68-
)
65+
66+
self.parse_context_output(context)
6967

7068
def output_handler(self,callback:OUTPUT_CALLBACKS, **kwargs):
7169
# TODO test code below with asyncio.gather
@@ -79,7 +77,7 @@ def output_handler(self,callback:OUTPUT_CALLBACKS, **kwargs):
7977
,
8078
loop=self.loop)
8179

82-
def create_context(self, tags:str|None, user_name_str:str, channel_str:str|TwitchChannel, text:tuple[str]) -> TwitchContext:
80+
def create_context(self, raw_irc,tags:str|None, user_name_str:str, channel_str:str|TwitchChannel, text:tuple[str]) -> TwitchContext:
8381
return TwitchContext(
8482
message_tags=TwitchMessageTags.new_from_tags_str(tags)
8583
if self.bot.twitch_capability_tags and tags is not None else
@@ -88,7 +86,8 @@ def create_context(self, tags:str|None, user_name_str:str, channel_str:str|Twitc
8886
channel=channel_str
8987
if isinstance(channel_str, TwitchChannel) else
9088
TwitchChannel(channel_str) ,
91-
raw_text=text
89+
raw_text=text,
90+
raw_irc=raw_irc
9291
)
9392

9493
# ------------------------------------------------------------------------------------------------------------------
@@ -104,8 +103,11 @@ def connection_made(self, transport: asyncio.transports.Transport) -> None:
104103
)
105104

106105
# add frequent_output methods to the coroutine loop
106+
107+
print(self.bot.scheduled_tasks)
108+
107109
for tsk in self.bot.scheduled_tasks:
108-
if tsk.channels: # the list is populated with channels
110+
if tsk.channels is not None and tsk.channels: # the list is populated with channels
109111
for channel in tsk.channels:
110112
coro = self.loop.create_task(self.scheduled_task_coro(tsk, channel=channel))
111113
asyncio.ensure_future(coro, loop=self.loop)
@@ -168,14 +170,8 @@ def parse_data(self, data: bytearray):
168170
# PRIVMSG
169171
# #directiveathena
170172
# :that sentence was poggers
171-
if self.parse_user_message(tags, user_name_str, channel_str, text):
172-
# a command was caught:
173-
continue # because the output was handled by the parser above
174-
# else:
175-
self.output_handler(
176-
callback=output_undefined,
177-
# below this point is all **kwargs
178-
text=" ".join(d_split)
173+
self.parse_context_output(
174+
self.parse_user_message(d_split, tags, user_name_str, channel_str, text)
179175
)
180176

181177
case str(user_name_str), _privmsg, str(channel_str), *text \
@@ -186,14 +182,8 @@ def parse_data(self, data: bytearray):
186182
# PRIVMSG
187183
# #directiveathena
188184
# :that sentence was poggers
189-
if self.parse_user_message(None, user_name_str, channel_str, text):
190-
# a command was caught:
191-
continue # because the output was handled by the parser above
192-
# else:
193-
self.output_handler(
194-
callback=output_undefined,
195-
# below this point is all **kwargs
196-
text=" ".join(d_split)
185+
self.parse_context_output(
186+
self.parse_user_message(d_split, None, user_name_str, channel_str, text)
197187
)
198188

199189
case str(bot_name_long), _join, str(channel) \
@@ -203,7 +193,6 @@ def parse_data(self, data: bytearray):
203193
# :eva_athenabot!eva_athenabot@eva_athenabot.tmi.twitch.tv
204194
# JOIN
205195
# #directiveathena
206-
pass # todo functionality
207196
self.output_handler(
208197
callback=output_undefined,
209198
# below this point is all **kwargs
@@ -217,7 +206,6 @@ def parse_data(self, data: bytearray):
217206
# *
218207
# ACK
219208
# :twitch.tv/tags
220-
pass # todo functionality
221209
self.output_handler(
222210
callback=output_undefined,
223211
# below this point is all **kwargs
@@ -235,7 +223,6 @@ def parse_data(self, data: bytearray):
235223
# =
236224
# #directiveathena
237225
# :eva_athenabot
238-
pass # todo functionality
239226
self.output_handler(
240227
callback=output_undefined,
241228
# below this point is all **kwargs
@@ -251,7 +238,6 @@ def parse_data(self, data: bytearray):
251238
# =
252239
# #directiveathena
253240
# :eva_athenabot
254-
pass # todo functionality
255241
self.output_handler(
256242
callback=output_undefined,
257243
# below this point is all **kwargs
@@ -268,36 +254,42 @@ def parse_data(self, data: bytearray):
268254
# ------------------------------------------------------------------------------------------------------------------
269255
# - Message Parsing -
270256
# ------------------------------------------------------------------------------------------------------------------
271-
def parse_user_message(self, tags:str|None, user_name_str:str, channel_str:str, text:tuple[str]) -> bool:
272-
context:TwitchContext = self.create_context(tags,user_name_str,channel_str,text)
273-
257+
def parse_user_message(self, raw_irc, tags:str|None, user_name_str:str, channel_str:str, text:tuple[str]) -> TwitchContext:
258+
context:TwitchContext = self.create_context(raw_irc, tags,user_name_str,channel_str,text)
274259
PREFIX_FULL = f":{self.bot.prefix}"
275260

276-
if (cmd_str := context.raw_text[0]).startswith(PREFIX_FULL) and context.raw_text[0] != PREFIX_FULL:
277-
context.is_command = True
278-
context.command_str = cmd_str.replace(PREFIX_FULL, "").lower()
261+
if (cmd_str := context.raw_text[0]).startswith(PREFIX_FULL) and cmd_str != PREFIX_FULL:
279262

280-
command:Command= self.bot.commands[context.command_str.lower()]
263+
context.command_str = cmd_str.replace(PREFIX_FULL, "")
264+
cmd_str_lower = context.command_str.lower()
281265

282-
#checvk if the command was case-sensitive and break if it is
283-
if command.case_sensitive and context.command_str != context.command_str.lower():
284-
return False
266+
try:
267+
command:Command = self.bot.commands[cmd_str_lower]
268+
# check if the command was case-sensitive and break if it is
269+
if command.case_sensitive and context.command_str != cmd_str_lower:
270+
raise KeyError
271+
except KeyError:
272+
return context
273+
274+
context.is_command = True
275+
command.callback(self=self.bot,context=context)
285276

286-
#check if the user message is a command
287-
if context.command_str.lower() in self.bot.commands:
288-
command:Command
289-
command.callback(self=self.bot,context=context)
277+
return context
290278

279+
def parse_context_output(self, context:TwitchContext):
280+
match context:
281+
case TwitchContext(is_write=True):
291282
self.output_handler(
292-
callback=output_reply if context.is_reply else output_write,
293-
# below this point is all **kwargs
283+
callback=output_write,
294284
context=context
295285
)
296-
return True
297-
return False
298-
299-
300-
301-
302-
303-
286+
case TwitchContext(is_reply=True):
287+
self.output_handler(
288+
callback=output_reply,
289+
context=context
290+
)
291+
case _:
292+
self.output_handler(
293+
callback=output_undefined,
294+
text=" ".join(context.raw_irc)
295+
)

src/AthenaTwitchBot/models/twitch_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ class TwitchContext:
2121
user:TwitchUser
2222
channel:TwitchChannel
2323
raw_text:tuple[str]
24+
raw_irc:list[str]
2425

2526
#some non init stuff
26-
is_command:bool=field(init=False, default=False)
2727
command_str:str=field(init=False, default=None)
28+
is_command:bool=field(init=False, default=False)
2829
is_reply:bool=field(init=False, default=False)
30+
is_write:bool=field(init=False, default=False)
2931
output_text:str=field(init=False, default=None)
3032

3133
def reply(self, text:str):
3234
self.is_reply = True
3335
self.output_text=self._cleanup_output_text(text)
3436

3537
def write(self, text:str):
38+
self.is_write = True
3639
self.output_text=self._cleanup_output_text(text)
3740

3841
@staticmethod

0 commit comments

Comments
 (0)