基于 discord.py 的机器人在 2.0 更新后不起作用



我一年前就习惯使用这个机器人了,现在我想再次启动它,但是在discord.py 2.0更新之后,它似乎不工作了

import discord
from keep_alive import keep_alive
class MyClient(discord.Client):
async def on_ready(self):
print('bot is online now', self.user)
async def on_message(self, message):
word_list = ['ffs','gdsgds']

if message.author == self.user:
return
messageContent = message.content
if len(messageContent) > 0:
for word in word_list:
if word in messageContent:
await message.delete()
await message.channel.send('Do not say that!')

# keep_alive()
client = discord.Client(intents=discord.Intents.default())
client.run('OTkxfsa9WC5G34')
from flask import Flask
from threading import Thread 
app = Flask('')
@app.route('/')
def home():
return 'dont forget uptime robot monitor'
def run():
app.run(host='0.0.0.0',port=8000)
def keep_alive():
t = Thread(target=run)
t.start()

我试图通过修改这一行来修复它

client = discord.Client(intents=discord.Intents.default())

这一定是一些微不足道的语法错误,但我找不到它

编辑1:所以我打开意图在bot开发人员门户,使我的代码看起来像这样,但似乎仍然有些东西不工作

import discord
from keep_alive import keep_alive
class MyClient(discord.Client):
async def on_ready(self):
print('bot is online now', self.user)
async def on_message(self, message):
word_list = ['fdsfds','fsa']

if message.author == self.user:
return
messageContent = message.content
if len(messageContent) > 0:
for word in word_list:
if word in messageContent:
await message.delete()
await message.channel.send('Do not say that!')

# keep_alive()
intents = discord.Intents.default()                  
intents.message_content = True                  
client = discord.Client(intents = intents)
client.run('OTkxMDcxMTUx')

如果这是一个语法错误,你会得到一个语法错误。真正的问题是您没有启用message_content意图,因此您无法读取消息的内容。Intents.default()不包含特权意图

intents = discord.Intents.default()
intents.message_content = True

不要忘记在bot的开发人员门户上也启用它。

所有这些都保持活力&Flask的东西暗示你正在滥用一个在线主机来运行一个机器人。这会带来很多你无法解决的问题,所以你应该考虑放弃它。每天都有人发帖抱怨这个问题。

你的代码应该是:

import discord
from discord.ext import commands # you need to import this to be able to use commands and events
from keep_alive import keep_alive
client = commands.Bot(intents=discord.Intents.default())
@bot.event
async def on_ready(): # you don't need self in here
print('bot is online now', client.user) # you can just use client.user
@bot.event
async def on_message(message): # again, you do not need self
word_list = ['ffs','gdsgds']
if message.author == client.user: # you can use client.user here too
return
messageContent = message.content
if len(messageContent) > 0:
for word in word_list:
if word in messageContent:
await message.delete()
await message.channel.send('Do not say that!')
keep_alive()
client.run('OTkxfsa9WC5G34') #if this is your real token/you have been using this token since you made the bot, you should definitely generate a new one

为了帮助你,我添加了一些注释来帮助你展示我改变了什么以及为什么。正如代码中的一个评论所说,您应该在Discord Developer Portal中重新生成您的bot令牌。

有关迁移到2.0的更多信息,请阅读Discord.Py文档

最新更新