Some quality refinement.
This commit is contained in:
parent
a91d4e72ec
commit
4d5553468e
4
TODO.md
4
TODO.md
@ -1,4 +0,0 @@
|
|||||||
# Todo's
|
|
||||||
|
|
||||||
- implement logging module instead of all print statements.
|
|
||||||
- use asyncio processes instead of subprocess module processes.
|
|
14
example.py
14
example.py
@ -9,24 +9,29 @@ from snekbot.bot import Bot
|
|||||||
class ExampleBot(Bot):
|
class ExampleBot(Bot):
|
||||||
|
|
||||||
async def on_join(self, channel_uid):
|
async def on_join(self, channel_uid):
|
||||||
|
await super().on_join(channel_uid)
|
||||||
print(f"I joined!")
|
print(f"I joined!")
|
||||||
|
channel = await self.get_channel(channel_uid)
|
||||||
await self.send_message(
|
await self.send_message(
|
||||||
channel_uid,
|
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):
|
async def on_leave(self, channel_uid):
|
||||||
|
await super().on_leave(channel_uid)
|
||||||
print(f"I left!!")
|
print(f"I left!!")
|
||||||
await self.send_message(
|
await self.send_message(
|
||||||
channel_uid, "I stop actively being part of the conversation now. Bye!"
|
channel_uid, "I stop actively being part of the conversation now. Bye!"
|
||||||
)
|
)
|
||||||
|
|
||||||
async def on_ping(self, username, user_nick, channel_uid, message):
|
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)
|
await self.send_message(channel_uid, "pong " + message)
|
||||||
|
|
||||||
async def on_own_message(self, data):
|
async def on_own_message(self, channel_uid, message):
|
||||||
print(f"Received my own message: {data.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):
|
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):
|
if not self.has_joined(channel_uid):
|
||||||
print(f"Probably not for me since i'm not mentioned and not joined yet")
|
print(f"Probably not for me since i'm not mentioned and not joined yet")
|
||||||
return
|
return
|
||||||
|
|
||||||
message = message.lower()
|
message = message.lower()
|
||||||
result = None
|
result = None
|
||||||
if "hello" in message:
|
if "hello" in message:
|
||||||
|
@ -29,7 +29,7 @@ class Bot:
|
|||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
self.user = None
|
self.user = None
|
||||||
self.channels = None
|
self._channels = None
|
||||||
self.rpc = None
|
self.rpc = None
|
||||||
self.ws = None
|
self.ws = None
|
||||||
self.joined = set()
|
self.joined = set()
|
||||||
@ -77,8 +77,17 @@ class Bot:
|
|||||||
await self.rpc.send_message(channel_uid, message)
|
await self.rpc.send_message(channel_uid, message)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def get_channels(self):
|
async def get_channel(self, channel_uid=None, refresh=False):
|
||||||
return await (await self.rpc.get_channels())()
|
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 def run_once(self):
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
@ -87,23 +96,25 @@ class Bot:
|
|||||||
rpc = RPC(self.ws)
|
rpc = RPC(self.ws)
|
||||||
self.rpc = rpc
|
self.rpc = rpc
|
||||||
await (await rpc.login(self.username, self.password))()
|
await (await rpc.login(self.username, self.password))()
|
||||||
self.channels = await self.get_channels()
|
for channel in await self.get_channels():
|
||||||
for channel in self.channels:
|
|
||||||
logger.debug("Found channel: " + channel["name"])
|
logger.debug("Found channel: " + channel["name"])
|
||||||
self.user = await (await rpc.get_user(None))()
|
self.user = await (await rpc.get_user(None))()
|
||||||
logger.debug("Logged in as: " + self.user["username"])
|
logger.debug("Logged in as: " + self.user["username"])
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
await self.on_idle()
|
await self.on_idle()
|
||||||
|
|
||||||
data = await rpc.receive()
|
message = None
|
||||||
|
|
||||||
if data is None:
|
while True:
|
||||||
break
|
data = await rpc.receive()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
message = data.message.strip()
|
message = data.message.strip()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
continue
|
continue
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
if data.username == self.user["username"]:
|
if data.username == self.user["username"]:
|
||||||
await self.on_own_message(data.channel_uid, message)
|
await self.on_own_message(data.channel_uid, message)
|
||||||
|
Loading…
Reference in New Issue
Block a user