在我的Discord机器人中,ctx.author.voice.channel没有返回任何值。

huangapple go评论71阅读模式
英文:

In my discord bot ctx.author.voice.channel is not returning any value

问题

异步 def play(self, ctx, *args):
        query = " ".join(args)
        voice_channel = ctx.author.voice.channel
        print(voice_channel)
        if voice_channel is None:
            print('测试')
            #需要连接才能让机器人知道要去哪里
            # await message.channel.send(response) 
            await ctx.send("连接到语音频道!")
        elif self.is_paused:
            print('poop')
            self.vc.resume()
        else:
            print('你好')
            song = self.search_yt(query)
            if type(song) == type(True):
                await ctx.send("无法下载歌曲。请尝试其他关键字,可能是播放列表或直播流格式错误。")
            else:
                await ctx.send("歌曲已添加到队列")
                self.music_queue.append([song, voice_channel])
                
                if self.is_playing == False:
                    await self.play_music(ctx)
英文:

I want my discord bot to send me a message if there is no one in a voice channel.

async def play(self, ctx, *args):
        query = " ".join(args)
        voice_channel = ctx.author.voice.channel
        print(voice_channel)
        if voice_channel is None:
            print('test')
            #you need to be connected so that the bot knows where to go
            # await message.channel.send(response) 
            await ctx.send("Connect to a voice channel!")
        elif self.is_paused:
            print('poop')
            self.vc.resume()
        else:
            print('hello')
            song = self.search_yt(query)
            if type(song) == type(True):
                await ctx.send("Could not download the song. Incorrect format try another keyword. This could be due to playlist or a livestream format.")
            else:
                await ctx.send("Song added to the queue")
                self.music_queue.append([song, voice_channel])
                
                if self.is_playing == False:
                    await self.play_music(ctx)

However, if I'm not in the voice channel, nothing gets returned. If I do print(voice_channel) literally nothing shows up in my linux terminal, not even a None value. Any thoughts here?

答案1

得分: 2

因为如果你不在语音频道中,ctx.author.voice 将为 None。然后,None.channel 将导致未捕获的异常,即 NoneType 对象没有属性。<br>
要检查命令作者是否在频道中,可以考虑检查其 voice 属性,类似于以下方式:

voice = ctx.author.voice
if voice is None:  # 作者不在语音频道中
  # 进行一些操作,比如告诉作者应该加入语音频道,然后返回
  return

# 这意味着作者已加入语音频道
print(voice.channel)  # 获取作者的频道

此外,如果你想检查音乐是否正在播放,需要访问你的机器人 voice_client,而不是命令作者(用户)的 voice_client。你可以像这样进行操作:

bot_voice = ctx.voice_client  # 快捷方式到 ctx.guild.voice_client
if bot_voice and bot_voice.is_paused():  # 机器人连接到频道且音乐已暂停
  bot_voice.resume()
elif ... :  # 可能检查 voice 并执行一些连接操作
英文:

Because if you are not in the voice channel, ctx.author.voice will be None. Then, None.channel will lead to an uncaught exception, which is NoneType Object has no attribute. <br>
To check whether command author is in the channel or not. Consider checking its voice, sth like

voice = ctx.author.voice
if voice is None:  # author is not in the voice channel
  # do sth like telling author should join a voice channel and return
  return

# it means author joins a voice channel
print(voice.channel)  # to get the author&#39;s channel

Also, if you want to check whether the music is still playing or not. You need to access your bot voice_client, not the command author (which is the user)'s voice_client. You could do sth like:

bot_voice = ctx.voice_client  # shortcut to ctx.guild.voice_client
if bot_voice and bot_voice.is_paused():  # bot connects to a channel and music is paused
  bot_voice.resume()
elif ... :  # maybe check voice and do some connection stuff
  

</details>



huangapple
  • 本文由 发表于 2023年5月18日 11:17:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定