Skip to content

Commit

Permalink
add ready_on, invoked, check events, $deletecommand
Browse files Browse the repository at this point in the history
  • Loading branch information
MZshnik committed Sep 1, 2024
1 parent 1c41de0 commit ccec4f7
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/MZscript/Functions/Message/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import add_reaction
from . import delete_command
from . import edit_message
from . import send_message
from . import send_message
52 changes: 52 additions & 0 deletions src/MZscript/Functions/Message/delete_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import disnake

from ...functions_handler import FunctionsHandler


class Functions(FunctionsHandler):
def __init__(self, handler):
super().__init__()
self.handler = handler
self.bot = handler.client.bot

async def func_deletecommand(self, ctx: disnake.Message, args: str):
"""
`$deletecommand[(delay;format)]`
### Example:
`$deletecommand[5;s]`
"""
args_list = await self.get_args(await self.is_have_functions(args, ctx))
if len(args_list) > 2:
error_msg = "$deletecommand: Too many args provided"
if self.handler.debug_console:
raise ValueError(error_msg)
await ctx.channel.send(error_msg)
return True
is_delete = None
if len(args_list) != 0:
try:
float(args_list[0])
except:
error_msg = f"$sendMessage: #deleteIn: First argument must be number: \"{args_list[0]}\""
if self.handler.debug_console:
raise ValueError(error_msg)
await ctx.channel.send(error_msg)
return True

if len(args_list) > 1:
if args_list[1] not in ['s', 'm', 'h', 'd']:
error_msg = f"$sendMessage: #deleteIn: Unsupported time format \"{args_list[1]}\". Select: s, m, h or d"
if self.handler.debug_console:
raise ValueError(error_msg)
await ctx.channel.send(error_msg)
return True
else:
args_list.append('s')
is_delete = int(args_list[0]) * {'s': 1, 'm': 60, 'h': 60*60, 'd': 60*60*24}[args_list[1]]
if is_delete:
await ctx.delete(delay=int(args_list[0]) * {'s': 1, 'm': 60, 'h': 60*60, 'd': 60*60*24}[args_list[1]])
else:
await ctx.delete()

def setup(handler):
return Functions(handler)
2 changes: 1 addition & 1 deletion src/MZscript/Functions/Message/send_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ async def reply_to(entry: str):
async def delete_in(entry: str):
args_splitted = await self.get_args(entry)
if len(args_splitted) > 2 or len(args_splitted) == 0:
error_msg = "$sendMessage: #вудуеу: Too many args provided."
error_msg = "$sendMessage: #deleteIn: Too many args provided."
if self.handler.debug_console:
raise ValueError(error_msg)
else:
Expand Down
6 changes: 6 additions & 0 deletions src/MZscript/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ async def on_interaction(self, inter: disnake.MessageInteraction):

async def on_slash(self, inter: disnake.MessageInteraction):
pass

async def invoked(self, command: dict):
pass

async def check(self, command: dict):
pass
2 changes: 1 addition & 1 deletion src/MZscript/functions_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self):
# checks
"$isadmin", "$ischannelexists", "$isguildexists", "$ismemberexists", "$isnumber", "$isroleexists", "$isuserexists",
# messages
"$sendmessage", "$editmessage", "$message", "$addreaction",
"$sendmessage", "$editmessage", "$message", "$addreaction", "$deletecommand",
# administration
"$addrole", "$removerole",
# moderation
Expand Down
63 changes: 52 additions & 11 deletions src/MZscript/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,28 @@ def __init__(
debug_log: bool = False,
debug_console: bool = True,
):
self.user_on_ready = on_ready
sync_commands = commands.CommandSyncFlags.default()
sync_commands.sync_commands_debug = debug_log
self.user_commands = []
self.user_command_names = []
self.plugins = []
self.user_commands: list = []
self.user_command_names: list = []
self.plugins: list[BasePlugin] = []
self.exec_on_start = []
self.user_slash_commands = []
self.user_events = {"message": None, "button": None, "interaction": None}
self.user_slash_commands: list = []
self.user_events: dict = {
"on_ready": on_ready,
"message": None,
"button": None,
"interaction": None,
"slash": None,
"invoked": {},
"check": {}
}
self.bot = commands.InteractionBot(
intents=self._get_intents(intents), command_sync_flags=sync_commands
)
self.funcs = FunctionsCore(self, db_warns, debug_log, debug_console)
self.funcs = FunctionsCore(
self, db_warns, debug_log, debug_console
)
self._register_listeners()

def _get_intents(self, entry: str):
Expand Down Expand Up @@ -237,8 +246,8 @@ async def on_ready(self):
## `on_ready` event
### Invoked when bot is ready to use
"""
if self.user_on_ready:
await self.run_code(self.user_on_ready)
if self.user_events["on_ready"]:
await self.run_code(self.user_events["on_ready"], disnake.MessageInteraction)
await self.update_commands()
for plugin in self.plugins:
await plugin.on_ready()
Expand All @@ -257,8 +266,17 @@ async def on_message(self, message: disnake.Message):
user_command = message.content.split(" ")[0]
for command_name, command_code in self.user_commands:
if user_command == command_name:
message.content = message.content[len(command_name)+1:]
await self.run_code(command_code, message)
command = {
"name": command_name,
"code": command_code,
"message": message,
"type": "command"
}
if await self.check(command):
message.content = message.content[len(command_name)+1:]
result = await self.run_code(command_code, message)
# await self.invoked(result, command_name, command_code, message)
await self.invoked(command)

async def on_button_click(self, inter: disnake.MessageInteraction):
"""
Expand Down Expand Up @@ -291,6 +309,29 @@ async def on_slash(self, inter: disnake.AppCmdInter):
if name == inter.application_command.name:
await self.run_code(code, inter)

async def invoked(self, command: dict): # TODO: Check event work
"""
## `invoked` event
Executed when command or slash of bot invoked
"""
for plugin in self.plugins:
await plugin.invoked(command)
for i in self.user_events["invoked"]:
await self.run_code(self.user_events["invoked"][i], command["message"])

async def check(self, command: dict): # TODO: Check event work
"""
## `check` event
Executed when command or slash of bot invoked. Uses for command check before executed
"""
for plugin in self.plugins:
if await plugin.check(command) == False:
return False
for i in self.user_events["check"]:
if await self.run_code(self.user_events["check"][i], command["message"]) == False:
return False
return True

def run(self, token: str):
"""
## Runs bot with provided token
Expand Down

0 comments on commit ccec4f7

Please sign in to comment.