我的Discord命令在python中无法工作

lp0sw83n  于 2023-01-19  发布在  Python
关注(0)|答案(1)|浏览(133)

我正在尝试用Python创建一个不和谐机器人。2但是没有一个命令能正常工作。3让我给你看我的代码:

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”命令,机器人没有输出。但如果我输入一个简单的消息“你好”,所以机器人也回复“嘿,那里”
this is what i am talking about (output on discord)

kmpatx3s

kmpatx3s1#

机器人命令(或用户命令)不再受支持。您必须切换到“猛击命令”。

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,discord.py已不再维护。

相关问题