Python 为什么我的discord机器人只执行我的命令一次,而且只执行一次?

Python 为什么我的discord机器人只执行我的命令一次,而且只执行一次?,python,command,bots,disconnect,discord,Python,Command,Bots,Disconnect,Discord,我正在完成一个简单的声音剪辑Discord机器人,我是通过在python中循环使用一个基本的音乐机器人示例制作的。我想让机器人做的就是输入调用命令(!womble)的用户的语音频道,从声音剪辑文件夹中播放随机声音剪辑,然后离开语音频道 “简单,对吧?”当然不是,显然不是这个API 经过一系列的尝试和错误,通过至少3次API修订,我让机器人实际执行了命令……一次。任何进一步的命令提示都会遇到蟋蟀。我能做一件好事!召唤并将机器人带入通道,但是!womble命令不再有效 def bot_leave(s

我正在完成一个简单的声音剪辑Discord机器人,我是通过在python中循环使用一个基本的音乐机器人示例制作的。我想让机器人做的就是输入调用命令(!womble)的用户的语音频道,从声音剪辑文件夹中播放随机声音剪辑,然后离开语音频道

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

经过一系列的尝试和错误,通过至少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次对话的聊天中寻求支持的原因。)

我猜您可能不再需要帮助,但只是为了以防万一,您应该尝试删除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
致:


这是我唯一能想到的看到您的代码片段的地方,但问题可能出在代码的其他地方。

Welp,这在一个方面改进了bot:bot现在将重新加入服务器。第一次工作完美无瑕,第二次,机器人出现了……只是坐在那里。它似乎不喜欢创造一个以上的球员一次…或至少这是我的假设。
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