公告命令中的换行符不起作用
Line Breaks In Announcement Command Not Working
我目前正在学习 Java 并决定开发一个 discord 机器人只是为了好玩,但我遇到了 运行 问题。我正在研究一个 announce 命令,该命令会向某个频道发送公告,但问题是在输入时换行符不会显示在输出中(注意:我使用的是普通换行符“shift+enter” ,我没有使用“\n”)。例如,这是我输入的一个输入:
-announce Hello there! Hope you're having a wonderful day!
Hello there again! I hope YOU'RE having a wonderful day!
Test
Test
但是它将嵌入发送到频道,如下所示:
image
这是我的代码:
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
String[] args = event.getMessage().getContentRaw().split("\s+");
String msg = event.getMessage().getContentRaw();
if (args[0].equalsIgnoreCase("-announce")) {
if (args.length < 2) {
EmbedBuilder embed = new EmbedBuilder();
embed
.setDescription("Usage: -announce <test>")
.setColor(Color.CYAN);
event.getChannel().sendMessage(embed.build()).queue();
embed.clear();
} else {
try {
TextChannel textChannel = event.getJDA().getTextChannelById("CHANNEL-ID");
if (textChannel.canTalk()) {
EmbedBuilder embed = new EmbedBuilder();
String message = "";
for (int i = 1; i < args.length; i++) {
message += args[i] + " ";
}
embed
.setColor(Color.CYAN)
.setDescription(message);
textChannel.sendMessage(embed.build()).queue();
embed.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
您正在删除 split("\s+")
中的所有换行符。您应该改用 substring
:
String message = msg.substring(msg.indexOf(' ') + 1);
embed.setDescription(message);
我目前正在学习 Java 并决定开发一个 discord 机器人只是为了好玩,但我遇到了 运行 问题。我正在研究一个 announce 命令,该命令会向某个频道发送公告,但问题是在输入时换行符不会显示在输出中(注意:我使用的是普通换行符“shift+enter” ,我没有使用“\n”)。例如,这是我输入的一个输入:
-announce Hello there! Hope you're having a wonderful day!
Hello there again! I hope YOU'RE having a wonderful day!
Test
Test
但是它将嵌入发送到频道,如下所示: image
这是我的代码:
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
String[] args = event.getMessage().getContentRaw().split("\s+");
String msg = event.getMessage().getContentRaw();
if (args[0].equalsIgnoreCase("-announce")) {
if (args.length < 2) {
EmbedBuilder embed = new EmbedBuilder();
embed
.setDescription("Usage: -announce <test>")
.setColor(Color.CYAN);
event.getChannel().sendMessage(embed.build()).queue();
embed.clear();
} else {
try {
TextChannel textChannel = event.getJDA().getTextChannelById("CHANNEL-ID");
if (textChannel.canTalk()) {
EmbedBuilder embed = new EmbedBuilder();
String message = "";
for (int i = 1; i < args.length; i++) {
message += args[i] + " ";
}
embed
.setColor(Color.CYAN)
.setDescription(message);
textChannel.sendMessage(embed.build()).queue();
embed.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
您正在删除 split("\s+")
中的所有换行符。您应该改用 substring
:
String message = msg.substring(msg.indexOf(' ') + 1);
embed.setDescription(message);