v1.1
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from .plugins import Plugin, MethodWithPriority
|
||||
from .message import Message, MessageContext, Attachment
|
||||
from .event import Event, ChatEvent
|
||||
|
@@ -1,11 +1,10 @@
|
||||
from kurocore.utils.vk_utils import generate_random_id
|
||||
from kurocore.vk.utils import generate_random_id
|
||||
|
||||
|
||||
class ChatEvent:
|
||||
__slots__ = ('session', 'api', 'raw', 'type', 'member_id', 'text', 'photo')
|
||||
|
||||
def __init__(self, session, api, raw: dict):
|
||||
self.session = session
|
||||
def __init__(self, api, raw: dict):
|
||||
self.api = api
|
||||
self.raw: dict = raw
|
||||
|
||||
@@ -18,8 +17,7 @@ class ChatEvent:
|
||||
class Event:
|
||||
__slots__ = ('session', 'api', 'raw', 'type')
|
||||
|
||||
def __init__(self, session, api, raw: dict):
|
||||
self.session = session
|
||||
def __init__(self, api, raw: dict):
|
||||
self.api = api
|
||||
|
||||
self.raw: dict = raw['object']
|
||||
@@ -35,7 +33,7 @@ class Event:
|
||||
if text:
|
||||
data.update({'message': text})
|
||||
if attachments:
|
||||
if type(attachments) == str:
|
||||
if type(attachments) is str:
|
||||
data.update({'attachment': attachments})
|
||||
else:
|
||||
data.update({'attachment': ','.join(attachments)})
|
||||
|
@@ -4,11 +4,11 @@ import traceback
|
||||
from aiohttp.web import run_app
|
||||
from sentry_sdk import init as sentry_init, capture_exception, set_user
|
||||
|
||||
from kurocore.logger import BotLogger
|
||||
from kurocore.vk import VK, VKLongPoll, VkBotEventType
|
||||
from .event import ChatEvent, Event
|
||||
from kurocore.utils.database.database import Database
|
||||
from kurocore.utils.vk.longpoll import VKLongPoll, VkBotEventType
|
||||
from kurocore.utils.vk.vk import VK
|
||||
from ..logger import BotLogger
|
||||
from .message import MessageContext, Message
|
||||
from kurocore.database import Database
|
||||
|
||||
|
||||
class Handler:
|
||||
@@ -145,7 +145,10 @@ class Handler:
|
||||
for before_process in p.before_process_methods:
|
||||
before_process.call()
|
||||
|
||||
await p._process_command(command, msg, args)
|
||||
ctx = MessageContext(self.api, msg, None)
|
||||
if Database.db:
|
||||
ctx.db = Database
|
||||
await p._process_command(command, ctx, args)
|
||||
return
|
||||
except Exception as e:
|
||||
if self.config.debug:
|
||||
@@ -175,9 +178,9 @@ class Handler:
|
||||
if not await self.check_payload(msg):
|
||||
await self.check_command(msg)
|
||||
|
||||
# if db:
|
||||
# db.close()
|
||||
# BotLogger.log.debug('Connection closed!')
|
||||
if db:
|
||||
db.close()
|
||||
BotLogger.log.debug('Connection closed!')
|
||||
|
||||
async def check_event(self, event: (ChatEvent, Event), msg):
|
||||
event_type = event.type
|
||||
@@ -220,23 +223,22 @@ class Handler:
|
||||
# self.shutdown()
|
||||
|
||||
async def handle_event(self, event):
|
||||
from kurocore import Message
|
||||
if ((event.type == VkBotEventType.MESSAGE_NEW and 'action' not in event.obj)
|
||||
or event.type == VkBotEventType.MESSAGE_EVENT):
|
||||
msg = Message(self.session, self.api, event.obj)
|
||||
msg = Message(self.api, event.obj)
|
||||
if msg.user_id > 0:
|
||||
set_user({'id': msg.user_id})
|
||||
await self.check(msg)
|
||||
|
||||
elif event.type == VkBotEventType.MESSAGE_NEW and 'action' in event.obj:
|
||||
e = ChatEvent(self.session, self.api, event.obj['action'])
|
||||
msg = Message(self.session, self.api, event.obj)
|
||||
e = ChatEvent(self.api, event.obj['action'])
|
||||
msg = Message(self.api, event.obj)
|
||||
if msg.user_id > 0:
|
||||
set_user({'id': msg.user_id})
|
||||
await self.check_event(e, msg)
|
||||
|
||||
else:
|
||||
e = Event(self.session, self.api, event.raw)
|
||||
e = Event(self.api, event.raw)
|
||||
await self.check_event(e, None)
|
||||
|
||||
async def _run(self):
|
||||
|
@@ -2,9 +2,9 @@ import json
|
||||
from enum import Enum
|
||||
from typing import Union, Type, Tuple
|
||||
|
||||
from kurocore.utils.vk.keyboard import VkKeyboard
|
||||
from kurocore.utils.vk.vk import VKApiException
|
||||
from kurocore.utils.vk_utils import generate_random_id
|
||||
from kurocore.database import Database
|
||||
from kurocore.vk import VkKeyboard, VKApiException, VK
|
||||
from kurocore.vk.utils import generate_random_id
|
||||
|
||||
|
||||
class MessageArgs(dict):
|
||||
@@ -137,41 +137,6 @@ class Document(VkObject):
|
||||
Attachment = Type[Photo], Type[Document], Type[Sticker]
|
||||
|
||||
|
||||
def load_attachments(raw: list[dict]) -> list[Attachment]:
|
||||
attachments = []
|
||||
for attachment in raw:
|
||||
match attachment['type']:
|
||||
case 'photo':
|
||||
attachments.append(Photo(attachment))
|
||||
case 'doc':
|
||||
attachments.append(Document(attachment))
|
||||
case 'sticker':
|
||||
attachments.append(Sticker(attachment))
|
||||
case _:
|
||||
attachments.append(VkObject(attachment))
|
||||
return attachments
|
||||
|
||||
|
||||
def dump_attachments(raw_attachments: list[Attachment]) -> str:
|
||||
return ','.join(map(repr, raw_attachments))
|
||||
|
||||
|
||||
class EventActions:
|
||||
SHOW_SNACKBAR = 'show_snackbar'
|
||||
OPEN_LINK = 'open_link'
|
||||
OPEN_APP = 'open_app'
|
||||
|
||||
|
||||
class MessageID:
|
||||
def __init__(self, body):
|
||||
if type(body) is dict:
|
||||
self.message_id = body.get('message_id', 0)
|
||||
self.cmid = body.get('cmid', 0)
|
||||
else:
|
||||
self.message_id = body
|
||||
self.cmid = body
|
||||
|
||||
|
||||
class Message(VkObject):
|
||||
__slots__ = (
|
||||
'vk', 'api', 'raw', 'id', 'conversation_message_id', 'cmid',
|
||||
@@ -181,10 +146,9 @@ class Message(VkObject):
|
||||
'meta'
|
||||
)
|
||||
|
||||
def __init__(self, vk, api, raw):
|
||||
def __init__(self, api, raw):
|
||||
super().__init__(raw)
|
||||
|
||||
self.vk = vk
|
||||
self.api = api
|
||||
|
||||
if type(raw) is Message:
|
||||
@@ -218,8 +182,7 @@ class Message(VkObject):
|
||||
self.event_id: str = self.raw.get('event_id', '')
|
||||
|
||||
self.forwarded_messages: list = self.raw.get('fwd_messages', [])
|
||||
self.reply_message = Message(self.vk, self.api, self.raw['reply_message']) \
|
||||
if 'reply_message' in self.raw else None
|
||||
self.reply_message = Message(self.api, self.raw['reply_message']) if 'reply_message' in self.raw else None
|
||||
|
||||
self.meta: dict = {}
|
||||
|
||||
@@ -325,7 +288,56 @@ class Message(VkObject):
|
||||
'conversation_message_ids': [cmid]
|
||||
}
|
||||
res = await self.api.messages.getByConversationMessageId(**data)
|
||||
return Message(self.vk, self.api, res['items'][0])
|
||||
return Message(self.api, res['items'][0])
|
||||
|
||||
def __repr__(self):
|
||||
return str(self.raw)
|
||||
|
||||
|
||||
class MessageContext:
|
||||
__slots__ = ('api', 'msg', 'db', 'args', 'meta')
|
||||
|
||||
def __init__(self, api, msg, db):
|
||||
self.api: VK = api
|
||||
self.msg: Message = msg
|
||||
self.db: Database = db
|
||||
self.args = None
|
||||
self.meta = {}
|
||||
|
||||
def __repr__(self):
|
||||
return f'<MessageContext{self.msg}({self.args})>'
|
||||
|
||||
|
||||
def load_attachments(raw: list[dict]) -> list[Attachment]:
|
||||
attachments = []
|
||||
for attachment in raw:
|
||||
match attachment['type']:
|
||||
case 'photo':
|
||||
attachments.append(Photo(attachment))
|
||||
case 'doc':
|
||||
attachments.append(Document(attachment))
|
||||
case 'sticker':
|
||||
attachments.append(Sticker(attachment))
|
||||
case _:
|
||||
attachments.append(VkObject(attachment))
|
||||
return attachments
|
||||
|
||||
|
||||
def dump_attachments(raw_attachments: list[Attachment]) -> str:
|
||||
return ','.join(map(repr, raw_attachments))
|
||||
|
||||
|
||||
class EventActions:
|
||||
SHOW_SNACKBAR = 'show_snackbar'
|
||||
OPEN_LINK = 'open_link'
|
||||
OPEN_APP = 'open_app'
|
||||
|
||||
|
||||
class MessageID:
|
||||
def __init__(self, body):
|
||||
if type(body) is dict:
|
||||
self.message_id = body.get('message_id', 0)
|
||||
self.cmid = body.get('cmid', 0)
|
||||
else:
|
||||
self.message_id = body
|
||||
self.cmid = body
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import inspect
|
||||
import re
|
||||
|
||||
from kurocore.main.message import MessageArgs
|
||||
|
||||
|
||||
class MethodWithPriority:
|
||||
__slots__ = ('priority', 'method')
|
||||
@@ -160,12 +161,10 @@ class Plugin:
|
||||
self.tasks.append(f)
|
||||
return f
|
||||
|
||||
async def _process_command(self, command: str, msg, args):
|
||||
sig = inspect.signature(self.commands[command])
|
||||
if len(sig.parameters) == 1:
|
||||
await self.commands[command](msg)
|
||||
elif len(sig.parameters) == 2:
|
||||
await self.commands[command](msg, args)
|
||||
async def _process_command(self, command: str, ctx, args):
|
||||
# sig = inspect.signature(self.commands[command])
|
||||
ctx.args = args
|
||||
await self.commands[command](ctx)
|
||||
|
||||
async def _process_payload(self, payload: str, msg):
|
||||
await self.payloads[payload](msg)
|
||||
@@ -180,7 +179,6 @@ class Plugin:
|
||||
return command in self.admin_commands
|
||||
|
||||
async def _validate_command_args(self, command: str, cmd_args: tuple):
|
||||
from kurocore import MessageArgs
|
||||
commands_args = self.commands_args
|
||||
if command not in commands_args:
|
||||
return True, MessageArgs({})
|
||||
@@ -215,7 +213,6 @@ class Plugin:
|
||||
return True, MessageArgs(args)
|
||||
|
||||
async def _validate_payload_args(self, payload: str, msg_args: dict):
|
||||
from kurocore import MessageArgs
|
||||
payloads_args = self.payloads_args
|
||||
if payload not in payloads_args:
|
||||
return True, None
|
||||
|
Reference in New Issue
Block a user