好的,所以我试着用on_message做一个命令,我把await bot.process_commands(message)
。但它总是提高'NoneType' object has no attribute 'id'
import os
import random
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.message_content = True
activity = discord.Activity(name='null, dying, trying to fix me', type=discord.ActivityType.watching)
client = discord.Client(intents=intents, activity = activity)
bot = commands.Bot(intents=intents,command_prefix='!')
@client.event
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
channel = client.get_channel(956302622170701946)
await channel.send(f'connected successfully as {client.user.name}#{client.user.discriminator}')
@client.event
async def on_message(message):
await bot.process_commands(message)
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send("hello stopid")
elif message.content.startswith('die'):
await message.channel.send('ok :sob: i ded now')
await client.close()
@bot.command
async def a(ctx):
await ctx.send("a")
client.run(TOKEN)
这是整个错误:
2022-12-08 18:15:54 ERROR discord.client Ignoring exception in on_message
Traceback (most recent call last):
File "C:UsersxxxxxsourcereposDBotDiscordBotlibsite-packagesdiscordclient.py", line 409, in _run_event
await coro(*args, **kwargs)
File "c:UsersxxxxxsourcereposDBotdiscordbot.py", line 25, in on_message
await bot.process_commands(message)
File "C:UsersxxxxxsourcereposDBotDiscordBotlibsite-packagesdiscordextcommandsbot.py", line 1389, in process_commands
ctx = await self.get_context(message)
File "C:UsersxxxxxsourcereposDBotDiscordBotlibsite-packagesdiscordextcommandsbot.py", line 1285, in get_context
if origin.author.id == self.user.id: # type: ignore
AttributeError: 'NoneType' object has no attribute 'id'
我只想要一个带有'$'前缀的命令,并发送回用户发送的参数
我可能做了什么蠢事,但还是谢谢你。
- 只需要
bot
实例,不需要client。CommandsBot只是升级了discord。客户机,还包括@bot。事件装饰 - 删除client.close()并将bot.process_commands(message)移动到on_message函数的底部
*注意,你不应该做任何事情,除了打印on_ready,因为它触发多次