Skip to content

Commit 009ec34

Browse files
authored
Merge pull request #127 from Matyrobbrt/fix-tricks
Fix tricks
2 parents 8bac195 + b0f829f commit 009ec34

File tree

21 files changed

+320
-304
lines changed

21 files changed

+320
-304
lines changed

src/main/java/com/mcmoddev/mmdbot/MMDBot.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,18 @@ public static void main(final String[] args) {
159159
.disableCache(CacheFlag.CLIENT_STATUS)
160160
.disableCache(CacheFlag.ONLINE_STATUS)
161161
.addEventListeners(new ThreadedEventListener(new MiscEvents(), GENERAL_EVENT_THREAD_POOL))
162-
.setActivity(Activity.watching("through the mist..."))
163-
.build();
162+
.build().awaitReady();
164163
CommandModule.setupCommandModule();
165164
LoggingModule.setupLoggingModule();
165+
166+
MMDBot.getInstance().getPresence().setActivity(Activity.of(config.getActivityType(), config.getActivityName()));
166167
} catch (final LoginException exception) {
167168
MMDBot.LOGGER.error("Error logging in the bot! Please give the bot a valid token in the config file.",
168169
exception);
169170
System.exit(1);
171+
} catch (InterruptedException e) {
172+
MMDBot.LOGGER.error("Error awaiting caching.", e);
173+
System.exit(1);
170174
}
171175
}
172176

src/main/java/com/mcmoddev/mmdbot/core/BotConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.jagrosh.jdautilities.commons.utils.SafeIdUtil;
3131
import com.mcmoddev.mmdbot.MMDBot;
3232
import net.dv8tion.jda.api.Permission;
33+
import net.dv8tion.jda.api.entities.Activity;
3334
import net.dv8tion.jda.api.entities.Message;
3435
import org.jetbrains.annotations.NotNull;
3536

@@ -503,4 +504,12 @@ public void addRolePanel(final long channelId, final long messageId, final Strin
503504
public boolean isRolePanelPermanent(final long channelId, final long messageId) {
504505
return config.<Boolean>getOrElse("role_panels.%s-%s.permanent".formatted(channelId, messageId), false);
505506
}
507+
508+
public Activity.ActivityType getActivityType() {
509+
return Activity.ActivityType.valueOf(config.getOrElse("bot.activity.type", "PLAYING"));
510+
}
511+
512+
public String getActivityName() {
513+
return config.getOrElse("bot.activity.name", "");
514+
}
506515
}

src/main/java/com/mcmoddev/mmdbot/core/TaskScheduler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,8 @@ public static void init() {
7373
}
7474
}, 0, 14, TimeUnit.DAYS);
7575
}
76+
77+
public static void scheduleTask(Runnable toRun, long delay, TimeUnit unit) {
78+
TIMER.schedule(toRun, delay, unit);
79+
}
7680
}

src/main/java/com/mcmoddev/mmdbot/modules/commands/CommandModule.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdAbout;
2929
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdGist;
3030
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdHelp;
31-
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdUptime;
3231
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdAvatar;
3332
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdRefreshScamLinks;
3433
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdRename;
34+
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdRestart;
3535
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdShutdown;
3636
import com.mcmoddev.mmdbot.modules.commands.contextmenu.GuildOnlyMenu;
3737
import com.mcmoddev.mmdbot.modules.commands.contextmenu.message.ContextMenuAddQuote;
@@ -62,14 +62,11 @@
6262
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdAddTrick;
6363
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdEditTrick;
6464
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdListTricks;
65-
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdRemoveTrick;
6665
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdRunTrick;
67-
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdRunTrickExplicitly;
66+
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdTrick;
6867
import com.mcmoddev.mmdbot.utilities.ThreadedEventListener;
6968
import com.mcmoddev.mmdbot.utilities.Utils;
70-
import com.mcmoddev.mmdbot.utilities.tricks.Tricks;
7169
import me.shedaniel.linkie.Namespaces;
72-
import net.dv8tion.jda.api.entities.Guild;
7370
import net.dv8tion.jda.api.hooks.EventListener;
7471
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
7572

@@ -140,21 +137,20 @@ public static void setupCommandModule() {
140137
new CmdOldChannels(),
141138
new CmdAvatar(),
142139
new CmdRename(),
143-
new CmdUptime(),
144140
//TODO Setup DB storage for tricks and polish them off/add permission restrictions for when needed.
145-
new CmdAddTrick(),
146141
new CmdEditTrick(),
147142
new CmdListTricks(),
148-
new CmdRemoveTrick(),
149-
new CmdRunTrickExplicitly(),
143+
new CmdRunTrick(),
150144
new CmdShutdown(),
145+
new CmdRestart(),
151146
new CmdQuote(),
152147
new CmdRolePanel(),
153-
new CmdWarning());
148+
new CmdWarning(),
149+
new CmdTrick());
154150

155151
addSlashCommand(CmdTranslateMappings.createCommands());
156152
addSlashCommand(CmdMappings.createCommands()); // TODO: This is broken beyond belief. Consider moving away from linkie. - Curle
157-
addSlashCommand(Tricks.getTricks().stream().map(CmdRunTrick::new).toArray(SlashCommand[]::new));
153+
// addSlashCommand(Tricks.getTricks().stream().map(CmdRunTrickSeparated::new).toArray(SlashCommand[]::new));
158154

159155
commandClient.addCommand(new CmdRefreshScamLinks());
160156
commandClient.addCommand(new CmdReact());
@@ -176,8 +172,8 @@ public static void setupCommandModule() {
176172
MMDBot.getInstance().addEventListener(buttonListener(CmdTranslateMappings.ButtonListener.INSTANCE));
177173
MMDBot.getInstance().addEventListener(buttonListener(CmdRoles.getListener()));
178174
MMDBot.getInstance().addEventListener(buttonListener(CmdHelp.getListener()));
179-
MMDBot.getInstance().addEventListener(buttonListener(CmdListTricks.getListener()));
180-
MMDBot.getInstance().addEventListener(buttonListener(CmdQuote.ListQuotes.getListener()));
175+
MMDBot.getInstance().addEventListener(buttonListener(CmdListTricks.getListListener()));
176+
MMDBot.getInstance().addEventListener(buttonListener(CmdQuote.ListQuotes.getQuoteListener()));
181177
MMDBot.LOGGER.warn("Command module enabled and loaded.");
182178
} else {
183179
MMDBot.LOGGER.warn("Command module disabled via config, commands will not work at this time!");

src/main/java/com/mcmoddev/mmdbot/modules/commands/bot/info/CmdAbout.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
import java.awt.Color;
3131
import java.time.Instant;
32+
import java.time.OffsetDateTime;
33+
import java.time.ZoneOffset;
34+
import java.time.temporal.ChronoUnit;
3235

3336
/**
3437
* Shows information about the bot.
@@ -81,6 +84,10 @@ protected void execute(final SlashCommandEvent event) {
8184
true);
8285
embed.addField("Current maintainers:", "jriwanek, WillBL, KiriCattus, sciwhiz12, Curle, matyrobbrt",
8386
true);
87+
embed.addField("I've been online for: ", Utils.getTimeDifference(Utils.getTimeFromUTC(
88+
References.STARTUP_TIME), OffsetDateTime.now(ZoneOffset.UTC),
89+
ChronoUnit.YEARS, ChronoUnit.MONTHS, ChronoUnit.DAYS, ChronoUnit.HOURS, ChronoUnit.HOURS, ChronoUnit.SECONDS)
90+
, false);
8491
embed.setTimestamp(Instant.now());
8592

8693
if (event.isFromGuild() && Utils.memberHasRole(event.getMember(), MMDBot.getConfig().getRole("bot_maintainer"))) {

src/main/java/com/mcmoddev/mmdbot/modules/commands/bot/info/CmdHelp.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
public class CmdHelp extends PaginatedCommand {
5050

5151
private List<Command> commands;
52-
private static CmdHelp.HelpListener listener;
52+
private static ButtonListener helpListener;
5353

5454
public CmdHelp() {
5555
super("help",
@@ -59,15 +59,16 @@ public CmdHelp() {
5959
25);
6060

6161
arguments = "[command]";
62-
listener = new CmdHelp.HelpListener();
62+
this.listener = new PaginatedCommand.ButtonListener();
63+
helpListener = this.listener;
6364
}
6465

6566
/**
6667
* Returns the instance of our button listener.
6768
* Used for handling the pagination buttons.
6869
*/
69-
public static PaginatedCommand.ButtonListener getListener() {
70-
return listener;
70+
public static ButtonListener getListener() {
71+
return helpListener;
7172
}
7273

7374
/**
@@ -136,8 +137,5 @@ protected EmbedBuilder getEmbed(int index) {
136137

137138
return embed;
138139
}
139-
140-
public class HelpListener extends PaginatedCommand.ButtonListener {
141-
}
142140
}
143141

src/main/java/com/mcmoddev/mmdbot/modules/commands/bot/info/CmdUptime.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/main/java/com/mcmoddev/mmdbot/modules/commands/bot/management/CmdRestart.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@
2222

2323
import com.jagrosh.jdautilities.command.Command;
2424
import com.jagrosh.jdautilities.command.CommandEvent;
25+
import com.jagrosh.jdautilities.command.SlashCommand;
26+
import com.jagrosh.jdautilities.command.SlashCommandEvent;
27+
import com.mcmoddev.mmdbot.MMDBot;
28+
import com.mcmoddev.mmdbot.core.TaskScheduler;
29+
30+
import java.util.concurrent.TimeUnit;
2531

2632
/**
2733
* Restart the bot on command rather than via console.
2834
*
2935
* @author KiriCattus
36+
* @author matyrobbrt
3037
*/
31-
public class CmdRestart extends Command {
38+
public class CmdRestart extends SlashCommand {
3239

3340
/**
3441
* Instantiates a new Cmd.
@@ -43,13 +50,14 @@ public CmdRestart() {
4350
guildOnly = false;
4451
}
4552

46-
/**
47-
* Try to restart the command from Discord rather than having to get someone with actual console access.
48-
*
49-
* @param event The event.
50-
*/
5153
@Override
52-
protected void execute(final CommandEvent event) {
53-
//TODO Work on restart code, attempt to make it platform agnostic. -KiriCattus
54+
protected void execute(final SlashCommandEvent event) {
55+
event.reply("Restarting the bot!").queue();
56+
event.getJDA().shutdown();
57+
MMDBot.LOGGER.warn("Restarting the bot by request of {} via Discord!", event.getUser().getName());
58+
TaskScheduler.scheduleTask(() -> {
59+
// TODO some other things may need to be nullified for this to restart with no exceptions!
60+
MMDBot.main(new String[]{});
61+
}, 3, TimeUnit.SECONDS);
5462
}
5563
}

src/main/java/com/mcmoddev/mmdbot/modules/commands/general/PaginatedCommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public abstract class PaginatedCommand extends SlashCommand {
5252
protected int items_per_page = 25;
5353
// The maximum number of items in the list. Update with #updateMaximum
5454
protected int maximum = 0;
55+
protected PaginatedCommand.ButtonListener listener = new ButtonListener();
5556

5657
public PaginatedCommand(String name, String help, boolean guildOnly, List<OptionData> options, int items) {
5758
super();
@@ -106,9 +107,9 @@ protected void sendPaginatedMessage(SlashCommandEvent event) {
106107
* @return A row of buttons to go back and forth by one page.
107108
*/
108109
private ItemComponent[] createScrollButtons(int start) {
109-
Button backward = Button.primary(getName() + "-" + start + "-prev",
110+
Button backward = Button.primary(listener.getButtonID() + "-" + start + "-prev",
110111
Emoji.fromUnicode("◀️")).asDisabled();
111-
Button forward = Button.primary(getName() + "-" + start + "-next",
112+
Button forward = Button.primary(listener.getButtonID() + "-" + start + "-next",
112113
Emoji.fromUnicode("▶️")).asDisabled();
113114

114115
if (start != 0) {
@@ -130,7 +131,7 @@ private ItemComponent[] createScrollButtons(int start) {
130131
* Implement the {@link #getButtonID()} function in any way you like.
131132
* Make sure that this listener is registered to the {@link com.jagrosh.jdautilities.command.CommandClient}.
132133
*/
133-
public abstract class ButtonListener extends ListenerAdapter {
134+
public class ButtonListener extends ListenerAdapter {
134135

135136
public String getButtonID() {
136137
return PaginatedCommand.this.getName();

src/main/java/com/mcmoddev/mmdbot/modules/commands/server/quotes/CmdQuote.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ protected void execute(final SlashCommandEvent event) {
288288
* @author Curle
289289
*/
290290
public class ListQuotes extends PaginatedCommand {
291-
private static QuoteListener listener;
291+
private static ButtonListener quoteListener;
292292

293293
/**
294294
* Create the command.
@@ -299,11 +299,12 @@ public ListQuotes() {
299299
category = new Category("Fun");
300300
guildOnly = true;
301301

302-
listener = new QuoteListener();
302+
this.listener = new QuoteListener();
303+
quoteListener = this.listener;
303304
}
304305

305-
public static QuoteListener getListener() {
306-
return listener;
306+
public static ButtonListener getQuoteListener() {
307+
return quoteListener;
307308
}
308309

309310
@Override
@@ -353,7 +354,7 @@ protected EmbedBuilder getEmbed(int start) {
353354
} else {
354355
// Put it in the description.
355356
// message - author
356-
embed.addField(String.valueOf(fetchedQuote.getID()),
357+
embed.addField(String.valueOf(fetchedQuote.getID()),
357358
fetchedQuote.getQuoteText() + " - " + fetchedQuote.getQuotee().resolveReference(), false);
358359
}
359360
}
@@ -364,7 +365,7 @@ protected EmbedBuilder getEmbed(int start) {
364365
public class QuoteListener extends PaginatedCommand.ButtonListener {
365366
@Override
366367
public String getButtonID() {
367-
return "list";
368+
return "quotelist";
368369
}
369370
}
370371
}

0 commit comments

Comments
 (0)