MIsskey-ebooks-bot/rdbot.py

69 lines
2.4 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-05-08 07:59:36 +00:00
from mipa.ext import commands, tasks
from mipa.router import Router
from mipac.models import Note
from mipac.util import check_multi_arg
2022-01-08 17:25:25 +00:00
2022-05-08 07:59:36 +00:00
import roboduck
2022-01-08 17:25:25 +00:00
2022-02-20 02:15:27 +00:00
# Load Misskey configuration
2022-05-08 08:25:55 +00:00
config = roboduck.configparser.ConfigParser()
config.read(roboduck.Path(__file__).parent.joinpath('bot.cfg'))
2022-05-08 07:59:36 +00:00
url = "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
try:
contentwarning = config.get("misskey", "cw")
if contentwarning.lower() == "none":
contentwarning = None
2022-05-08 08:25:55 +00:00
except (TypeError, ValueError, roboduck.configparser.NoOptionError):
2022-03-30 19:04:26 +00:00
contentwarning = None
2022-01-08 17:25:25 +00:00
2022-05-08 07:59:36 +00:00
if not check_multi_arg(url, token):
raise Exception("Misskey instance and token are required.")
2022-01-08 17:25:25 +00:00
class MyBot(commands.Bot):
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):
2022-05-08 08:25:55 +00:00
text = roboduck.create_sentence()
2022-05-08 07:59:36 +00:00
await bot.client.note.action.send(content=text, visibility="public", cw=contentwarning)
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-05-08 08:25:55 +00:00
thread_update = threading.Thread(target=roboduck.update())
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
2022-05-08 08:25:55 +00:00
await self.client.note.action.send(content=roboduck.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " Roboduck Bot started!",
2022-02-20 02:15:27 +00:00
visibility="specified")
self.loop_12h.start() # Launching renew posts every 12 hours
self.loop_1h.start() #
2022-05-08 08:25:55 +00:00
print(roboduck.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() + " "
2022-05-08 08:25:55 +00:00
text += roboduck.create_sentence()
2022-02-03 22:57:53 +00:00
2022-03-30 19:04:26 +00:00
await note.reply(content=text, cw=contentwarning) # Reply to a note
2022-01-08 17:25:25 +00:00
async def on_reconnect(self, ws):
await Router(ws).connect_channel(["global", "main"]) # Connect to global and main channels
2022-01-08 17:25:25 +00:00
if __name__ == "__main__":
2022-05-08 08:25:55 +00:00
databasepath = roboduck.Path(__file__).parent.joinpath('roboduck.db')
2022-02-03 22:16:27 +00:00
2022-05-08 08:25:55 +00:00
if not (roboduck.os.path.exists(databasepath) and roboduck.os.stat(databasepath).st_size != 0):
2022-06-16 21:46:05 +00:00
roboduck.init_bot()
2022-02-03 22:57:53 +00:00
2022-01-23 19:20:44 +00:00
bot = MyBot()
2022-05-08 07:59:36 +00:00
asyncio.run(bot.start(url, token))