我想要在外部直接用函數來呼叫其中的_join
元素
MusicBot-by vbe0201|GitHub
目前的程式碼:
import discord
from discord.ext import commands
from music_bot_example import Music
def reply_handler(bot, ctx):
obj1 = Music(bot)
class obj2(obj1, ctx):
pass
obj1.bot.loop.create_task(obj1.core_join(obj2))
class Chat(Cog_Extension):
@commands.Cog.listener()
async def on_message(self, ctx):
reply_handler(self.bot, ctx)
def setup(bot):
bot.add_cog(Chat(bot))
並且將 _join
替換成一個裸函數以供呼叫:
async def core_join(self, ctx: commands.Context):
destination = ctx.author.voice.channel
if ctx.voice_state.voice:
await ctx.voice_state.voice.move_to(destination)
return
ctx.voice_state.voice = await destination.connect()
@commands.command(name='join', invoke_without_subcommand=True)
async def _join(self, ctx: commands.Context):
"""Joins a voice channel."""
return self.core_join(ctx)
error
in reply_handler
class Object(Music(bot), ctx):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
請各位高手指點 Orz
不知這樣是否可行
class Music(commands.Cog):
... ... ...
async def invoke_join(self):
await self.bot.get_command('_join').callback()
class Chat(Cog_Extension):
... ... ...
async def bkgd_task(self):
await self.bot.wait_until_ready()
music = self.bot.get_cog('Music')
if music is not None:
await music.invoke_join()
@commands.Cog.listener()
async def on_message(self, ctx):
self.bot.loop.create_task(bkgd_task())