【问题标题】:Discord.py bot won't join voiceDiscord.py 机器人不会加入语音
【发布时间】:2020-12-30 23:38:57
【问题描述】:

我正在尝试制作一个不和谐的机器人。它可以很好地响应某些消息,这是我想要它做的一部分。我现在希望它能够加入语音频道,但它不会:

import discord
import youtube_dl
from discord.ext import commands
from discord.utils import get


TOKEN = 'token string here
BOT_PREFIX = '/'

bot = commands.Bot(command_prefix=BOT_PREFIX)

players = {}

@bot.event
async def on_ready():
    print(bot.user.name + ' is now online.\n')

然后在我处理@bot.event 的消息之后,

@bot.command(pass_context=True)
async def join(ctx):
    print('Join executed')
    global voice
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()

    print (f"The bot has joined {channel}\n")


@bot.command(pass_context=True)
async def leave(ctx):
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.disconnect()
        print(f"The bot has left {channel}")
    else:
        print("Bot was asked to leave, but was not in one.")

我是新手,所以它可能是(或可能是)一个非常简单、愚蠢的解决方法。

连接命令甚至没有被处理,因为当我在不和谐聊天中调用它时,该打印语句没有显示在控制台中。

我正在关注 this video tutorial 到 T,他显然工作正常。

【问题讨论】:

  • 您的代码对我有用。您是否在机器人可以看到的文本频道中调用/join?除了/join,其他命令是否有效? (尝试制作一个仅打印到控制台的简单命令)
  • 你有on_message() 活动吗?
  • 我确实有一个 on_message 事件,它工作得非常好。 Allister,我会尝试一些其他简单的命令并回复您。

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

检查下面。这将适用于最新版本的 discord.py。

   @bot.command(name='join', invoke_without_subcommand=True)
    async def join(ctx):
       destination = ctx.author.voice.channel
       if ctx.voice_state.voice:
         await ctx.voice_state.voice.move_to(destination)
         return
    
       ctx.voice_state.voice = await destination.connect()
       await ctx.send(f"Joined {ctx.author.voice.channel} Voice Channel")

【讨论】:

    【解决方案2】:

    我想你刚刚收到了 on_message 事件。如果您这样做,请检查此docs page。 另外我不知道你为什么通过pass_context 他们在 1.0 版本中删除了它,现在它甚至不存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-30
      • 2022-01-04
      • 2019-04-08
      • 2021-12-06
      • 1970-01-01
      • 2023-02-25
      • 2020-10-04
      • 2018-06-16
      相关资源
      最近更新 更多