【问题标题】:I can't seem to add my discord bot I created to a voice channel. Any help will be appreciated. Thank You我似乎无法将我创建的不和谐机器人添加到语音频道。任何帮助将不胜感激。谢谢你
【发布时间】:2021-06-07 17:21:14
【问题描述】:

程序无缝编译,没有错误显示。它是 aradio bot,它将广播流式传输到语音频道。我在replit上做了这个。但是每当我输入带有前缀的所需命令时,在加入语音频道后,机器人根本不会加入语音频道。这是我的机器人的代码。任何人都可以指出任何错误并帮助我吗?

from keep_alive import keep_alive
import discord
import os
import requests
import json
import random
from discord import FFmpegPCMAudio
from discord.ext.commands import Bot

client = discord.Client()

sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "sorrowful", "regretful", "downcast", "miserable", "downhearted", "despondent", "despairing", "disconsolate" ]

starter_encouragements = [
  "Cheer up! Turn that frown upside down!",
  "Hang in there. It will all soon pass.",
  "You are a great person!(Hopefully :-P)",
  "Use your sharp-sightedness to make clear distinctions and wise choices. Move forward with confidence in your inner compass.",
  "Give yourself another day, another chance. You will find your courage eventually. Don't give up on yourself just yet.",
  "Don't let life discourage you; everyone who got where he is had to begin where he was.",
  "Even if you're on the right track, you'll get run over if you just sit there.",
  "There are two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle."

]

def get_quote():
  response = requests.get("https://zenquotes.io/api/random")
  json_data = json.loads(response.text)
  quote = json_data[0]['q'] + " -" + json_data[0]['a']
  return(quote)

client = Bot(command_prefix=list('PREFIX'))

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    msg = message.content  

    if message.content.startswith('#hello'):
        await message.channel.send('Hello! Hope you are having a nice day, bud!')
    
    if message.content.startswith('#inspire'):
        quote = get_quote()
        await message.channel.send(quote)

    if any(word in msg for word in sad_words):
        await message.channel.send(random.choice(starter_encouragements)) 

@client.command(aliases=['p'])
async def play(ctx, url: str = 'http://stream.radioparadise.com/rock-128'):
    channel = ctx.message.author.voice.channel
    global player
    try:
        player = await channel.connect()
    except:
        pass
    player.play(FFmpegPCMAudio('http://stream.radioparadise.com/rock-128'))

@client.command(aliases=['s'])
async def stop(ctx):
    player.stop()

keep_alive()
client.run(os.getenv('TOKEN'))

【问题讨论】:

  • 如果你想使用命令使用commands.Bot而不是discord.Client
  • @ŁukaszKwieciński 我完全按照你说的做了。感谢您的建议。但机器人仍然无法连接。
  • 您还需要将await client.process_commands(message)放在on_message的末尾,看看:stackoverflow.com/questions/49331096/…您还需要安装PyNaCl库(pip install PyNaCl)并导入到您的代码 (import nacl)

标签: python discord discord.py


【解决方案1】:

我想你没有安装支持语音的 discord.py,使用这个 pip 命令安装它

pip install discord.py[voice]

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 2021-05-11
    • 2020-10-15
    • 1970-01-01
    • 2017-12-29
    • 2021-05-23
    • 2019-12-08
    • 1970-01-01
    相关资源
    最近更新 更多