2017-04-14 57 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,在一方面設法改進機器人:機器人現在將重新加入服務器。第一次完美地工作,第二次,機器人出現....只是坐在那裏。 它似乎不喜歡創造一個以上的球員....或至少這就是我所假設的。 –