【问题标题】:Discord.py channel.connect() never returnsDiscord.py channel.connect() 永远不会返回
【发布时间】:2020-10-14 20:13:28
【问题描述】:

我目前正在为我的不和谐服务器开发一个 discord.py-rewrite (1.3.3) 机器人。目前,我正在尝试让机器人在语音频道中播放音乐。根据 discord.py 文档,您将使用函数 channel.connect() 连接到语音通道,这将返回一个 VoiceClient 对象。

但是,我从来没有从 channel.connect() 得到 VoiceClient 对象。该机器人确实加入了我的频道,但它似乎陷入了无限循环。执行“await channel.connect()”行后没有任何内容,因此不打印“test”行。当我更新 bot 在服务器中的角色时,它可以工作一次,但在我重新启动 bot 后它将不再工作。

# This is just a function, not the command the user calls. The context is passed through
async def join(ctx):
    voice_status = ctx.author.voice

    # Checking if author voice_status is not none
    if voice_status:
        # Getting the channel of the author
        channel = voice_status.channel

        if ctx.voice_client is None:
            # Connect the bot
            vc = await channel.connect()
            print("test")

我在 github 上发现了一些线程并溢出,人们遇到了同样的问题,但他们从未修复它。我很确定代码是正确的。

我已经尝试过重新安装和更新 discord.py。我还在 discord API 服务器中寻求帮助,但他们无法复制我的问题。

这是我的第一个溢出帖子,所以如果我的帖子有任何问题,我提前道歉。

干杯

【问题讨论】:

    标签: python bots discord discord.py discord.py-rewrite


    【解决方案1】:

    这是让您的机器人加入语音频道的一种方法:

    async def join(ctx):
        channel = ctx.message.author.voice.channel
        if not channel:
            await ctx.send("You're not connected to any voice channel !")
        else:
            voice = get(self.bot.voice_clients, guild=ctx.guild)
            if voice and voice.is_connected():
                await voice.move_to(channel)
            else:
                voice = await channel.connect()
    

    PS : 如果你添加一个play 命令,你仍然需要获取机器人的频道和语音:

    voice = get(self.bot.voice_clients, guild=ctx.guild)
    channel = ctx.message.author.voice.channel
    

    【讨论】:

    • 这工作了几次,但现在已经不行了。这似乎是随机的。
    • 当它不再工作时,您是否有任何错误消息?
    • 不,它不会抛出任何异常。它只是卡在 connect() 行。
    【解决方案2】:

    我删除了你的 cmets 并添加了我自己的 #comments

    voice_status = ctx.author.voice
    if voice_status:  # this check does nothing. This is a discord.VoiceState object
      channel = voice_status.channel
      # channel is None if you are not connected to a voice channel
      # channel is a Channel object if you are connected to a voice channel
      if ctx.voice_client is None:
      # I may be wrong but if I read the docs right this returns the voice client of the guild, something semi-related to the author.
        vc = await channel.connect()
        # You are not connected to a voice channel. So channel is None. Now you are trying to connect to a None channel.
        print("test")
    

    【讨论】:

    • 我检查 voice_status 是否为 none,因为如果是,则通道不存在。如果我然后尝试获取 ctx.author.voice.channel 你会得到一个属性错误。
    【解决方案3】:

    ik 这很旧,但我自己花了很长时间解决问题,但我还没有在网上找到任何答案。我的问题是我的机器人没有语音内容的意图。如果您的机器人仅适用于像您的朋友这样的一小部分人,则只需将 intents 设置为 discord.Intents.all() 即可。否则,您可以手动选择它们。

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 2011-07-25
      • 1970-01-01
      • 2012-10-02
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多