我的Discord命令在python中不工作



我正在尝试使用Python创建一个不和Bot。但是这些命令都不能正常工作。让我展示一下我的代码:

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)

如果我正在运行代码,没有一个命令是工作的。例如:如果我正在输入"!ping"命令,bot没有输出。但是如果我在输入一个简单的消息"hello",那么bot也会回复"Hey there">

这就是我要说的(输出在discord)

不支持Bot命令(或User命令)。你必须切换到"斜杠命令"

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

建议使用Pycord

最新更新