2022-08-16 18:53:43 +00:00
|
|
|
import importlib
|
2021-08-19 12:17:48 +00:00
|
|
|
import os
|
2022-07-29 10:55:20 +00:00
|
|
|
import re
|
2022-08-16 18:53:43 +00:00
|
|
|
import traceback
|
2021-07-21 17:58:33 +00:00
|
|
|
|
2021-07-26 12:43:51 +00:00
|
|
|
import discord
|
|
|
|
|
2022-06-12 07:07:53 +00:00
|
|
|
from bots.discord.client import client
|
|
|
|
from bots.discord.message import MessageSession, FetchTarget
|
2021-08-07 07:56:48 +00:00
|
|
|
from config import Config
|
2023-02-05 14:33:33 +00:00
|
|
|
from core.builtins import PrivateAssets, Url
|
2021-07-21 17:58:33 +00:00
|
|
|
from core.logger import Logger
|
|
|
|
from core.parser.message import parser
|
2023-02-05 14:33:33 +00:00
|
|
|
from core.types import MsgInfo, Session
|
2023-03-01 13:41:42 +00:00
|
|
|
from core.utils.bot import init_async, load_prompt
|
2021-08-19 12:17:48 +00:00
|
|
|
|
|
|
|
PrivateAssets.set(os.path.abspath(os.path.dirname(__file__) + '/assets'))
|
2022-06-12 07:07:53 +00:00
|
|
|
Url.disable_mm = True
|
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-11-12 14:25:53 +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:
|
2023-03-01 13:41:42 +00:00
|
|
|
await init_async()
|
2021-08-23 16:08:21 +00:00
|
|
|
await load_prompt(FetchTarget)
|
|
|
|
count = 1
|
2021-07-21 17:58:33 +00:00
|
|
|
|
2021-07-24 08:59:15 +00:00
|
|
|
|
2022-08-16 18:53:43 +00:00
|
|
|
slash_load_dir = os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'slash'))
|
|
|
|
|
|
|
|
|
|
|
|
def load_slashcommands():
|
|
|
|
fun_file = None
|
|
|
|
dir_list = os.listdir(slash_load_dir)
|
|
|
|
for file_name in dir_list:
|
|
|
|
try:
|
|
|
|
file_path = os.path.join(slash_load_dir, file_name)
|
|
|
|
fun_file = None
|
|
|
|
if os.path.isdir(file_path):
|
|
|
|
if file_name[0] != '_':
|
|
|
|
fun_file = file_name
|
|
|
|
elif os.path.isfile(file_path):
|
|
|
|
if file_name[0] != '_' and file_name.endswith('.py'):
|
|
|
|
fun_file = file_name[:-3]
|
|
|
|
if fun_file is not None:
|
|
|
|
Logger.info(f'Loading slash.{fun_file}...')
|
|
|
|
modules = 'bots.discord.slash.' + fun_file
|
|
|
|
importlib.import_module(modules)
|
|
|
|
Logger.info(f'Succeeded loaded bots.discord.slash.{fun_file}!')
|
|
|
|
except:
|
|
|
|
tb = traceback.format_exc()
|
|
|
|
errmsg = f'Failed to load bots.discord.slash.{fun_file}: \n{tb}'
|
|
|
|
Logger.error(errmsg)
|
|
|
|
|
|
|
|
|
|
|
|
load_slashcommands()
|
2022-08-09 16:38:03 +00:00
|
|
|
|
|
|
|
|
2021-07-21 17:58:33 +00:00
|
|
|
@client.event
|
|
|
|
async def on_message(message):
|
|
|
|
# don't respond to ourselves
|
2022-09-14 13:05:52 +00:00
|
|
|
if message.author == client.user or message.author.bot:
|
2021-07-21 17:58:33 +00:00
|
|
|
return
|
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"
|
2022-06-17 05:59:15 +00:00
|
|
|
targetId = f"{target}|{message.channel.id}"
|
2022-06-28 06:11:03 +00:00
|
|
|
replyId = None
|
|
|
|
if message.reference is not None:
|
|
|
|
replyId = message.reference.message_id
|
2022-07-29 10:55:20 +00:00
|
|
|
prefix = None
|
2022-07-29 10:56:07 +00:00
|
|
|
if match_at := re.match(r'^<@(.*?)>', message.content):
|
2022-07-29 10:55:20 +00:00
|
|
|
if match_at.group(1) == str(client.user.id):
|
|
|
|
prefix = ['']
|
2022-07-29 10:56:07 +00:00
|
|
|
message.content = re.sub(r'<@(.*?)>', '', message.content)
|
2022-06-28 06:11:03 +00:00
|
|
|
|
2022-06-17 05:59:15 +00:00
|
|
|
msg = MessageSession(target=MsgInfo(targetId=targetId,
|
2021-08-21 15:58:07 +00:00
|
|
|
senderId=f"Discord|Client|{message.author.id}",
|
2022-06-12 14:30:02 +00:00
|
|
|
senderName=message.author.name, targetFrom=target, senderFrom="Discord|Client",
|
2022-06-28 06:11:03 +00:00
|
|
|
clientName='Discord',
|
|
|
|
messageId=message.id,
|
|
|
|
replyId=replyId),
|
2021-08-03 16:04:59 +00:00
|
|
|
session=Session(message=message, target=message.channel, sender=message.author))
|
2022-07-29 10:55:20 +00:00
|
|
|
await parser(msg, prefix=prefix)
|
2021-07-21 17:58:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
dc_token = Config('dc_token')
|
|
|
|
if dc_token:
|
2021-07-24 08:59:15 +00:00
|
|
|
client.run(dc_token)
|