Discord Bot 不加入语音聊天 (Python)

时间:2021-07-31 09:15:55

标签: python discord discord.py

我没有收到任何错误代码,而且每当我在聊天中输入命令 !join 时什么也没有发生。我到处搜索似乎代码应该是正确的。 (顺便说一句,我的代码中有我的机器人令牌)

import os
import discord
from discord.ext import commands
my_secret = os.environ['Token']


client = discord.Client()
bot = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
  print ('{0.user} Activated'.format(client))

@bot.command()
async def join(ctx):
  channel = ctx.author.voice.channel.id
  await channel.connect()



client.run(my_secret)

2 个答案:

答案 0 :(得分:1)

首先,使用 commands.Botdiscord.Client。不要同时使用它们。 Boteventcommand

import os
import discord
from discord.ext import commands


my_secret = os.environ['Token']
bot = commands.Bot(command_prefix="!")

@bot.event
async def on_ready():
  print ('{0.user} Activated'.format(client))

@bot.command()
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()

@bot.command()
async def leave(ctx):
    await ctx.voice_client.disconnect()


bot.run(my_secret)

答案 1 :(得分:0)

您还需要 PyNaCl 库。使用 pip install discord.py[voice]

安装它
@Bot.command()
async def join(ctx, channel: discord.VoiceChannel = None):
    if channel == None:
        channel = ctx.message.author.voice.channel
    voice = discord.utils.get(Bot.voice_clients, guild=ctx.guild)
    if not voice or not voice.is_connected():
        await channel.connect()
    elif voice and voice.is_connected():
        await voice.move_to(channel)
    await ctx.send(f'Bot has joined')