首页 > 解决方案 > Discord Bot 不加入语音聊天 (Python)

问题描述

我没有收到任何错误代码,并且每当我将命令 !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)

标签: pythondiscorddiscord.py

解决方案


首先,使用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)

推荐阅读