gpt4 book ai didi

discord.py - 如何检查机器人是否连接到 channel ? |不和谐.py

转载 作者:行者123 更新时间:2023-12-04 01:42:39 31 4
gpt4 key购买 nike

我决定尝试让我的不和谐机器人播放音乐,但我已经卡住了。主要是因为我找不到任何资源来帮助当前版本,我一直在从文档中获取所有内容。但是,我不知道如何检查机器人是否已连接到语音 channel 。

我试过 if not Client.is_connected(): ,但是那没有用。如果有任何更新的资源可以帮助我了解 discord.py 语音功能的基础知识,请给我一个链接:) 这是我目前的代码:

# ----- ATTEMPT AT VOICE COMMANDS ------
#discord.opus.load_opus() - what goes in bracket???

@client.command(name="join", pass_ctx=True)
async def join(ctx):
#if not is_connected(): - Client.is_connected() not working

user = ctx.message.author
vc = user.voice.channel
await vc.connect()
await ctx.send(f"Joined **{vc}**")

#else:
# await ctx.send("I'm already connected!")

@client.command(name="disconnect", pass_ctx=True)
async def disconnect(ctx):
# if not is_connected(): - once again can't work it out
vc = ctx.message.guild.voice_client # i don't even know how this worked :D
await vc.disconnect()

#else:
# await ctx.send("I'm not connected to any channels")

@client.command(name="play", pass_ctx=True)
async def play(ctx, songurl=None):
if not songurl: # this works at least
await ctx.send("Please specify a song")
return
if not is_connected(): # once again, how to check if bot is connected?
vc = ctx.message.author.voice.channel
if not vc: # i think this should work
await ctx.send("You're not in a voice channel!")

await vc.connect()
# haven't even worked out anything past this point and it's broken

ps:对不起,我只是倾倒了我的整个 vc 部分,但我不太明白

这里真正重要的是播放命令,但我包括其他命令只是因为(正如您从我的评论中看到的那样)我不明白发生了什么。我应该怎么做?当前版本有什么好的来源吗?提前致谢。

最佳答案

一个bot可以同时连接多个公会的语音,所以需要获取 VoiceClient 来自 Client.voice_clients 的相应公会然后检查 VoiceClient.is_connected :

def is_connected(ctx):
voice_client = get(ctx.bot.voice_clients, guild=ctx.guild)
return voice_client and voice_client.is_connected()

关于discord.py - 如何检查机器人是否连接到 channel ? |不和谐.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56718658/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com