forked from frankplus/AntonBot
/
ircbot.py
executable file
·66 lines (55 loc) · 1.83 KB
/
ircbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python3
from pyrcb2 import IRCBot, Event
import bot
import threading
from timeloop import Timeloop
from datetime import timedelta
from config import CHANNEL, BOTNAME, IRC_SERVER_ADDRESS
import config
from apis import Miniflux
import asyncio
class MyBot:
def __init__(self):
self.bot = IRCBot(log_communication=True)
self.bot.load_events(self)
async def run(self):
async def init():
await self.bot.connect(IRC_SERVER_ADDRESS, 6667)
await self.bot.register(BOTNAME)
await self.bot.join(CHANNEL)
print("IRC bot connected", flush=True)
await self.rss_reader_loop()
await self.bot.run(init())
@Event.privmsg
async def on_privmsg(self, sender, channel, message):
if channel is None:
channel = sender
with open("chatlog.txt", 'a+') as f:
f.write(f"{sender}: {message}\n")
response = await bot.elaborate_query(channel, sender, message)
if response:
lines = response.split("\n")
for line in lines:
if line:
self.bot.privmsg(channel, line)
@Event.join
async def on_join(self, sender, channel):
response = bot.on_join(sender)
if response:
self.bot.privmsg(channel, response)
async def rss_reader_loop(self):
rss = Miniflux()
while True:
response = rss.get_new_entries()
if response:
lines = response.split("\n")
for channel in self.bot.channels:
for line in lines:
if line:
self.bot.privmsg(channel, line)
await asyncio.sleep(60)
async def main():
mybot = MyBot()
await mybot.run()
if __name__ == '__main__':
asyncio.run(main())