feat: refactor bot event handlers to accept flat parameters and add join/leave auto-replies
This commit is contained in:
+44
-50
@@ -17,6 +17,9 @@ import aiohttp
|
||||
|
||||
from snekbot.rpc import RPC
|
||||
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger("snekbot")
|
||||
|
||||
class Bot:
|
||||
|
||||
@@ -28,7 +31,7 @@ class Bot:
|
||||
self.channels = None
|
||||
self.rpc = None
|
||||
self.ws = None
|
||||
self.join_conversation = False
|
||||
self.joined = set()
|
||||
|
||||
async def run(self, reconnect=True):
|
||||
while True:
|
||||
@@ -39,6 +42,9 @@ class Bot:
|
||||
await asyncio.sleep(1)
|
||||
if not reconnect:
|
||||
break
|
||||
|
||||
def has_joined(self, channel_uid):
|
||||
return channel_uid in self.joined
|
||||
|
||||
async def send_message(self, channel_uid, message):
|
||||
await self.rpc.send_message(channel_uid, message)
|
||||
@@ -50,60 +56,48 @@ class Bot:
|
||||
self.ws = ws
|
||||
self.rpc = RPC(ws)
|
||||
rpc = self.rpc
|
||||
await (await rpc.login(self.username, self.password))()
|
||||
try:
|
||||
raise Exception(self.login_result.exception)
|
||||
except:
|
||||
pass
|
||||
success = await (await rpc.login(self.username, self.password))()
|
||||
print(success)
|
||||
self.channels = await (await rpc.get_channels())()
|
||||
for channel in self.channels:
|
||||
logger.debug("Found channel: " + channel["name"])
|
||||
self.user = (await (await rpc.get_user(None))()).data
|
||||
logger.debug("Logged in as: " + self.user["username"])
|
||||
self.join_conversation = False
|
||||
while True:
|
||||
print("Waiting for message...")
|
||||
data = await rpc.receive()
|
||||
|
||||
if not data:
|
||||
break
|
||||
|
||||
try:
|
||||
message = data.message.strip()
|
||||
except:
|
||||
continue
|
||||
try:
|
||||
await self.on_idle()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
data = await rpc.receive()
|
||||
|
||||
if data.username == self.user["username"]:
|
||||
try:
|
||||
message = data.message.strip()
|
||||
except AttributeError:
|
||||
continue
|
||||
|
||||
if data.username == self.user["username"]:
|
||||
await self.on_own_message(data)
|
||||
except Exception as ex:
|
||||
print("Error", ex)
|
||||
continue
|
||||
elif message.startswith("ping"):
|
||||
try:
|
||||
await self.on_ping(data)
|
||||
except Exception as ex:
|
||||
print("Error:", ex)
|
||||
continue
|
||||
elif "@" + self.user["nick"] in data.message:
|
||||
try:
|
||||
await self.on_mention(data)
|
||||
except Exception as ex:
|
||||
print("Error:", ex)
|
||||
continue
|
||||
elif "@" + self.user["nick"] + " join" in data.message:
|
||||
self.join_conversation = True
|
||||
try:
|
||||
await self.on_join(data)
|
||||
except:
|
||||
print("Error:", ex)
|
||||
continue
|
||||
elif "@" + self.user["nick"] + " leave" in data.message:
|
||||
self.join_conversation = False
|
||||
try:
|
||||
await self.on_leave(data)
|
||||
except:
|
||||
print("Error:", ex)
|
||||
continue
|
||||
else:
|
||||
try:
|
||||
await self.on_message(data)
|
||||
except Exception as ex:
|
||||
print("Error:", ex)
|
||||
elif message.startswith("ping"):
|
||||
await self.on_ping(data.username, data.user_nick, data.channel_uid, data.message.lstrip("ping ").strip())
|
||||
elif any([
|
||||
"@" + self.user["nick"] + " join" in data.message,
|
||||
"@" + self.user["username"] + " join" in data.message]):
|
||||
self.joined.add(data.channel_uid)
|
||||
await self.on_join(data.channel_uid)
|
||||
elif any([
|
||||
"@" + self.user["nick"] + " leave" in data.message,
|
||||
"@" + self.user["username"] + " leave" in data.message]):
|
||||
self.joined.remove(data.channel_uid)
|
||||
await self.on_leave(data.channel_uid)
|
||||
elif "@" + self.user["nick"] in data.message or "@" + self.user["username"] in data.message:
|
||||
await self.on_mention(data.username, data.user_nick, data.channel_uid, data.message)
|
||||
else:
|
||||
await self.on_message(data.username, data.user_nick, data.channel_uid, data.message)
|
||||
except AttributeError:
|
||||
logger.debug("Event unhandled regarding message: " + data.message)
|
||||
except Exception as ex:
|
||||
logger.exception(ex)
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user