1

The program compiles seamlessly, and there are no errors displayed. It is aradio bot, which will stream radio to a voice channel. I have made this on replit. But whenever I type the required command with the prefix, after joining a voice channel, the bot doesn't join the voice channel at all. Here is the code for my bot. Can anyone please point out any errors and help me?

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'))
Conduit21
  • 11
  • 1
  • If you want to use commands use `commands.Bot` instead of `discord.Client` – Łukasz Kwieciński Jun 06 '21 at 19:30
  • @ŁukaszKwieciński I tried that exactly as you said. Thank You for the suggestion. But the bot still doesn't connect. – Conduit21 Jun 06 '21 at 20:10
  • You also need to put `await client.process_commands(message)` at the end of `on_message`, take a look at: https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working You also need the `PyNaCl` library installed (`pip install PyNaCl`) and imported within your code (`import nacl`) – Łukasz Kwieciński Jun 06 '21 at 20:11

1 Answers1

1

I suppose you didn't install the discord.py with voice support, use this pip command to install it

pip install discord.py[voice]

aph
  • 225
  • 3
  • 12