您如何检查机器人连接到的语音通道 ID? (discord.py)
How can you check voice channel id that bot is connected to? (discord.py)
我有一个机器人,只有当调用它的用户在同一个语音通道中时,我才想听命令。这是我的代码。
@bot.command(name='leave', help='Disconnects the bot.')
async def leave(ctx):
user_channel = ctx.message.author.voice.channel
bot_channel = ctx.guild.voice_client
print(user_channel)
print(bot_channel)
if user_channel == bot_channel:
client = ctx.guild.voice_client
await client.disconnect()
else:
await ctx.send('You have to be connected to the same voice channel to disconnect me.')
但是,我的问题是那些打印行 return 不同的字符串。用户通道:vc 2、机器人通道:<\discord.voice_client.VoiceClient object at 0x000001D4E168FB20>
我怎样才能让他们都读取语音频道的 ID,以便我可以比较他们?
您的代码的唯一问题是您将用户当前的语音通道对象与语音客户端对象进行了比较。您可以将 .channel
添加到 ctx.guild.voice_client
的末尾。
比较两个频道对象与比较频道的 ID 一样。如果你真的想通过他们的 ID 来比较它们,那么只需在它们每个上添加 .id
。
示例:
@bot.command(help='Disconnects the bot.')
async def leave(ctx):
if ctx.author.voice.channel and ctx.author.voice.channel == ctx.voice_client.channel:
# comparing channel objects ^
await ctx.voice_client.disconnect()
else:
await ctx.send('You have to be connected to the same voice channel to disconnect me.')
请注意,我添加了 ctx.author.voice.channel and
,这样您就不会 运行 在命令执行程序和机器人都不在频道中时出现属性错误。
如果您不检查其中一个对象不是 None
,那么您会收到一条错误消息,指出 NoneType
没有属性 disconnect()
作为表达式None == None
将是 True
和 运行 语句。
参考文献:
我有一个机器人,只有当调用它的用户在同一个语音通道中时,我才想听命令。这是我的代码。
@bot.command(name='leave', help='Disconnects the bot.')
async def leave(ctx):
user_channel = ctx.message.author.voice.channel
bot_channel = ctx.guild.voice_client
print(user_channel)
print(bot_channel)
if user_channel == bot_channel:
client = ctx.guild.voice_client
await client.disconnect()
else:
await ctx.send('You have to be connected to the same voice channel to disconnect me.')
但是,我的问题是那些打印行 return 不同的字符串。用户通道:vc 2、机器人通道:<\discord.voice_client.VoiceClient object at 0x000001D4E168FB20> 我怎样才能让他们都读取语音频道的 ID,以便我可以比较他们?
您的代码的唯一问题是您将用户当前的语音通道对象与语音客户端对象进行了比较。您可以将 .channel
添加到 ctx.guild.voice_client
的末尾。
比较两个频道对象与比较频道的 ID 一样。如果你真的想通过他们的 ID 来比较它们,那么只需在它们每个上添加 .id
。
示例:
@bot.command(help='Disconnects the bot.')
async def leave(ctx):
if ctx.author.voice.channel and ctx.author.voice.channel == ctx.voice_client.channel:
# comparing channel objects ^
await ctx.voice_client.disconnect()
else:
await ctx.send('You have to be connected to the same voice channel to disconnect me.')
请注意,我添加了 ctx.author.voice.channel and
,这样您就不会 运行 在命令执行程序和机器人都不在频道中时出现属性错误。
如果您不检查其中一个对象不是 None
,那么您会收到一条错误消息,指出 NoneType
没有属性 disconnect()
作为表达式None == None
将是 True
和 运行 语句。
参考文献: