【发布时间】:2021-03-07 21:52:33
【问题描述】:
我正在构建一个机器人,该机器人需要连接到语音通道,这是我的代码:
import discord
from discord.ext import commands
from discord.utils import get
b = commands.Bot(command_prefix=".")
@b.event
async def on_ready():
print("Bot is logged in")
@b.command(pass_context=True, aliases=['j'])
async def join(ctx):
channel = ctx.message.author.voice.channel
voice = get(b.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
await ctx.send("Moved to the voice channel")
elif #Check if the user is in the voice channel and the bot is not:
voice = await channel.connect()
await ctx.send("Connected to the voice channel")
elif #Check if the user is in the voice channel
await ctx.send("**Get in a voice channel**")
elif #Check if the bot is already in the channel
await ctx.send("**Already in the channel**")
b.run("stack overflow")
所以在进入语音通道之前我想检查几件事,我想出了如何连接并从不同的语音通道移动但被困在那里,我是 python 的新手,所以如果我的代码可以简化任何建议将不胜感激。
【问题讨论】:
-
第一个问题,检查人是否有VoiceState,检查机器人是否也有VoiceState,如果用户没有语音状态,请他加入语音频道,第三个,同
if voice and voice.is_connected():
标签: python python-3.x discord discord.py discord.py-rewrite