You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

from typing import List, Optional
from datetime import datetime
import os
import time
import asyncio
import random
from ircserver.factory import connected_websockets
from ircserver.factory import irc_bot as bot
import settings
msg_queue = asyncio.Queue()
messages = []
@bot.on('CLIENT_CONNECT')
async def connect(**kwargs):
bot.send('NICK', nick=settings.IRC_NICK)
bot.send('USER', user=settings.IRC_NICK, realname=settings.IRC_REALNAME)
# Don't try to join channels until server sent MOTD
done, pending = await asyncio.wait(
[bot.wait("RPL_ENDOFMOTD"), bot.wait("ERR_NOMOTD")],
loop=bot.loop,
return_when=asyncio.FIRST_COMPLETED
)
# Cancel whichever waiter's event didn't come in.
for future in pending:
future.cancel()
bot.send('JOIN', channel=settings.IRC_CHANNEL)
@bot.on('PING')
def keepalive(message, **kwargs):
bot.send('PONG', message=message)
@bot.on('client_disconnect')
def reconnect(**kwargs):
from quart import current_app as app
app.logger.warning("Lost IRC server connection")
time.sleep(3)
bot.loop.create_task(bot.connect())
app.logger.warning("Reconnecting to IRC server")
@bot.on('PRIVMSG')
async def message(nick, target, message, **kwargs):
if nick == settings.IRC_NICK:
return
if target == settings.IRC_NICK:
target = nick
msg = message
now = datetime.now()
now = time.mktime(now.timetuple())
data = {"nick": nick, "message": msg, "date": now}
for ws in connected_websockets:
await ws.put({"data": [data]})
messages.append(data)
if len(messages) > 20:
messages.pop(0)
def start():
bot.loop.create_task(bot.connect())