python - 我无法运行任何命令 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
函数作为命令函数。
解决方案
您需要添加await client.process_commands()
到您的 on_message 事件!
我个人将我的留在方法的底部!
推荐阅读
- kubernetes-pod - YAML 文件中的 Kubernetes 命令和参数
- java - Swagger 忽略了生成代码的必填字段
- java - 为什么输入在打印之前仍然要求另一个输入?
- python - 使用给定单词列表中的单词形成给定单词
- spring-boot - Apache mina FTP服务器初始目录结构
- javascript - 正则表达式在两者之间解析 MIME 消息正文。如何?
- python-3.x - 有没有办法在调用 get 方法之前构造完整的 url?
- python - 寻求帮助以优化我在 python 中的自动键盘按压器
- sql - 合并来自不同表的列 BigQuery 返回数百万行
- flutter - 检测 draggableScrollableSheet 的位置滚动并覆盖其他小部件?