2022-01-08 17:25:25 +00:00
|
|
|
import asyncio
|
2022-01-30 19:49:42 +00:00
|
|
|
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'))
|
2022-02-20 09:05:15 +00:00
|
|
|
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
|
|
|
|
2022-03-30 19:04:26 +00:00
|
|
|
contentwarning = config.get("misskey", "cw")
|
|
|
|
if contentwarning.lower() == "none":
|
|
|
|
contentwarning = None
|
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):
|
2022-01-30 19:49:42 +00:00
|
|
|
text = create_sentence()
|
2022-03-30 19:04:26 +00:00
|
|
|
await bot.client.note.send(content=text, visibility="home", 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-02-20 02:15:27 +00:00
|
|
|
thread_update = threading.Thread(target=update)
|
2022-03-11 16:29:15 +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:
|
2022-02-20 09:05:15 +00:00
|
|
|
text = note.author.action.get_mention() + " "
|
2022-01-30 19:49:42 +00:00
|
|
|
text += 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
|
|
|
|
2022-03-26 16:50:22 +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-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):
|
2022-01-30 19:49:42 +00:00
|
|
|
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))
|