Hello I just want to use the async module on the first cogs into another by so that the code wont be too long for a single file and maximize the code reuse:
Module_cogs_1.py :
class Music_base(commands.Cog):
def __init__(self, bot):
self.bot = bot
# this is the module that to be imported
async def insert_player_song(self, ctx, track, track_index):
player = self.bot.lavalink.player_manager.get(ctx.guild.id)
player.add(requester=ctx.author.id, track=track, index=track_index)
Module_cogs_2.py :
class Module_test(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(aliases=['test'])
async def test_track(self, ctx, track_index :typing.Optional[int]=0, *, query:str):
# accessing the async module from Module_cogs_1.py
await Music_base.insert_player_song(self, ctx, track, track_index)
If imported locally i can used as await self.insert_player_song()
but when i try to import outside or in second file without self on the argument await Music_base.insert_player_song(ctx, track, track_index)
:
Traceback (most recent call last):
File "/home/vee/sauce-v3x/venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/home/vee/sauce-v3x/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/vee/sauce-v3x/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: insert_player_song() missing 1 required positional argument: 'track_index'
But when adding the self onto the argument await Music_base.insert_player_song(self, ctx, track, track_index)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/vee/sauce-v3x/venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/home/vee/sauce-v3x/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/home/vee/sauce-v3x/venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Music_base' object has no attribute 'guild_queues'
I hope you can understand guys and thank you so much in advance
Non-static methods can't be called without an instance of the class.
You should be able to get the created instance of the class using bot.get_cog
passing the name of the Cog.
Then you can use that instance to call the method.
music_base = self.bot.get_cog("Music_base")
music_base.func(ctx, ...)
If it is a command you want to run, you can also get the command with bot.get_command
, store it in a variable, and execute it as a function.