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

Commit c2358c0

Browse files
author
DirectiveAthena
committed
Feat: Outputs are created while connection_made function
1 parent 520ad75 commit c2358c0

File tree

4 files changed

+38
-30
lines changed

4 files changed

+38
-30
lines changed

src/AthenaTwitchBot/functions/launch.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def launch(
2222
*, # after this, keywords only
2323
bot:TwitchBot=None,
2424
protocol_factory:Callable=None,
25-
outputs:list[AbstractOutput]=None,
25+
outputs:list[type[AbstractOutput]]=None,
2626
console_enabled:bool=True,
2727
irc_connection:bool=True,
2828
irc_host:str='irc.chat.twitch.tv',
@@ -32,14 +32,14 @@ def launch(
3232
):
3333
if irc_connection:
3434
if outputs is None:
35-
outputs=[OutputTwitch()]
35+
outputs=[OutputTwitch]
3636
# thanks for fikotta for pointing me to use isinstance here
37-
if not any(isinstance(o, OutputTwitch) for o in outputs):
37+
if not any(issubclass(o, OutputTwitch) for o in outputs):
3838
# always make sure OutputTwitch callbacks are the first to be made
39-
outputs.insert(0,OutputTwitch())
40-
if not any(isinstance(o, OutputConsole) for o in outputs) and console_enabled:
39+
outputs.insert(0,OutputTwitch)
40+
if not any(issubclass(o, OutputConsole) for o in outputs) and console_enabled:
4141
# placement of the Console does not matter
42-
outputs.append(OutputConsole())
42+
outputs.append(OutputConsole)
4343

4444
# a bot always has to be defined
4545
if bot is None or not isinstance(bot, TwitchBot):

src/AthenaTwitchBot/models/outputs/abstract_output.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# General Packages
55
from __future__ import annotations
66
from abc import ABC, abstractmethod
7-
import asyncio
87

98
# Custom Library
109

@@ -17,26 +16,29 @@
1716
# ----------------------------------------------------------------------------------------------------------------------
1817
class AbstractOutput(ABC):
1918

19+
def __init__(self, **kwargs):
20+
pass
21+
2022
@abstractmethod
21-
async def connection_made(self, bot:TwitchBot, transport: asyncio.transports.Transport,**kwargs):
23+
async def connection_made(self, bot:TwitchBot, **kwargs):
2224
"""Output of data when the connection to the Twitch servers is established"""
2325

2426
@abstractmethod
25-
async def connection_ping(self, transport: asyncio.transports.Transport, ping_response:list[str],**kwargs):
27+
async def connection_ping(self, ping_response:list[str],**kwargs):
2628
"""Output response to a ping sent by the Twitch servers"""
2729

2830
@abstractmethod
2931
async def undefined(self,text:str, **kwargs):
3032
"""Output response to anything that wasn't caught correctly"""
3133

3234
@abstractmethod
33-
async def write(self,transport: asyncio.transports.Transport,context:TwitchContext, **kwargs):
35+
async def write(self, context:TwitchContext, **kwargs):
3436
"""Output which context is not a reply, but simply write out to the chat"""
3537

3638
@abstractmethod
37-
async def reply(self,transport: asyncio.transports.Transport,context:TwitchContext, **kwargs):
39+
async def reply(self, context:TwitchContext, **kwargs):
3840
"""Output which context is a reply to a user"""
3941

4042
@abstractmethod
41-
async def scheduled_task(self, transport: asyncio.transports.Transport, context:TwitchContext, **kwargs):
43+
async def scheduled_task(self, context:TwitchContext, **kwargs):
4244
"""Automated output after a task has been run"""

src/AthenaTwitchBot/models/outputs/output_twitch.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,43 @@
2020
# - Code -
2121
# ----------------------------------------------------------------------------------------------------------------------
2222
class OutputTwitch(AbstractOutput):
23-
async def connection_made(self, bot:TwitchBot, transport: asyncio.transports.Transport,**kwargs):
24-
transport.write(format_message(f"{PASS}{bot.oauth_token}"))
25-
transport.write(format_message(f"{NICK} {bot.nickname}"))
26-
transport.write(format_message(f"{JOIN} {','.join(str(c) for c in bot.channels)}"))
27-
transport.write(format_message(REQUEST_TAGS))
23+
transport:asyncio.transports.Transport
2824

29-
async def connection_ping(self, transport: asyncio.transports.Transport, ping_response:list[str], **kwargs):
30-
transport.write(format_message(f"{PONG} {' '.join(ping_response)}"))
25+
def __init__(self,transport: asyncio.transports.Transport, **kwargs):
26+
self.transport = transport
27+
28+
async def connection_made(self, bot:TwitchBot,**kwargs):
29+
self.transport.write(format_message(f"{PASS}{bot.oauth_token}"))
30+
self.transport.write(format_message(f"{NICK} {bot.nickname}"))
31+
self.transport.write(format_message(f"{JOIN} {','.join(str(c) for c in bot.channels)}"))
32+
self.transport.write(format_message(REQUEST_TAGS))
33+
34+
async def connection_ping(self, ping_response:list[str], **kwargs):
35+
self.transport.write(format_message(f"{PONG} {' '.join(ping_response)}"))
3136

3237
async def undefined(self,**kwargs):
3338
pass # don't answer to something that is undefined
3439

35-
async def write(self, transport: asyncio.transports.Transport, context:TwitchContext, **kwargs):
40+
async def write(self, context:TwitchContext, **kwargs):
3641
if context.output_text is not None:
37-
transport.write(
42+
self.transport.write(
3843
format_message(
3944
f"{PRIVMSG} {context.channel} :{context.output_text}"
4045
))
4146

42-
async def reply(self, transport: asyncio.transports.Transport, context:TwitchContext, **kwargs):
47+
async def reply(self, context:TwitchContext, **kwargs):
4348
if context.output_text is not None:
4449
if context.message_tags.message_id != EMPTY_STR:
45-
transport.write(
50+
self.transport.write(
4651
format_message(
4752
f"@reply-parent-msg-id={context.message_tags.message_id} {PRIVMSG} {context.channel} :{context.output_text}"
4853
))
4954
else:
50-
transport.write(
55+
self.transport.write(
5156
format_message(
5257
f"{PRIVMSG} {context.channel} :{context.output_text}"
5358
))
5459

5560

56-
async def scheduled_task(self,transport: asyncio.transports.Transport, context:TwitchContext, **kwargs):
61+
async def scheduled_task(self, context:TwitchContext, **kwargs):
5762
pass # is handled by the "write" output

src/AthenaTwitchBot/models/twitch_bot_protocol.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
@dataclass(kw_only=True, slots=True, eq=False, order=False)
3434
class TwitchBotProtocol(asyncio.Protocol):
3535
bot:TwitchBot
36-
outputs:list[AbstractOutput]
36+
outputs:list[type[AbstractOutput]]
3737

3838
# non init slots
39-
transport:asyncio.transports.Transport = field(init=False)
39+
outputs_initialized:list[AbstractOutput] =field(init=False)
4040
message_constructor:Callable = field(init=False)
4141
loop:asyncio.AbstractEventLoop = field(init=False)
4242
PREFIX_FULL:str = field(init=False)
@@ -70,8 +70,8 @@ async def scheduled_task_coro(self, tsk:TwitchBotMethod, channel:TwitchChannel):
7070
def output_handler(self,callback:OUTPUT_CALLBACKS, **kwargs):
7171
asyncio.ensure_future(
7272
asyncio.gather(
73-
*(callback(output=output, transport=self.transport, **kwargs)
74-
for output in self.outputs)
73+
*(callback(output=output, **kwargs)
74+
for output in self.outputs_initialized)
7575
),
7676
loop=self.loop
7777
)
@@ -80,7 +80,8 @@ def output_handler(self,callback:OUTPUT_CALLBACKS, **kwargs):
8080
# - Protocol necessary -
8181
# ------------------------------------------------------------------------------------------------------------------
8282
def connection_made(self, transport: asyncio.transports.Transport) -> None:
83-
self.transport = transport
83+
self.outputs_initialized = [output(transport=transport) for output in self.outputs]
84+
8485
# first write the password then the nickname else the connection will fail
8586
self.output_handler(
8687
callback=output_connection_made,

0 commit comments

Comments
 (0)