Rework spawning and task structure

Still not working: See TestBot.py
This commit is contained in:
Joscha 2017-09-04 16:21:29 +00:00
parent 34e1ae4b8f
commit 1c3b9d0a20
5 changed files with 252 additions and 93 deletions

View file

@ -6,41 +6,36 @@ from yaboli.utils import *
#class TestBot(Bot):
class TestBot(yaboli.Controller):
def __init__(self, roomname):
super().__init__(roomname)
async def on_snapshot(self, user_id, session_id, version, listing, log, nick=None,
pm_with_nick=None, pm_with_user_id=None):
await self.room.nick("TestBot")
def __init__(self, nick):
super().__init__(nick=nick)
async def on_send(self, message):
await self.room.send("Hey, a message!", message.message_id)
async def on_join(self, session):
if session.nick != "":
await self.room.send(f"Hey, a @{mention(session.nick)}!")
else:
await self.room.send("Hey, a lurker!")
async def on_nick(self, session_id, user_id, from_nick, to_nick):
if from_nick != "" and to_nick != "":
if from_nick == to_nick:
await self.room.send(f"You didn't even change your nick, @{mention(to_nick)} :(")
else:
await self.room.send(f"Bye @{mention(from_nick)}, hi @{mention(to_nick)}")
elif from_nick != "":
await self.room.send(f"Bye @{mention(from_nick)}? This message should never appear...")
elif to_nick != "":
await self.room.send(f"Hey, a @{mention(to_nick)}!")
else:
await self.room.send("I have no idea how you did that. This message should never appear...")
async def on_part(self, session):
if session.nick != "":
await self.room.send(f"Bye, you @{mention(session.nick)}!")
else:
await self.room.send("Bye, you lurker!")
if message.content == "!spawnevil":
bot = TestBot("TestSpawn")
task, reason = await bot.connect("test")
second = await self.room.send("We have " + ("a" if task else "no") + " task. Reason: " + reason, message.message_id)
if task:
await bot.stop()
await self.room.send("Stopped." if task.done() else "Still running (!)", second.message_id)
await self.room.send("All's over now.", message.message_id)
elif message.content == "!tree":
messages = [message]
newmessages = []
for i in range(2):
for m in messages:
for j in range(2):
newm = await self.room.send(f"{m.content}.{j}", m.message_id)
newmessages.append(newm)
messages = newmessages
newmessages = []
async def run_bot():
bot = TestBot("TestSummoner")
task, reason = await bot.connect("test")
if task:
await task
if __name__ == "__main__":
bot = TestBot("test")
asyncio.get_event_loop().run_until_complete(bot.run())
asyncio.get_event_loop().run_until_complete(run_bot())