skiqqy / C45DiscordBot

Proprietary™ (Thats a joke btw) C45 discord bot.
MIT License
2 stars 1 forks source link

Ability to add plugins #28

Closed DavidBakerEffendi closed 4 years ago

DavidBakerEffendi commented 4 years ago

Resolves #27

deavmi commented 4 years ago

Will do. Was for debugging and not intended to make it into master (not that I merged it yet anyways)

On 03 April 2020 09:11:15 SAST, David Baker Effendi notifications@github.com wrote:

@DavidBakerEffendi commented on this pull request.

There is a bit of unsafe code, please review my notes and make changes accordingly

@@ -0,0 +1,3 @@ +# TODO: This shadows built-in name exec, i.e. packages will get confused

Please take note that exec is a built-in function name. Please use another word.

>>> import builtins
>>> dir(builtins)
['ArithmeticError', 'AssertionError', 'AttributeError',
'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError',
'BytesWarning',
'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',
'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',
'EOFError',
'Ellipsis', 'EnvironmentError', 'Exception', 'False',
'FileExistsError',
'FileNotFoundError', 'FloatingPointError', 'FutureWarning',
'GeneratorExit', 'IOError',
'ImportError', 'ImportWarning', 'IndentationError', 'IndexError',
'InterruptedError', 'IsADirectoryError', 'KeyError',
'KeyboardInterrupt', 'LookupError',
'MemoryError', 'NameError', 'None', 'NotADirectoryError',
'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError',
'PendingDeprecationWarning',
'PermissionError', 'ProcessLookupError', 'RecursionError',
'ReferenceError',
'ResourceWarning', 'RuntimeError', 'RuntimeWarning',
'StopAsyncIteration',
'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',
'SystemExit',
'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError',
'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
'UnicodeTranslateError',
'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning',
'ZeroDivisionError', '_',
'__build_class__', '__debug__', '__doc__', '__import__', '__loader__',
'__name__',
'__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool',
'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile',
'complex',
'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod',
'enumerate', 'eval',
'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr',
'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int',
'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals',
'map',
'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord',
'pow',
'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round',
'set',
'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super',
'tuple',
'type', 'vars', 'zip']

@@ -0,0 +1,28 @@ +# Plugin loader +# +# Modular plugin loader +import importlib + +preloaded_plugins = [] + + +def load(config):

  • plugins_available = config["plugins"]
  • for plugin in plugins_available:
  • plugin_key = list(plugin.keys())[0]

Not particularly safe. Assumes that plugin.keys().length > 0. Also, isn't there a better way to do this?

@@ -0,0 +1,28 @@ +# Plugin loader +# +# Modular plugin loader +import importlib + +preloaded_plugins = [] + + +def load(config):

  • plugins_available = config["plugins"]
  • for plugin in plugins_available:
  • plugin_key = list(plugin.keys())[0]
  • plugin_data = plugin[plugin_key]
  • print(plugin_data)

Let's maybe cut down on the prints. Also could we add a prefix to the print? Such as [INFO], [WARN], [ERR]?

  • plugins_available = config["plugins"]
  • for plugin in plugins_available:
  • plugin_key = list(plugin.keys())[0]
  • plugin_data = plugin[plugin_key]
  • print(plugin_data)
  • print("Preloading plugin: " + str(plugin_key))
  • plugin_module = importlib.import_module("mods." + plugin_data["file"])
  • preloaded_plugins.append((plugin_module, plugin))
  • print("Loaded plugins into memory: " + str(preloaded_plugins))
  • +def find_plugin(command_string):

  • for plugin in preloaded_plugins:
  • plugin_name = list(plugin[1].keys())[0]

Again, this is not safe code as it assumes plugin.length > 1. Please check the lengths and print useful error messages accordingly so that the bot does not crash.

+ +import os + + +def load_modules(type_of):

  • Get a list of modules

  • modules = os.listdir(type_of)
  • commands = {}
  • for module in modules:
  • print("Module: " + str(module))
  • os.chdir(type_of + "/")
  • print(module.split(".")[0])
  • import importlib
  • print(eval("import pong"))

Please remove this lol. A lot of potentially unnecessary printing.

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/Skippy404/C45DiscordBot/pull/28#pullrequestreview-386981101

-- Sent from my Android device with K-9 Mail. Please excuse my brevity.

DavidBakerEffendi commented 4 years ago

@deavmi Mind testing the new changes on a live bot, please? If the functionality is good them we can merge

AndreiDreyer commented 4 years ago

Note that the plugin commands won't execute. Forgot to make the change somewhere else. Did this now on my command prefix branch to test it out there.