Skip to content

Commit

Permalink
forgot to update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
showierdata9978 committed Jun 17, 2023
1 parent d0c5e2f commit 7e4890c
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 43 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"python.formatting.provider": "black",
"python.formatting.provider": "none",
"python.linting.banditEnabled": true,
"python.linting.enabled": true
"python.linting.enabled": true,
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
}
}
22 changes: 17 additions & 5 deletions MeowerBot/Bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import threading
import shlex

from .cloudlink import CloudLink
from .cl import CloudLink
import sys

import json
Expand Down Expand Up @@ -145,6 +145,15 @@ def __handle_error__(self, e):
self.__handle_close__()
return

if (type(e)) == KeyboardInterrupt:
#kill all bot threads
self.bad_exit = True

self.wss = None # effectively kill the bot
self.__handle_close__( )
return





Expand All @@ -154,16 +163,18 @@ def _debug_fix(self, packet):

try:
self.__handle_packet__(packet)
except Exception as e: # cq: skip #IDC ABOUT GENERAL EXCP

except BaseException as e: # cq: skip #IDC ABOUT GENERAL EXCP
self.__handle_error__(e)
self.logger.error(traceback.format_exc())
self.run_cb("error", args=(e, ))

try:
self.run_cb("__raw__", args=(packet, )) # raw packets
except Exception as e: # cq: skip #IDC ABOUT GENERAL EXCP
except BaseException as e: # cq: skip #IDC ABOUT GENERAL EXCP
self.__handle_error__(e)
self.logger.error(traceback.format_exc())
self.run_cb("error", args=(e, ))


def __handle_on_connect__(self):
self.wss.sendPacket(
Expand Down Expand Up @@ -240,7 +251,8 @@ def _handle_status(self, status, listener):
)
print("CRITICAL ERROR! ACCOUNT SOFTLOCKED!!!!.", file=sys.__stdout__)
self.bad_exit = True
self.wss.stop()
del self.wss

return

if status != "I:100 | OK":
Expand Down
File renamed without changes.
35 changes: 24 additions & 11 deletions MeowerBot/Cloudlink/cloudlink.py → MeowerBot/cl/cloudlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ def client(self, ip="ws://127.0.0.1:3000/"): # Runs CloudLink in client mode.

# Run the client
self.wss.run_forever()
except Exception as e:
except BaseException as e:
self.logging.error(f"Error at client: {e}")
self._on_error_client(self.wss, e)

def stop(self, abrupt=False): # Stops CloudLink (not sure if working)
self.wss.close()
Expand All @@ -94,8 +95,9 @@ def callback(
self.logging.error(
f"Error: Callback {callback_id} is not a valid callback id!"
)
except Exception as e:
except BaseException as e:
self.logging.error(f"Error at callback: {e}")
self._on_error_client(self.wss, e)

def sendPacket(
self, msg
Expand All @@ -104,8 +106,9 @@ def sendPacket(

self.logging.debug(f"Sending {json.dumps(msg)}")
self.wss.send(json.dumps(msg))
except Exception as e:
except BaseException as e:
self.logging.error(f"Error on sendPacket (client): {e}")
self._on_error_client(self.wss, e)

def getUsernames(self): # Returns the username list.
return self.statedata["ulist"]["usernames"]
Expand Down Expand Up @@ -149,12 +152,15 @@ def _on_connection_client(self, ws): # Client-side connection handler
def run(*args):
try:
self.callback_function["on_connect"]()
except Exception as e:
except BaseException as e:
self.logging.error(f"Error on _on_connection_client: {e}")
self._on_error_client(self.wss, error)

threading.Thread(target=run).start()
except Exception as e:
except BaseException as e:
self.logging.info(f"Error on _on_connection_client: {e}")
self._on_error_client(self.wss, e)


def _on_packet_client(self, ws, message): # Client-side packet handler
try:
Expand All @@ -177,13 +183,15 @@ def _on_packet_client(self, ws, message): # Client-side packet handler
def run(*args):
try:
self.callback_function["on_packet"](message)
except Exception as e:
except BaseException as e:

self.logging.error(f"Error on _on_packet_client: {e}")
self._on_error_client(self.wss, e)

threading.Thread(target=run).start()
except Exception as e:
except BaseException as e:
self.logging.error(f"Error on _on_packet_client: {e}")
self._on_error_client(self.wss, e)

def _on_error_client(self, ws, error): # Client-side error handler
try:
Expand All @@ -194,13 +202,16 @@ def _on_error_client(self, ws, error): # Client-side error handler
def run(*args):
try:
self.callback_function["on_error"](error)
except Exception as e:
except BaseException as e:
self.logging.error(f"Error on _on_error_client: {e}")

self._on_error_client(self.wss, e)

threading.Thread(target=run).start()
except Exception as e:
except BaseException as e:

self.logging.error(f"Error on _on_error_client: {e}")
self._on_error_client(self.wss, e)

def _closed_connection_client(
self, ws, close_status_code, close_msg
Expand All @@ -215,10 +226,12 @@ def _closed_connection_client(
def run(*args):
try:
self.callback_function["on_close"]()
except Exception as e:
except BaseException as e:

self.logging.error(f"Error on _closed_connection_client: {e}")
self._on_error_client(self.wss, e)

threading.Thread(target=run).start()
except Exception as e:
except BaseException as e:
self.logging.error(f"Error on _closed_connection_client: {e}")
self._on_error_client(self.wss, e)
48 changes: 38 additions & 10 deletions MeowerBot/command.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warnings
import inspect

import traceback
from logging import getLogger

logger = getLogger("MeowerBot")
Expand All @@ -9,16 +9,41 @@
class AppCommand:
connected = None

def __init__(self, func, name=None, args=0):
def __init__(self, func, name=None, args=0, is_subcommand=False):
if name is None:
name = func.__name__

self.name = name
self.func = func
self.args = args
self.args_num = args

spec = inspect.signature(func)

# Get the names and annotations of the arguments
self.args = [
(param.name, param.annotation)
for param in spec.parameters.values()
if param.name not in ["self", "ctx"]
]

# Check if the function has an arbitrary number of positional arguments
self.has_unamed_args = any(
param.kind == inspect.Parameter.VAR_POSITIONAL for param in spec.parameters.values()
)

self.is_subcommand = is_subcommand

# Get the names, annotations, and default values of the keyword-only arguments
self.optional_args = [
(param.name, param.annotation, param.default)
for param in spec.parameters.values()
if param.kind == inspect.Parameter.KEYWORD_ONLY
]

# Set the namespace based on whether the command is a subcommand or not
self.namespace = self.is_subcommand if type(self.is_subcommand) is str else self.name


self.arg_names = inspect.getfullargspec(func)[0]
self.arg_types = func.__annotations__
self.subcommands = {}

def __call__(self, *args):
Expand All @@ -31,6 +56,7 @@ def register_class(self, con):
subcommand.register_class(con)



def subcommand(self, name=None, args=0):
def inner(func):

Expand All @@ -55,10 +81,10 @@ def run_cmd(self, ctx, *args):
logger.debug(f"Cant find subcommand {args[0]}")

except IndexError:
logger.debug(f"IndexError: {args}")
logger.debug(traceback.format_exc())

if not self.args == 0:
args = args[: self.args]
if not self.args_num == 0:
args = args[:self.args_num]

if self.connected is None:
self.func(ctx, *args)
Expand All @@ -69,8 +95,10 @@ def info(self):
return {
self.name: {
"args": self.args,
"arg_names": self.arg_names,
"arg_types": self.arg_types,
"args_num": self.args_num,
"optional_args": self.optional_args,
"has_unamed_args": self.has_unamed_args,
"subcommands": self.subcommands,
"command": self,
"func": self.func,

Expand Down
84 changes: 84 additions & 0 deletions MeowerBot/ext/help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from MeowerBot.cog import Cog
from MeowerBot.command import command
import inspect

def _get_index_or(l, i, d):
try:
r = l[i]
if str(r) == str(inspect._empty):
return d
return r
except IndexError:
return d

class Help(Cog):
def __init__(self, bot, *args, **kwargs):
self.bot = bot
self.page = ""
Cog.__init__(self)



def generate_help(self):
self.pages = []
self.page = ""
page_size = 0
for name, cog in self.bot.cogs.items():

self.page+= f"-- [ {name} ] --\n"
page_size = len(self.page)

for command in cog.__commands__.values():
self.handle_command(command["command"].name, command["command"])

if page_size >= 500:
self.pages.append(page)
self.page = f"-- [ {name} ] --\n"
page_size = len(self.page)

self.page += "-- [ Unsorted ] --\n"
page_size = len(self.page)



for name, comamnd in self.bot.commands.items():
if comamnd["command"].connected is not None: continue #skip cog based commands

self.handle_command(name, comamnd["command"])

if page_size >= 500:
self.pages.append(page)
self.page = f"-- [ Unsorted ] --\n"
page_size = len(self.page)

self.pages.append(self.page)


def handle_command(self, name, cmd):
self.page += (f"{self.bot.prefix}{name} ")

for arg in cmd.args:
self.page += f"<{arg[0][0]}: {str(_get_index_or(arg, 1, 'any'))}> "


for arg in cmd.optional_args:
self.page += f"[{arg[0]}: {str(_get_index_or(arg, 1, 'any'))}: optional ] "

self.page += "\n"

for subcommand_name, command in cmd.subcommands.items():
self.page += f"\t"
self.handle_command(f"{name} {subcommand_name}", command["command"])
self.page += "\n"

self.page += "\n"

@command(name="help")
def help(self, ctx, page: int=0):

if page >= len(self.pages):
page = len(self.pages) - 1

ctx.send_msg(self.pages[page])


Loading

0 comments on commit 7e4890c

Please sign in to comment.