我知道 Python 脚本 (3.12) 正在从 PyCharm 终端中的以下响应连接到 Discord 服务器
[2024-08-13 00:55:09] [INFO]discord.client:使用静态令牌登录
[2024-08-13 00:55:09] [INFO ]discord.gateway:分片 ID 没有连接到网关(会话 ID:xxxx)。
以 D&D 随机房间生成器#9850 身份登录!
但是,当我在 Discord 服务器中以用户身份输入“$hello”时,我期望在 Discord 服务器上得到“Hello World”的响应,但没有得到任何类型的回复。也没有错误代码。我的代码是否有问题,或者这是 Discord 服务器上的机器人角色的某种权限问题?
import discord
intents = discord.Intents.default()
intents.message_content = True
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
client = MyClient(intents=intents)
async def on_message(self, message):
if message.author == self.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello World!')
client.run('My Token')
在
on_message
类之外使用 Client
函数时,您需要一个 @client.event
装饰器。
在你的情况下,在课堂上移动
on_message
会更容易,因为你已经有self
作为参数。
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
async def on_message(self, message):
if message.author == self.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello World!')
client = MyClient(intents=intents)