我的问题是,如果我知道用户id,我如何在没有用户在任何聊天中键入的情况下获得用户所在的语音通道id。
示例代码:
USER_ID = 1234578654
@bot.command()
async def join():
account = bot.get_user(USER_ID)
channel = account.voice.channel
voice = await channel.connect()
分步
- 找出用户在哪个通道中有id
- 加入那个频道
您可以尝试通过member
参数获取通道。现在,您可以使用用户的名称或ID。
看看下面的例子:
@bot.command()
async def join(ctx, member : discord.Member):
try:
channel = member.voice.channel
if channel: # If user is in a channel
await channel.connect() # Connect
await ctx.send("User is connected to a channel, joining...")
else:
await ctx.send("I am already connected to a channel.") # If the bot is already connected
except AttributeError:
return await ctx.send("User is not in a channel, can't connect.") # Error message
我们做了什么
- 获取
discord.Member
的语音通道 - 如果成员在语音频道中,请连接到该频道
- 如果
member
未连接到通道,则发出错误
要定义用户,可以使用以下功能:
from discord.utils import get
@bot.command()
async def join(ctx):
try:
member = ctx.guild.get_member(YourID) # Get the member by ID
if member: # If it is the defined member
await member.voice.channel.connect() # Connect
return await ctx.send("User is connected to a channel, joining...")
else:
await ctx.send("Not the defined user.")
except AttributeError:
return await ctx.send("The defined user is not in a channel.") # Error message
有关更多信息,您也可以查看文档