robertwayne / cogwatch

Hot-reloading for discord.py-based command files.
MIT License
44 stars 6 forks source link

Update cogwatch.py to support disnake library #27

Closed Tdarnell closed 1 year ago

Tdarnell commented 1 year ago

Added support for disnake and raise an error specifying that no forks have been found.

robertwayne commented 1 year ago

Thanks for the PR!

I'm happy to add this, though I can see the import code is starting to look gnarly with multiple libs supported. I'm thinking we should consider something like this:

from importlib import import_module     

supported_libraries = ['discord.py', 'nextcord', '...etc', '...etc']
for library_name in supported_libraries:
    try:
        library = import_module(library_name)
    except:
        raise ImportError("Could not find discord.py or another supported library.")
    else:
        globals()[library_name] = library
        break

I haven't tested this, so unsure if it behaves as I'd expect.

I'm busy for a couple more days, so if you want to take a stab at this and see if it works - I'm all for it.

If not, I'll merge and take a shot at it later this week! :D

Tdarnell commented 1 year ago

The following worked for disnake, worth noting I moved the block to be after the logger definition so it can be used in here.

# import the first available discord.py library, preferring discord.py over nextcord ect.
supported_libraries = ['discord.py', 'nextcord', 'disnake', 'pycord']
for library_name in supported_libraries:
    try:
        library = import_module(library_name)
        commands = import_module(f'{library_name}.ext.commands')
    except ImportError:
        logger.debug(f'Could not find {library_name} library, passing...')
        pass
    except Exception as e:
        logger.error(f'Failed to import {library_name} library, please report this error.')
        raise e
    else:
        globals()[library_name] = library
        globals()[f'{library_name}.ext.commands'] = commands
        break
else:
    raise ImportError("Could not find discord.py or another supported library, please install one of the following:\n"
                      + '\n'.join(supported_libraries))
robertwayne commented 1 year ago

Looks great, thanks!

I'll do some tests against the other libraries to make sure everything is working and push a new release.