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