Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

java - How to work with multiple strings and different types of data on a single command line with Discord JDA?

I want make a Event Manager function for my Discord bot but I have a problem to resolve. The bot uses the split() method to read each string and thus execute a command. However, one day, I ran into a problem of needing to return multiple strings (for example, an event title, which can vary in lenght). I managed to get around using StringBuffer(), but the way I did it's only sufficient if there is no other type of data after this string sequence. The split() method makes it much easier to create commands, because I can work with the indexes, but it is getting in the way when I need to join multiple strings and work with other types of data after.

Currently the command to create an event, in summary, works as follows:

$event title [Str title]
$event url [Str url]
$event slots [int slots]

How I would like it to work:

$event '[Str title]' [Str url] [int slots]

If the title consists of multiple strings, it must have quotation marks at the beginning and end and everything inside is part of it.

This is an example of code I currently use:

public class EventManager extends ListenerAdapter {

private String title;

public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
     
     String[] message = e.getMessage().getContentRaw().split(" ");
     EmbedBuilder eb = new EmbedBuilder();

     if (message[0].equalsIgnoreCase("$event") && message[1].equalsIgnoreCase("title")) {
        
         StringBuffer sb = new StringBuffer();
         for (int i  = 2; i < message.length; i++) {
            sb.append(message[i]);
            sb.append(" ");
         }
            
         this.title = sb.toString();
         
         eb.setColor(Color.GREEN);
         eb.setDescription("Title: " + title);
         e.getChannel().sendMessage(eb.build()).queue();

    }

}

}

Is there an efficient way to do this? That is, on a single command line read different types of data?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...