首页 > 解决方案 > 我无法运行任何命令 discord.py

问题描述

所以我一直在为不和谐创建一个机器人,所以我一直在关注许多教程,但我唯一的命令不会运行。所以这是我的代码:

import discord
import os
from discord.ext import commands

client = discord.Client()
bot = commands.Bot(command_prefix = "_")

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith("salut le bot"):
        await message.channel.send("Salut, je suis Bapo Bot, pour vous servir !")
    if message.content.startswith("merci fréro"):
        await message.channel.send("De rien mon gars !")
    if str(message.channel) == "uniquement-des-images" and message.content != "":
        await message.channel.purge(limit=1)
        print('Message :', (message.content), 'from', (message.author), 'deleted successfully.')

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print(discord.__version__)
    print('------------------')

@bot.command()
async def ping(ctx):
    await ctx.send('Pong!')

client.run(os.environ['BOT_TOKEN'])

此代码没有给出任何错误。起初我以为是client = discord.Client()在创建错误,但是当我删除它时,整个机器人不再工作了。我尝试更改 bot 前缀或更改现在称为 bot 的东西的名称(我仍然是 python 中的菜鸟,所以我不知道这个东西叫什么),现在我被卡住了。

编辑:由于答案已被版主(?)删除,这是解决方案。您不能同时使用on_message函数作为命令函数。

标签: pythondiscord.py

解决方案


您需要添加await client.process_commands()到您的 on_message 事件!

我个人将我的留在方法的底部!


推荐阅读