MIsskey-ebooks-bot/rdbot.py

58 lines
1.8 KiB
Python
Raw Normal View History

2022-01-08 17:25:25 +00:00
import asyncio
import threading
2022-01-08 17:25:25 +00:00
2022-02-20 02:15:27 +00:00
from mi.ext import commands, tasks
from mi.framework import Note
from mi.framework.router import Router
2022-01-08 17:25:25 +00:00
2022-02-20 02:15:27 +00:00
from roboduck import *
2022-01-08 17:25:25 +00:00
2022-02-20 02:15:27 +00:00
# Load Misskey configuration
2022-01-08 17:25:25 +00:00
config = configparser.ConfigParser()
2022-02-20 02:15:27 +00:00
config.read(Path(__file__).parent.joinpath('bot.cfg'))
uri = "https://" + config.get("misskey", "instance_write")
2022-02-20 02:15:27 +00:00
token = config.get("misskey", "token")
2022-01-08 17:25:25 +00:00
class MyBot(commands.Bot):
2022-02-20 02:15:27 +00:00
text_model = None # Holds the markov object, so it won't be recreated everytime
2022-02-03 22:57:53 +00:00
2022-01-23 19:20:44 +00:00
def __init__(self):
2022-02-03 22:57:53 +00:00
super().__init__()
2022-01-08 17:25:25 +00:00
@tasks.loop(3600)
async def loop_1h(self):
text = create_sentence()
await bot.client.note.send(content=text)
2022-02-03 22:57:53 +00:00
2022-01-08 17:25:25 +00:00
@tasks.loop(43200)
async def loop_12h(self):
2022-02-20 02:15:27 +00:00
thread_update = threading.Thread(target=update)
2022-03-12 11:59:21 +00:00
thread_update.daemon = True
2022-02-20 02:15:27 +00:00
thread_update.start()
2022-01-08 17:25:25 +00:00
async def on_ready(self, ws):
2022-02-20 02:15:27 +00:00
await Router(ws).connect_channel(["global", "main"]) # Connect to global and main channels
await bot.client.note.send(content=datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " :roboduck: Bot started!",
visibility="specified")
self.loop_12h.start() # Launching renew posts every 12 hours
self.loop_1h.start() #
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " Roboduck Bot started!")
2022-02-03 22:57:53 +00:00
2022-01-08 17:25:25 +00:00
async def on_mention(self, note: Note):
2022-02-20 02:15:27 +00:00
if not note.author.is_bot:
text = note.author.action.get_mention() + " "
text += create_sentence()
2022-02-03 22:57:53 +00:00
2022-02-20 02:15:27 +00:00
await note.reply(content=text) # Reply to a note
2022-01-08 17:25:25 +00:00
if __name__ == "__main__":
2022-02-20 02:15:27 +00:00
databasepath = Path(__file__).parent.joinpath('roboduck.db')
2022-02-03 22:16:27 +00:00
2022-02-20 02:15:27 +00:00
if not (os.path.exists(databasepath) and os.stat(databasepath).st_size != 0):
init_bot()
2022-02-03 22:57:53 +00:00
2022-01-23 19:20:44 +00:00
bot = MyBot()
2022-01-24 19:41:56 +00:00
asyncio.run(bot.start(uri, token, timeout=600))