【问题标题】:Discord.py how to leave channel without ctxDiscord.py 如何在没有 ctx 的情况下离开频道
【发布时间】:2021-07-06 03:50:38
【问题描述】:

这是一个不和谐的机器人功能,当有人加入时应该加入语音频道,播放一首歌 5 秒然后离开频道。

问题是@bot.event 我无法将上下文传递给它,所以我找到了一种与 after arg 连接的方法,但我找不到离开频道的方法。

 @bot.event                                                                                                                                                                                           
     async def on_voice_state_update(member, before, after):                                                                                                                                              
         if not before.channel and after.channel:                                                                                                                                                         
             await after.channel.connect()                                                                                                                                                                
             voice = discord.utils.get(bot.voice_clients)                                                                                                                                                 
             voice.play(discord.FFmpegPCMAudio("song.mp3"))                                                                                                                                               
             time.sleep(5)                                                                                                                                                                                
             await after.channel.disconnect()

它说

AttributeError: 'VoiceChannel' object has no attribute 'disconnect

我知道我执行此功能的方式可能是错误的,所以如果你们知道“正确”的方式,我希望你们能注意到我。

【问题讨论】:

标签: python discord


【解决方案1】:

我发现这种方式可以在不传递上下文的情况下离开语音频道:

 @bot.event                                                                                                                                                                                           
 async def on_voice_state_update(member, before, after):                                                                                                                                              
     if not before.channel and after.channel:                                                                                                                                                         
         await after.channel.connect()                                                                                                                                                                
         voice = discord.utils.get(bot.voice_clients)                                                                                                                                                 
         voice.play(discord.FFmpegPCMAudio("song.mp3"))                                                                                                                                               
         time.sleep(5)                                                                                                                                                                                
         await voice.disconnect()

【讨论】:

    猜你喜欢
    • 2021-05-29
    • 1970-01-01
    • 2019-09-26
    • 2021-05-18
    • 2017-12-29
    • 2018-06-16
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多