我似乎无法将我创建的discord机器人添加到语音频道。任何帮助都将不胜感激。非常感谢。

2024-04-25 23:26:03 发布

您现在位置:Python中文网/ 问答频道 /正文

程序可以无缝编译,并且不会显示任何错误。这是aradio机器人,它将把无线电传输到语音频道。这是我自己做的。但每当我键入带有前缀的所需命令时,在加入语音频道后,bot根本不会加入语音频道。这是我的机器人的代码。有人能指出错误并帮助我吗

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'))

Tags: inimportclientyoujsonmessageyourget