如何让我的不和谐机器人加入语音频道*在一个类别中*?

时间:2021-04-30 10:39:02

标签: python discord.py

我想编写一个能够加入的机器人;这是我的代码

import json
import discord
from discord.ext import commands

JSON_FILE = r"C:\JSON PATH"
bot = commands.Bot(command_prefix = "~")

with open(JSON_FILE, "r") as json_file:
    TOKEN = json.load(json_file)["token"]
    GUILD = json.load(json_file)["guild"]

bot = commands.Bot(command_prefix = "~")

@bot.event
async def on_ready():
    print(f"{bot.user.name} launched and has arrived for further use")

@bot.command(name = "join")
async def join(ctx, voice_channel: commands.VoiceChannelConverter):
    await voice_channel.connect()
    await ctx.send(f"I have joined: {voice_channel}")

@bot.command(name = "leave")
async def leave(ctx):
    server = ctx.message.server
    voice_client = bot.voice_client_int(server)
    await voice_client.disconnect()

bot.run(TOKEN)

可以加入普通频道,但不能加入分类频道。关于如何做到这一点的任何建议? 提前致谢

1 个答案:

答案 0 :(得分:1)

问题可能是,您使用的是过时版本的 discord.py,因为您使用的是 server,它自 v1.0 起已被弃用,而不是 guild。如果你更新它,它可能会起作用,因为当我尝试它时它也对我有用。

要将其编码为自动加入执行命令的用户的语音频道,只需使用ctx.author.voice.channel

@bot.command(name = "join")
async def join(ctx, voice_channel: commands.VoiceChannelConverter):
    if not ctx.author.voice is None:
        await ctx.author.voice.channel.connect()
        await ctx.send(f"I have joined your voice channel.")
    else:
        await ctx.send("You are not in a voice channel!")

参考文献: