如何对命令中的表情做出反应 (Discord JDA)?

Posted

技术标签:

【中文标题】如何对命令中的表情做出反应 (Discord JDA)?【英文标题】:How can I react with an emote from a command (Discord JDA)? 【发布时间】:2021-02-06 13:49:56 【问题描述】:

我一直在尝试向我的不和谐机器人添加一个反应角色命令。但奇怪的是它不能正常工作。 但问题是它在六个月前就奏效了。我没有更改代码中的任何内容。

代码停在这里:Emote emote = emotes.get(0);

我的班级:

    package de.nameddaniel.bot.commands;

import java.util.List;

import de.nameddaniel.bot.main.LiteSQL;
import de.nameddaniel.bot.main.Utils;
import de.nameddaniel.bot.types.ServerCommand;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;

public class ReactionRolesCommand implements ServerCommand 

    @Override
    public void performCommand(Member m, TextChannel channel, Message msg) 
        //!rr channel messageid :ok: ...
        
        String[] args = msg.getContentDisplay().split(" ");
        if(m.hasPermission(Permission.MANAGE_ROLES)) 
            if(args.length >= 5) 
                
                List<TextChannel> channels = msg.getMentionedChannels();
                List<Role> roles = msg.getMentionedRoles();
                List<Emote> emotes = msg.getEmotes();
                                
                System.out.println("3");
                
                    TextChannel tc = msg.getMentionedChannels().get(0);
                    String messageIDString = args[1];
                    Role role = roles.get(0);
                    
                    System.out.println("1");
                    
                    try                        
                        long messageID = Long.parseLong(messageIDString);
                                                
                        Emote emote = emotes.get(0);
                        
                        tc.addReactionById(messageID, emote).queue();
                                                    
                        System.out.println("2");
                        
                        LiteSQL.onUpdate("INSERT INTO reactroles(guildid, channelid, messageid, emote, roleid) "
                                        + "VALUES(" + channel.getGuild().getIdLong() + ", " + tc.getIdLong() + ", "
                                        + messageID + ", '" + emote.getId() + "', " + role.getIdLong() + ")");

                        Utils.Embed("Deiner Nachricht in " + msg.getMentionedChannels() + " wurde die Reaktion " + msg.getEmotes()
                                    + " hinzugefügt.", channel, "Erfolgreich!");
                        
                    
                    catch (NumberFormatException e) 
                        e.printStackTrace();
                        System.out.println("ReactionCommand");
                               
                
            
            else
                Utils.Embed("Bitte benutze: !rr [MessageID] #channel [Emote] @Rolle", channel, "Falscher Syntax");
            System.out.println(args.length);
            
        
        else
            msg.delete().queue();
    


错误代码:

3
1
[JDA [0 / 1] MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at java.util.Collections$UnmodifiableList.get(Unknown Source)
    at de.nameddaniel.bot.commands.ReactionRolesCommand.performCommand(ReactionRolesCommand.java:40)
    at de.nameddaniel.bot.main.CommandManager.perform(CommandManager.java:43)
    at de.nameddaniel.bot.listener.CommandListener.onMessageReceived(CommandListener.java:25)
    at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:430)
    at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:82)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:69)
    at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:147)
    at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:122)
    at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:36)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:948)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:835)
    at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:813)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:986)
    at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
    at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
    at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
    at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
    at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)

我能弄清楚的是,我的列表表情是空的。但我不知道为什么..

大部分代码是德语。所以问我是否需要翻译。 如果有什么遗漏也请告诉我。

希望你能帮助我,丹尼尔:D

【问题讨论】:

【参考方案1】:

错误是,我将 Emote 与 Emoji 混淆了。所以它必须是表情符号而不是表情。

【讨论】:

以上是关于如何对命令中的表情做出反应 (Discord JDA)?的主要内容,如果未能解决你的问题,请参考以下文章

Discord JS - 如何对同一个嵌入做出多次反应?

对表情符号 discord.py 做出反应时添加角色

Discord.js DiscordAPI 错误:未知表情符号 - 对嵌入做出反应

当用户对消息做出反应时,机器人会在 discord.js 中发送另一条消息

如何在调用函数时使用 discord.py 添加反应?

有没有办法对使用 discord.js 发送的每条消息做出反应