【问题标题】:Discord Bot doesnt join Voice Chat (Python)Discord Bot 不加入语音聊天 (Python)
【发布时间】:2021-10-06 13:50:23
【问题描述】:

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

【问题讨论】:

  • 看来你没有运行机器人,只有客户端。

标签: python discord discord.py


【解决方案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)

【讨论】:

    【解决方案2】:

    您还需要 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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 2021-01-05
      • 2020-11-03
      • 2020-11-04
      • 2020-06-26
      • 2021-01-10
      • 2019-06-29
      • 2017-06-22
      相关资源
      最近更新 更多