制作一个bot命令



我实际上试图为我和我的朋友制作一个不和谐的bot .py,只是为了好玩和练习python,但是,当我尝试制作一个bot命令时,可以使用前缀+ [name_of_the_command],它不起作用。

import discord
from discord.ext import commands
intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True
intents.messages = True
intents.guild_messages = True
bot = commands.Bot(command_prefix="$", intents=intents)
@bot.event
async def on_ready():
print(f'Logged on as {bot.user}!')
@bot.event
async def on_message(message):
print(f'Message from {message.author}: {message.content}')

#     if message.author == bot.user :   
#       return

#     if message.channel == discord.DMChannel:
#         return

@bot.command()
async def ping(ctx):
print("test")
await ctx.channel.send("pong")

bot.run('MY_BOT_TOKEN')

我搜索了很多的教程,网站,论坛…看看是否有一个解释,但我没有找到,所以希望有人能告诉我我做错了什么,这个简单的事情不工作;我不知道它是否改变了什么,但我正在使用python 3.11.1和discord.py的最后更新(我今天刚刚dl discord.py)

这就是我在cmd中得到的,正如你所看到的,我有消息内容,但是之后,什么也没做祝你今天愉快。

您必须通过使用process_commands来处理on_message事件中的命令:

import discord
from discord.ext import commands
intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True
intents.messages = True
intents.guild_messages = True
bot = commands.Bot(command_prefix="$", intents=intents)
@bot.event
async def on_ready():
print(f'Logged on as {bot.user}!')
@bot.event
async def on_message(message):
print(f'Message from {message.author}: {message.content}')
await bot.process_commands(message)
#     if message.author == bot.user :   
#       return

#     if message.channel == discord.DMChannel:
#         return

@bot.command()
async def ping(ctx):
print("test")
await ctx.channel.send("pong")

bot.run('MY_BOT_TOKEN')

最新更新