Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/com/mcmoddev/mmdbot/MMDBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,18 @@ public static void main(final String[] args) {
.disableCache(CacheFlag.CLIENT_STATUS)
.disableCache(CacheFlag.ONLINE_STATUS)
.addEventListeners(new ThreadedEventListener(new MiscEvents(), GENERAL_EVENT_THREAD_POOL))
.setActivity(Activity.watching("through the mist..."))
.build();
.build().awaitReady();
CommandModule.setupCommandModule();
LoggingModule.setupLoggingModule();

MMDBot.getInstance().getPresence().setActivity(Activity.of(config.getActivityType(), config.getActivityName()));
} catch (final LoginException exception) {
MMDBot.LOGGER.error("Error logging in the bot! Please give the bot a valid token in the config file.",
exception);
System.exit(1);
} catch (InterruptedException e) {
MMDBot.LOGGER.error("Error awaiting caching.", e);
System.exit(1);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/mcmoddev/mmdbot/core/BotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.jagrosh.jdautilities.commons.utils.SafeIdUtil;
import com.mcmoddev.mmdbot.MMDBot;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Message;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -503,4 +504,12 @@ public void addRolePanel(final long channelId, final long messageId, final Strin
public boolean isRolePanelPermanent(final long channelId, final long messageId) {
return config.<Boolean>getOrElse("role_panels.%s-%s.permanent".formatted(channelId, messageId), false);
}

public Activity.ActivityType getActivityType() {
return Activity.ActivityType.valueOf(config.getOrElse("bot.activity.type", "PLAYING"));
}

public String getActivityName() {
return config.getOrElse("bot.activity.name", "");
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/mcmoddev/mmdbot/core/TaskScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ public static void init() {
}
}, 0, 14, TimeUnit.DAYS);
}

public static void scheduleTask(Runnable toRun, long delay, TimeUnit unit) {
TIMER.schedule(toRun, delay, unit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdAbout;
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdGist;
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdHelp;
import com.mcmoddev.mmdbot.modules.commands.bot.info.CmdUptime;
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdAvatar;
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdRefreshScamLinks;
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdRename;
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdRestart;
import com.mcmoddev.mmdbot.modules.commands.bot.management.CmdShutdown;
import com.mcmoddev.mmdbot.modules.commands.contextmenu.GuildOnlyMenu;
import com.mcmoddev.mmdbot.modules.commands.contextmenu.message.ContextMenuAddQuote;
Expand Down Expand Up @@ -62,14 +62,11 @@
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdAddTrick;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdEditTrick;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdListTricks;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdRemoveTrick;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdRunTrick;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdRunTrickExplicitly;
import com.mcmoddev.mmdbot.modules.commands.server.tricks.CmdTrick;
import com.mcmoddev.mmdbot.utilities.ThreadedEventListener;
import com.mcmoddev.mmdbot.utilities.Utils;
import com.mcmoddev.mmdbot.utilities.tricks.Tricks;
import me.shedaniel.linkie.Namespaces;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.hooks.EventListener;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;

Expand Down Expand Up @@ -140,21 +137,20 @@ public static void setupCommandModule() {
new CmdOldChannels(),
new CmdAvatar(),
new CmdRename(),
new CmdUptime(),
//TODO Setup DB storage for tricks and polish them off/add permission restrictions for when needed.
new CmdAddTrick(),
new CmdEditTrick(),
new CmdListTricks(),
new CmdRemoveTrick(),
new CmdRunTrickExplicitly(),
new CmdRunTrick(),
new CmdShutdown(),
new CmdRestart(),
new CmdQuote(),
new CmdRolePanel(),
new CmdWarning());
new CmdWarning(),
new CmdTrick());

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

commandClient.addCommand(new CmdRefreshScamLinks());
commandClient.addCommand(new CmdReact());
Expand All @@ -176,8 +172,8 @@ public static void setupCommandModule() {
MMDBot.getInstance().addEventListener(buttonListener(CmdTranslateMappings.ButtonListener.INSTANCE));
MMDBot.getInstance().addEventListener(buttonListener(CmdRoles.getListener()));
MMDBot.getInstance().addEventListener(buttonListener(CmdHelp.getListener()));
MMDBot.getInstance().addEventListener(buttonListener(CmdListTricks.getListener()));
MMDBot.getInstance().addEventListener(buttonListener(CmdQuote.ListQuotes.getListener()));
MMDBot.getInstance().addEventListener(buttonListener(CmdListTricks.getListListener()));
MMDBot.getInstance().addEventListener(buttonListener(CmdQuote.ListQuotes.getQuoteListener()));
MMDBot.LOGGER.warn("Command module enabled and loaded.");
} else {
MMDBot.LOGGER.warn("Command module disabled via config, commands will not work at this time!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

import java.awt.Color;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;

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

if (event.isFromGuild() && Utils.memberHasRole(event.getMember(), MMDBot.getConfig().getRole("bot_maintainer"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
public class CmdHelp extends PaginatedCommand {

private List<Command> commands;
private static CmdHelp.HelpListener listener;
private static ButtonListener helpListener;

public CmdHelp() {
super("help",
Expand All @@ -59,15 +59,16 @@ public CmdHelp() {
25);

arguments = "[command]";
listener = new CmdHelp.HelpListener();
this.listener = new PaginatedCommand.ButtonListener();
helpListener = this.listener;
}

/**
* Returns the instance of our button listener.
* Used for handling the pagination buttons.
*/
public static PaginatedCommand.ButtonListener getListener() {
return listener;
public static ButtonListener getListener() {
return helpListener;
}

/**
Expand Down Expand Up @@ -136,8 +137,5 @@ protected EmbedBuilder getEmbed(int index) {

return embed;
}

public class HelpListener extends PaginatedCommand.ButtonListener {
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import com.mcmoddev.mmdbot.MMDBot;
import com.mcmoddev.mmdbot.core.TaskScheduler;

import java.util.concurrent.TimeUnit;

/**
* Restart the bot on command rather than via console.
*
* @author KiriCattus
* @author matyrobbrt
*/
public class CmdRestart extends Command {
public class CmdRestart extends SlashCommand {

/**
* Instantiates a new Cmd.
Expand All @@ -43,13 +50,14 @@ public CmdRestart() {
guildOnly = false;
}

/**
* Try to restart the command from Discord rather than having to get someone with actual console access.
*
* @param event The event.
*/
@Override
protected void execute(final CommandEvent event) {
//TODO Work on restart code, attempt to make it platform agnostic. -KiriCattus
protected void execute(final SlashCommandEvent event) {
event.reply("Restarting the bot!").queue();
event.getJDA().shutdown();
MMDBot.LOGGER.warn("Restarting the bot by request of {} via Discord!", event.getUser().getName());
TaskScheduler.scheduleTask(() -> {
// TODO some other things may need to be nullified for this to restart with no exceptions!
MMDBot.main(new String[]{});
}, 3, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void execute(final SlashCommandEvent event) {
}, "GuildCommandClearing").start();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
msg.get().editOriginal("Error! Shutdown cancelled!").queue();
event.reply("Error! Shutdown cancelled!").queue();
}
return;
}
Expand All @@ -103,7 +103,7 @@ protected void execute(final SlashCommandEvent event) {
}, "GlobalCommandClearing").start();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
msg.get().editOriginal("Error! Shutdown cancelled!").queue();
event.reply("Error! Shutdown cancelled!").queue();
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public abstract class PaginatedCommand extends SlashCommand {
protected int items_per_page = 25;
// The maximum number of items in the list. Update with #updateMaximum
protected int maximum = 0;
protected PaginatedCommand.ButtonListener listener = new ButtonListener();

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

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

public String getButtonID() {
return PaginatedCommand.this.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ protected void execute(final SlashCommandEvent event) {
* @author Curle
*/
public class ListQuotes extends PaginatedCommand {
private static QuoteListener listener;
private static ButtonListener quoteListener;

/**
* Create the command.
Expand All @@ -299,11 +299,12 @@ public ListQuotes() {
category = new Category("Fun");
guildOnly = true;

listener = new QuoteListener();
this.listener = new QuoteListener();
quoteListener = this.listener;
}

public static QuoteListener getListener() {
return listener;
public static ButtonListener getQuoteListener() {
return quoteListener;
}

@Override
Expand Down Expand Up @@ -353,7 +354,7 @@ protected EmbedBuilder getEmbed(int start) {
} else {
// Put it in the description.
// message - author
embed.addField(String.valueOf(fetchedQuote.getID()),
embed.addField(String.valueOf(fetchedQuote.getID()),
fetchedQuote.getQuoteText() + " - " + fetchedQuote.getQuotee().resolveReference(), false);
}
}
Expand All @@ -364,7 +365,7 @@ protected EmbedBuilder getEmbed(int start) {
public class QuoteListener extends PaginatedCommand.ButtonListener {
@Override
public String getButtonID() {
return "list";
return "quotelist";
}
}
}
Expand Down
Loading