这是我的代码
main.py
:
import discord
import asyncio
from discord.ext import commands
TOKEN="secret"
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="?", intents = intents)
@bot.event
async def on_ready():
print(f'{bot.user} successfully logged in!')
@bot.event
async def on_message(message):
if message.author == bot.user:
return
await bot.process_commands(message)
@bot.command()
async def spam(ctx, message, *, amount:int):
await ctx.send("Starting Spam...")
new_amount = amount+1
for i in range(1, new_amount):
await ctx.send(message)
await asyncio.sleep(0.5)
await ctx.send("Spam Completed!")
try:
bot.run(TOKEN, bot = False)
except discord.HTTPException as e:
if e.status == 429:
print("The Discord servers denied the connection for making too many requests")
else:
raise e
我的机器人确实上线了,但是,当我使用垃圾邮件命令时,它不起作用。 该代码没有给出任何错误。所以我想知道问题是什么。 任何帮助将不胜感激。
您没有将
message_content
意图设置为 True。 docs的第一页显示您需要启用message_content
意图。这允许您的机器人读取消息内容;因此实际上能够响应命令。
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="?", intents=intents)
您还需要在
Discord Developer Portal中启用
message content
意图。在您的应用程序下,转到“Bot”,然后向下滚动到“Privileged Gateway Intents”,然后启用消息内容之一。
根据discord.py 文档:
“随着 API 更改要求机器人作者指定意图,某些意图受到进一步限制并需要更多手动步骤。这些意图称为 特权意图。”
所以基本上您需要做的是转到不和谐开发人员门户中应用程序中的“机器人”部分,并在标题特权网关意图下,您需要根据您的需求启用消息内容意图等。这也需要通过代码来完成。
您的情况:
intents = discord.Intents.default()
需要更换为
intents = discord.Intents(messages=True,message_content=True)
或者简单地
intents = discord.Intents.all()
请随意评论有关此解决方案的任何疑问。