Skip to content

Commit c4a3df0

Browse files
committed
Add xkcd command
Also adds an order to the paginator, and the option to use a custom button factory
1 parent d7ca149 commit c4a3df0

20 files changed

+641
-26
lines changed

src/commander/java/com/mcmoddev/mmdbot/commander/commands/QuoteCommand.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.mcmoddev.mmdbot.commander.quotes.UserReference;
3232
import com.mcmoddev.mmdbot.core.commands.component.Component;
3333
import com.mcmoddev.mmdbot.core.commands.paginate.PaginatedCommand;
34+
import com.mcmoddev.mmdbot.core.commands.paginate.Paginator;
3435
import io.github.matyrobbrt.eventdispatcher.LazySupplier;
3536
import net.dv8tion.jda.api.EmbedBuilder;
3637
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
@@ -302,7 +303,14 @@ public static class ListQuotes extends PaginatedCommand {
302303
* Sets all the usual flags.
303304
*/
304305
private ListQuotes() {
305-
super(TheCommander.getComponentListener("list-quotes-cmd"), Component.Lifespan.TEMPORARY, 10);
306+
super(Paginator
307+
.builder(TheCommander.getComponentListener("list-quotes-cmd"))
308+
.buttonsOwnerOnly(false)
309+
.itemsPerPage(10)
310+
.lifespan(Component.Lifespan.TEMPORARY)
311+
.dismissible(true)
312+
.buttonOrder(Paginator.ButtonType.FIRST, Paginator.ButtonType.PREVIOUS, Paginator.ButtonType.DISMISS, Paginator.ButtonType.NEXT, Paginator.ButtonType.LAST)
313+
);
306314
name = "list";
307315
help = "Get all quotes.";
308316
category = new Category("Fun");
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
21+
package com.mcmoddev.mmdbot.commander.commands;
22+
23+
import com.jagrosh.jdautilities.command.SlashCommandEvent;
24+
import com.mcmoddev.mmdbot.commander.TheCommander;
25+
import com.mcmoddev.mmdbot.commander.annotation.RegisterSlashCommand;
26+
import com.mcmoddev.mmdbot.core.commands.paginate.PaginatedCommand;
27+
import com.mcmoddev.mmdbot.core.commands.paginate.Paginator;
28+
import com.mcmoddev.mmdbot.core.commands.paginate.PaginatorBuilder;
29+
import com.mcmoddev.mmdbot.core.util.Constants;
30+
import com.mcmoddev.mmdbot.core.util.event.DismissListener;
31+
import io.github.matyrobbrt.curseforgeapi.util.Utils;
32+
import lombok.extern.slf4j.Slf4j;
33+
import net.dv8tion.jda.api.EmbedBuilder;
34+
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
35+
import net.dv8tion.jda.api.interactions.commands.OptionType;
36+
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
37+
import net.dv8tion.jda.api.interactions.components.buttons.Button;
38+
39+
import java.io.IOException;
40+
import java.io.InputStreamReader;
41+
import java.net.URL;
42+
import java.util.List;
43+
import java.util.Random;
44+
45+
@Slf4j
46+
public class XkcdCommand extends PaginatedCommand {
47+
48+
public static final URL LATEST_COMIC = Utils.rethrowSupplier(() -> new URL("https://xkcd.com/info.0.json")).get();
49+
public static final Random RANDOM = new Random();
50+
51+
@RegisterSlashCommand
52+
public static final XkcdCommand COMMAND = new XkcdCommand(Paginator.builder(TheCommander.getComponentListener("xkcd-cmd"))
53+
.itemsPerPage(1)
54+
.dismissible(true)
55+
.buttonsOwnerOnly(true)
56+
.buttonOrder(Paginator.ButtonType.FIRST, Paginator.ButtonType.PREVIOUS, Paginator.ButtonType.DISMISS, Paginator.ButtonType.NEXT, Paginator.ButtonType.LAST)
57+
);
58+
59+
private XkcdCommand(final PaginatorBuilder paginator) {
60+
super(paginator);
61+
name = "xkcd";
62+
help = "Resolves a xkcd comic.";
63+
options = List.of(
64+
new OptionData(OptionType.INTEGER, "number", "The number of the comic to get. Defaults to random. Use -1 for the latest comic.")
65+
);
66+
guildOnly = false;
67+
}
68+
69+
@Override
70+
protected EmbedBuilder getEmbed(final int startingIndex, final int maximum, final List<String> arguments) {
71+
try {
72+
final var xkcd = getComic(startingIndex + 1);
73+
return new EmbedBuilder()
74+
.setTitle(xkcd.safe_title())
75+
.setDescription(xkcd.alt())
76+
.setImage(xkcd.img());
77+
} catch (IOException e) {
78+
log.warn("Exception trying to resolve comic ", e);
79+
return new EmbedBuilder()
80+
.setDescription("There was an exception trying to retrieve that comic: " + e.getLocalizedMessage());
81+
}
82+
}
83+
84+
@Override
85+
protected void execute(final SlashCommandEvent event) {
86+
event.deferReply()
87+
.queue(hook -> {
88+
try {
89+
final var latest = getLatestComic().num();
90+
var id = event.getOption("number", RANDOM.nextInt(latest - 1) + 1, OptionMapping::getAsInt);
91+
if (id == -1) {
92+
id = latest;
93+
}
94+
hook.editOriginal(paginator.createPaginatedMessage(id - 1, latest, event.getUser().getIdLong())).queue();
95+
} catch (IOException e) {
96+
hook.editOriginal("There was an exception executing that command: " + e.getLocalizedMessage())
97+
.setActionRow(DismissListener.createDismissButton())
98+
.queue();
99+
log.error("Exception executing command ", e);
100+
}
101+
});
102+
}
103+
104+
public Xkcd getLatestComic() throws IOException {
105+
try (final var reader = new InputStreamReader(LATEST_COMIC.openStream())) {
106+
return Constants.Gsons.NO_PRETTY_PRINTING.fromJson(reader, Xkcd.class);
107+
}
108+
}
109+
110+
public Xkcd getComic(int number) throws IOException {
111+
try (final var reader = new InputStreamReader(new URL("https://xkcd.com/%s/info.0.json".formatted(number)).openStream())) {
112+
return Constants.Gsons.NO_PRETTY_PRINTING.fromJson(reader, Xkcd.class);
113+
}
114+
}
115+
116+
public record Xkcd(String month, int num, String link, String year, String news, String safe_title, String transcript, String alt, String img, String title, String day) { }
117+
118+
}

src/commander/java/com/mcmoddev/mmdbot/commander/docs/ConfigBasedElementLoader.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.commander.docs;
222

323
import com.mcmoddev.mmdbot.core.util.ConfigurateUtils;

src/commander/java/com/mcmoddev/mmdbot/commander/docs/DocsButtonData.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.commander.docs;
222

323
import java.util.List;

src/commander/java/com/mcmoddev/mmdbot/commander/docs/DocsButtonType.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.commander.docs;
222

323
import com.mcmoddev.mmdbot.core.commands.component.Component;

src/commander/java/com/mcmoddev/mmdbot/commander/docs/DocsCommand.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.commander.docs;
222

323
import com.jagrosh.jdautilities.command.SlashCommand;

src/commander/java/com/mcmoddev/mmdbot/commander/docs/DocsConfig.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.commander.docs;
222

323
import com.google.common.collect.Lists;

src/commander/java/com/mcmoddev/mmdbot/commander/docs/DocsEmbed.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.commander.docs;
222

323
import static java.util.stream.Collectors.groupingBy;

src/commander/java/com/mcmoddev/mmdbot/commander/docs/DocsSender.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.commander.docs;
222

323
import de.ialistannen.javadocapi.model.JavadocElement;

src/commander/java/com/mcmoddev/mmdbot/commander/docs/MultipleResultsButtonData.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
121
package com.mcmoddev.mmdbot.commander.docs;
222

323
import de.ialistannen.javadocapi.querying.FuzzyQueryResult;

0 commit comments

Comments
 (0)