2017-04-14 54 views
0

我正在完成一个简单的声音剪辑的过程我通过在python中回收一个基本的音乐bot实例来制作不一致的bot。我想要的机器人只需输入调用命令(!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)) 

我曾尝试进入蟒蛇聊天不和谐API服务器,但就像我的机器人,我会见了蟋蟀。 (猜猜这就是我试图寻求从已经有4次对话的聊天中获得支持)

回答

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 

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

+0

Welp,在一方面设法改进机器人:机器人现在将重新加入服务器。第一次完美地工作,第二次,机器人出现....只是坐在那里。 它似乎不喜欢创造一个以上的球员....或至少这就是我所假设的。 –

相关问题