Some quality refinement.

This commit is contained in:
retoor 2025-04-24 21:38:56 +02:00
parent a91d4e72ec
commit 4d5553468e
3 changed files with 33 additions and 20 deletions

View File

@ -1,4 +0,0 @@
# Todo's
- implement logging module instead of all print statements.
- use asyncio processes instead of subprocess module processes.

View File

@ -9,24 +9,29 @@ from snekbot.bot import Bot
class ExampleBot(Bot):
async def on_join(self, channel_uid):
await super().on_join(channel_uid)
print(f"I joined!")
channel = await self.get_channel(channel_uid)
await self.send_message(
channel_uid,
f"Hello, i'm actively part of the conversation in channel {channel_uid} now, you don't have to mention me anymore. ",
f"Hello, i'm actively part of the conversation in channel {channel['name']} now, you don't have to mention me anymore. ",
)
async def on_leave(self, channel_uid):
await super().on_leave(channel_uid)
print(f"I left!!")
await self.send_message(
channel_uid, "I stop actively being part of the conversation now. Bye!"
)
async def on_ping(self, username, user_nick, channel_uid, message):
print(f"Ping from {user_nick} in channel {channel_uid}: {message}")
channel = await self.get_channel(channel_uid)
print(f"Ping from {user_nick} in channel {channel['name']}: {message}")
await self.send_message(channel_uid, "pong " + message)
async def on_own_message(self, data):
print(f"Received my own message: {data.message}")
async def on_own_message(self, channel_uid, message):
channel = await self.get_channel(channel_uid)
print(f"Received my own message in channel {channel['name']}: {message}")
async def on_mention(self, username, user_nick, channel_uid, message):
@ -54,6 +59,7 @@ class ExampleBot(Bot):
if not self.has_joined(channel_uid):
print(f"Probably not for me since i'm not mentioned and not joined yet")
return
message = message.lower()
result = None
if "hello" in message:

View File

@ -29,7 +29,7 @@ class Bot:
self.username = username
self.password = password
self.user = None
self.channels = None
self._channels = None
self.rpc = None
self.ws = None
self.joined = set()
@ -77,8 +77,17 @@ class Bot:
await self.rpc.send_message(channel_uid, message)
return True
async def get_channels(self):
return await (await self.rpc.get_channels())()
async def get_channel(self, channel_uid=None, refresh=False):
for channel in await self.get_channels(refresh):
if channel["uid"] == channel_uid:
return channel
if not refresh:
return await self.get_channel(channel_uid, True)
async def get_channels(self, refresh=False):
if refresh or not self._channels:
self._channels = await (await self.rpc.get_channels())()
return self._channels
async def run_once(self):
async with aiohttp.ClientSession() as session:
@ -87,23 +96,25 @@ class Bot:
rpc = RPC(self.ws)
self.rpc = rpc
await (await rpc.login(self.username, self.password))()
self.channels = await self.get_channels()
for channel in self.channels:
for channel in await self.get_channels():
logger.debug("Found channel: " + channel["name"])
self.user = await (await rpc.get_user(None))()
logger.debug("Logged in as: " + self.user["username"])
while True:
await self.on_idle()
data = await rpc.receive()
message = None
if data is None:
break
while True:
data = await rpc.receive()
try:
message = data.message.strip()
except AttributeError:
continue
try:
message = data.message.strip()
except AttributeError:
continue
else:
break
if data.username == self.user["username"]:
await self.on_own_message(data.channel_uid, message)