简体   繁体   English

JDA 5 embed addreaction is incompatible types 错误

[英]JDA 5 embed addreaction is incompatible types error

I have imported.net.dv8tion.jda.api.entities.emoji.Emoji and below is the code written我导入了.net.dv8tion.jda.api.entities.emoji.Emoji 下面是写的代码

EmbedBuilder embed = new EmbedBuilder();
                embed.setColor(Color.green);
                event.getChannel().sendTyping().queue();
                event.getChannel().sendMessage("").setEmbeds(embed.build()).queue(message -> {
                    message.addReaction("✔").queue();
                });

When running the code above, the error java: incompatible types: java.lang.String cannot be converted to.net.dv8tion.jda.api.entities.emoji.Emoji occurs.运行上述代码时,出现错误java: incompatible types: java.lang.String cannot be converted to.net.dv8tion.jda.api.entities.emoji.Emoji

You have to create an instance of Emoji .您必须创建一个Emoji实例。 It offers various factory methods:它提供了多种工厂方法:

  • Emoji.fromCustom Can be used to create a custom emoji (those you upload with custom images) Emoji.fromCustom可用于创建自定义表情符号(您使用自定义图像上传的表情符号)
  • Emoji.fromUnicode Can be used to create a unicode emoji (such as smiley or similar) Emoji.fromUnicode可用于创建 unicode 表情符号(例如笑脸或类似符号)
  • Emoji.fromFormatted Can be used to parse any emoji from the mention format (relevant for messages) Emoji.fromFormatted可用于解析提及格式中的任何表情符号(与消息相关)

In your case, the string you have is a unicode emoji.在您的例子中,您拥有的字符串是 unicode 表情符号。 To use it as a reaction, simply wrap it in Emoji.fromUnicode(...) .要将其用作反应,只需将其包装在Emoji.fromUnicode(...)中。

Example: message.addReaction(Emoji.fromUnicode("✔")).queue()示例: message.addReaction(Emoji.fromUnicode("✔")).queue()

Note that you should also make sure that your files and compiler use UTF-8 if you directly paste emoji this way.请注意,如果您以这种方式直接粘贴表情符号,还应确保您的文件和编译器使用 UTF-8。 To avoid issues with encoding you can use the codepoint notation instead.为避免编码问题,您可以改用代码点表示法。 You can find the codepoint notation by searching for ✔ unicode on your search engine of choice.您可以通过在您选择的搜索引擎上搜索✔ unicode来找到代码点表示法。

Example: message.addReaction(Emoji.fromUnicode("U+2713")).queue()示例: message.addReaction(Emoji.fromUnicode("U+2713")).queue()

For performance reasons, it is recommended to store these emoji instances in static final fields.出于性能原因,建议将这些表情符号实例存储在 static 最终字段中。

public class Emojis {
  public static final Emoji CHECKMARK = Emoji.fromUnicode("U+2713");
}

Then you can just use it like this: message.addReaction(Emojis.CHECKMARK).queue() .然后你可以像这样使用它: message.addReaction(Emojis.CHECKMARK).queue()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM