-2

i am trying to create a Discord Bot using Python. But none of the commands are working properly. Let me show you my code:

import os
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
import datetime
import youtube_dl

load_dotenv()
TOKEN = os.getenv('TOKEN')

bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())

@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')

@bot.command(name='ping', help= 'pong')
async def ping(ctx):
    await ctx.reply("pong")

@bot.event
async def on_message(message):
    if message.content == "Hello".lower():
        await message.channel.send("Hey there")

@bot.event
async def on_member_join(member):
    await member.guild.system_channel.send(f'Welcome {member.mention} to the server!')

@bot.event
async def on_member_remove(member):
    await member.guild.system_channel.send(f'{member.name} has left the server.')

@bot.command(name='date', help='Displays the current date')
async def date(ctx):
    date = datetime.datetime.now().strftime("%B %d, %Y")
    await ctx.send(f'The current date is {date}')

@bot.command(name='time', help='Displays the current time')
async def time(ctx):
    time = datetime.datetime.now().strftime("%I:%M %p")
    await ctx.send(f'The current time is {time}')

@bot.command(name='play', help='Plays music in a voice channel')
async def play(ctx, url: str):
    channel = ctx.author.voice.channel
    if channel is None:
        await ctx.send("You are not in a voice channel.")
        return
    vc = await channel.connect()

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    for file in ydl.prepare_filename(ydl.extract_info(url)):
        if file.endswith('.mp3'):
            vc.play(discord.FFmpegPCMAudio(file))
            vc.source = discord.PCMVolumeTransformer(vc.source)
            vc.source.volume = 0.07
            break

bot.run(TOKEN)

If i am running code, none of the commands are working. For example: If i am typing "!ping" command, no output by bot. But if i am typing a simple message "hello", so bot also replying "Hey there"

this is what i am talking about (output on discord)

  • 3
    CrazyChucky has the correct answer - on an additional note. Why do `"Hello".lower()`? Why not just have "hello"? Really, you should be converting the `message.content` to lower - not the string _you_ provide. So `if message.content.lower() == "hello"`. – ESloman Jan 19 '23 at 07:45

1 Answers1

-2

Bot Commands (or User command) are no more supported. You have to switch to "SLASH COMMANDS".

import discord

bot = discord.Bot()

@bot.slash_command()
async def hello(ctx, name: str = None):
    name = name or ctx.author.name
    await ctx.respond(f"Hello {name}!")

bot.run("token")

I suggest to use Pycord

Xrayman
  • 207
  • 1
  • 9
  • are these commands {bot.slash_command()} are on same discord.py package, or i've to install different pip pakage?? – Pankaj Bhardwaj Jan 19 '23 at 08:29
  • 2
    discord.py resumed development on March 2022. – TimG233 Jan 19 '23 at 08:42
  • 3
    Very misleading comment. You can use message commands but need to enable message intents do to so. [You can read up on how to do that here](https://discordpy.readthedocs.io/en/stable/intents.html). Also, as @TimG233 mentioned, discord.py resumed development long ago, pycord is not needed. The original question was about discord.py, and pycord has no real advantages (subjective), why suggest to switch? – moinierer3000 Jan 19 '23 at 11:21
  • Cause you can do it with less lines with pycord – Xrayman Jan 19 '23 at 17:09