首页 文章

为什么我的discord bot只执行一次和一次命令?

提问于
浏览
0

我正在完成一个简单的声音片段Discord bot我通过回收python中的基本音乐机器人示例 . 我想让机器人做的就是进入调用命令的用户的语音通道(!womble),从声音片段的文件夹中播放随机声音片段,然后离开语音通道 .

“简单,对吧?”当然不是,显然不是这个API .

经过BUNCH的试验和错误,至少看了3个API修订版,我让机器人实际执行命令.....一次 . 蟋蟀会遇到命令的任何进一步提示 . 我可以做一个!召唤并将机器人带入通道,但是!womble命令不再有效 .

def bot_leave(self, ctx):
    state = self.get_voice_state(ctx.message.server)
    coro = state.voice.disconnect()
    fut = asyncio.run_coroutine_threadsafe(coro, state.voice.loop)
    try:
        fut.result()
    except:
        # an error happened sending the message
        pass

@commands.command(pass_context=True, no_pm=True)
async def womble(self, ctx):
    state = self.get_voice_state(ctx.message.server)
    opts = {
        'default_search': 'auto',
        'quiet': True,
    }

    if state.voice is None:
        success = await ctx.invoke(self.summon)
        if not success:
            return

    try:
        random_clip = clip_dir + "\\" + random.choice(os.listdir(clip_dir))
        player = state.voice.create_ffmpeg_player(random_clip, after=lambda: self.bot_leave(ctx))
        player.start()
    except Exception as e:
        fmt = 'An error occurred while processing this request: ```py\n{}: {}\n```'
        await self.bot.send_message(ctx.message.channel, fmt.format(type(e).__name__, e))

我试过进入python聊天是Discord API服务器,但很像我的机器人,我遇到了蟋蟀 . (猜猜这就是我试图通过已经进行了4次对话的聊天来寻求支持 . )

1 回答

  • 0

    我猜你可能不再需要帮助,但万一你应该尝试删除coroutine.result()并直接运行它 . 改变:

    def bot_leave(self, ctx):
    state = self.get_voice_state(ctx.message.server)
    coro = state.voice.disconnect()
    fut = asyncio.run_coroutine_threadsafe(coro, state.voice.loop)
    try:
        fut.result()
    except:
        # an error happened sending the message
        pass
    

    至:

    def bot_leave(self, ctx):
    state = self.get_voice_state(ctx.message.server)
    coro = state.voice.disconnect()
    try:
        asyncio.run_coroutine_threadsafe(coro, state.voice.loop)
    except:
        # an error happened sending the message
        pass
    

    这是我唯一能想到看到你的代码片段的东西,但问题可能在于代码中的其他地方 .

相关问题