想要在消息发送时添加反应(使用 Hikari + Lightbulb)

huangapple go评论67阅读模式
英文:

Wanting to add a reaction whenever a message is sent (Using Hikari + Lightbulb)

问题

I'm using Hikari + Lightbulb to create my first discord bot and I'm using this as an opportunity to learn as much as possible. As mentioned in the title, I just want to learn how to use add_reaction(":smile:") correctly. I've read through the Hikari documentation and to be honest, I am having difficulties understanding it fully. I thought the correct line of code would be something along the lines of this

@bot.listen(hikari.MessageCreateEvent)
async def message(event):
    print(event.content)
    await message.add_reaction(":smile:")

When I go to run this code I receive the following error: AttributeError: 'GuildMessageCreateEvent' object has no attribute 'add_reaction'. This is making think my error lies with using the MessageCreateEvent object but after reading the documentation I can't tell which object does support this attribute. Is there something I'm missing? Or am I simply not using the attribute correctly and it's meant to be used in some other way?

英文:

I'm using Hikari + Lightbulb to create my first discord bot and I'm using this as an opportunity to learn as much as possible. As mentioned in the title, I just want to learn how to use
add_reaction(":smile:") correctly. I've read through the Hikari documentation and to be honest, I am having difficulties understanding it fully. I thought the correct line of code would be something along the lines of this

@bot.listen(hikari.MessageCreateEvent)
async def message(event):
    print(event.content)
    await message.add_reaction(":smile:")

When I go to run this code I receive the following error: AttributeError: 'GuildMessageCreateEvent' object has no attribute 'add_reaction'. This is making think my error lies with using the MessageCreateEvent object but after reading the documentation I can't tell which object does support this attribute. Is there something I'm missing? Or am I simply not using the attribute correctly and it's meant to be used in some other way?

答案1

得分: 0

Sure, here is the translated content:

"好的,所以在评论的帮助下(感谢Guimoute),我已经设法找出解决方法。

我需要在message.add_reaction之前包含event.

以防有人遇到类似问题,这是对我有效的完整代码

@bot.listen(hikari.MessageCreateEvent)
async def message(event):
    print(event.content)
    await event.message.add_reaction(emoji="👍")

你们中的敏锐者会注意到我不得不更改结尾。如果你不具体并且使用emoji="",它会告诉你该值不是雪花,我不完全理解,但在查看一些discord开发门户文档时,我认为这是discord的要求。"

英文:

Okay so I've managed to figure it out with Help from the comments (Thank you Guimoute).

I needed to include event. before the message.add_reaction.

Just in case anyone has had a similar issue this is the complete code that worked for me

@bot.listen(hikari.MessageCreateEvent)
async def message(event):
    print(event.content)
    await event.message.add_reaction(emoji="👍")

Keen eyed among you will notice I had to change the end. If you don't be specific and and use emoji="" is will tell you that the value is not snowflake, I don't fully understand it but reading over some of the discord dev portal documentation, I think it's a requirement of discord.

huangapple
  • 本文由 发表于 2023年2月18日 08:46:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定