Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/core/bots/discord/bot.py

60 lines
2 KiB
Python
Raw Normal View History

2021-07-21 17:58:33 +00:00
import asyncio
2021-08-21 07:45:21 +00:00
import logging
2021-08-19 12:17:48 +00:00
import os
2021-07-21 17:58:33 +00:00
2021-07-26 12:43:51 +00:00
import discord
2021-08-07 07:56:48 +00:00
from config import Config
2021-07-21 17:58:33 +00:00
from core.bots.discord.client import client
2021-08-07 07:56:48 +00:00
from core.bots.discord.message import MessageSession, FetchTarget
2021-10-14 15:29:26 +00:00
from core.elements import MsgInfo, Session, Schedule, StartUp, PrivateAssets
2021-10-14 15:18:47 +00:00
from core.loader import ModulesManager
2021-07-21 17:58:33 +00:00
from core.logger import Logger
from core.parser.message import parser
from core.scheduler import Scheduler
2021-10-14 15:29:26 +00:00
from core.utils import init, load_prompt
2021-08-19 12:17:48 +00:00
PrivateAssets.set(os.path.abspath(os.path.dirname(__file__) + '/assets'))
2021-08-21 15:58:07 +00:00
init()
2021-08-03 16:04:59 +00:00
2021-08-23 16:08:21 +00:00
count = 0
2021-08-23 12:44:31 +00:00
2021-07-21 17:58:33 +00:00
@client.event
async def on_ready():
Logger.info('Logged on as ' + str(client.user))
2021-08-23 16:08:21 +00:00
global count
if count == 0:
gather_list = []
2021-10-14 15:18:47 +00:00
Modules = ModulesManager.return_modules_list_as_dict()
2021-08-23 16:08:21 +00:00
for x in Modules:
2021-10-14 14:49:12 +00:00
if isinstance(Modules[x], StartUp):
2021-08-23 16:08:21 +00:00
gather_list.append(asyncio.ensure_future(Modules[x].function(FetchTarget)))
2021-08-30 18:53:39 +00:00
if isinstance(Modules[x], Schedule):
Scheduler.add_job(func=Modules[x].function, trigger=Modules[x].trigger, args=[FetchTarget])
2021-08-23 16:08:21 +00:00
await asyncio.gather(*gather_list)
Scheduler.start()
logging.getLogger('apscheduler.executors.default').setLevel(logging.WARNING)
await load_prompt(FetchTarget)
count = 1
2021-07-21 17:58:33 +00:00
2021-07-24 08:59:15 +00:00
2021-07-21 17:58:33 +00:00
@client.event
async def on_message(message):
# don't respond to ourselves
if message.author == client.user:
return
2021-07-26 12:43:51 +00:00
Logger.info(str(message) + message.content)
2021-08-21 15:58:07 +00:00
target = "Discord|Channel"
2021-07-26 12:43:51 +00:00
if isinstance(message.channel, discord.DMChannel):
2021-08-21 15:58:07 +00:00
target = "Discord|DM|Channel"
2021-08-03 16:04:59 +00:00
msg = MessageSession(target=MsgInfo(targetId=f"{target}|{message.channel.id}",
2021-08-21 15:58:07 +00:00
senderId=f"Discord|Client|{message.author.id}",
senderName=message.author.name, targetFrom=target, senderFrom="Discord|Client"),
2021-08-03 16:04:59 +00:00
session=Session(message=message, target=message.channel, sender=message.author))
2021-07-21 17:58:33 +00:00
await parser(msg)
dc_token = Config('dc_token')
if dc_token:
2021-07-24 08:59:15 +00:00
client.run(dc_token)