Discord.py 如何使用MessageType
因此,当Discord.py 如何使用MessageType,discord.py,Discord.py,因此,当premium\u guild\u subscription消息出现时,我试图让bot做一些事情。我试过: @bot.event async def on_message(message): MessageType = discord.MessageType if MessageType == 'pins_add': await message.channel.send('ITS WORKING!!!') 但它不起作用。当消息出现时,上述代码不起任何作用
premium\u guild\u subscription
消息出现时,我试图让bot做一些事情。我试过:
@bot.event
async def on_message(message):
MessageType = discord.MessageType
if MessageType == 'pins_add':
await message.channel.send('ITS WORKING!!!')
但它不起作用。当消息出现时,上述代码不起任何作用。如果我执行了
message.MessageType
,那么它会说message对象在代码中没有属性MessageType
,您正在尝试使用class。您必须从该类调用init函数。因此,您需要从消息
访问消息类型数据
根据API引用,您可以使用访问MessageType
对象。然后,您可以使用message.type.pins\u add
检查消息是否为pins\u add
@bot.event
异步def on_消息(消息):
如果message.type==discord.MessageType.pins\u添加:
等待message.channel.send('it WORKING!!!'))
您需要使用类的实例
,并对其进行比较,以确定其类型是否正确,而不是将类本身与字符串进行比较
@bot.event
async def on_message(message):
if message.type == discord.MessageType.pins_add:
await message.channel.send('ITS WORKING!!!')
另一个例子:
@bot.command()
async def test(ctx):
print(ctx.message.type == discord.MessageType.pins_add)
print(ctx.message.type == discord.MessageType.default)
导致
!test
> False
> True
表示AttributeError:“\u EnumValue\u MessageType”对象没有属性“pins\u add”
。为什么?这是完全错误的,你应该检查类型并进行比较,看看类型是否与你需要的类型相同,MessageType
并不是每个类型都有一个属性,它们也不是布尔值。