Core bot should be fully operational

This commit is contained in:
Steven Tracey 2022-08-11 13:16:16 -04:00
parent caa1c93027
commit 4764f862ac
19 changed files with 801 additions and 104 deletions

137
.gitignore vendored
View File

@ -1,26 +1,128 @@
# ---> Java
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
# ---> Windows
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# ---> JetBrains
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
# ---> Gradle
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
**/build/
!src/**/build/
# Ignore Gradle GUI config
gradle-app.setting
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
# Cache of project
.gradletasknamecache
# Eclipse Gradle plugin generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
@ -40,4 +142,9 @@ bin/
### Mac OS ###
.DS_Store
*-config.yml
# Ignore Gradle build output directory
build
*-config.yml
modules/

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
ModularBot

6
.idea/compiler.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="17" />
</component>
</project>

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$PROJECT_DIR$/../../../../../Program Files/Gradle" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>

30
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven" />
<option name="name" value="maven" />
<option name="url" value="https://m2.dv8tion.net/releases" />
</remote-repository>
<remote-repository>
<option name="id" value="maven2" />
<option name="name" value="maven2" />
<option name="url" value="https://jitpack.io" />
</remote-repository>
</component>
</project>

View File

@ -1,56 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="420bf329-9419-449f-bf0a-58df27b47f89" name="Changes" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ExternalProjectsData">
<projectState path="$PROJECT_DIR$">
<ProjectState />
</projectState>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="2D8GsxSkIL4J0dvFDWfVQ79cCs2" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false"
}
}]]></component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="420bf329-9419-449f-bf0a-58df27b47f89" name="Changes" comment="" />
<created>1660073510005</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1660073510005</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>

View File

@ -8,7 +8,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.nevets.modbot.api.BotModule;
import tech.nevets.modbot.api.Config;
import tech.nevets.modbot.util.commands.CommandListener;
import tech.nevets.modbot.util.commands.CoreListener;
import javax.security.auth.login.LoginException;
import java.util.*;
@ -24,10 +24,11 @@ public class CoreBot {
CORE_CONFIG.addDefaults(getDefaults());
CORE_CONFIG.loadConfig();
modules = ModuleLoader.loadModules();
CoreListener coreListener = new CoreListener();
JDABuilder builder = JDABuilder.createDefault(CORE_CONFIG.getConfig().getString("bot.token"))
.enableCache(CacheFlag.VOICE_STATE)
.addEventListeners(new CommandListener());
.addEventListeners(coreListener);
for (ListenerAdapter listener : PLUGIN_LISTENERS) {
builder.addEventListeners(listener);
@ -39,8 +40,9 @@ public class CoreBot {
e.printStackTrace();
}
//CommandManager.registerSlashCommands();
coreListener.getCommandRegistry().registerSlashCommands();
for (BotModule module : modules) {
module.loadCommandRegistry().registerSlashCommands();
module.onEnable(jda);
}
@ -50,6 +52,8 @@ public class CoreBot {
PLUGIN_LISTENERS.add(listener);
}
private static Map<String, Object> getDefaults() {
Map<String, Object> defaults = new HashMap<>();
defaults.put("bot.token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

View File

@ -1,11 +1,14 @@
package tech.nevets.modbot.api;
import net.dv8tion.jda.api.JDA;
import tech.nevets.modbot.api.commands.CommandRegistry;
public interface BotModule {
default void onPreEnable() {}
CommandRegistry loadCommandRegistry();
void onEnable(JDA jda);
void onDisable();

View File

@ -0,0 +1,154 @@
package tech.nevets.modbot.api.commands;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.nevets.modbot.CoreBot;
import tech.nevets.modbot.util.commands.CommandContext;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class CommandRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandRegistry.class);
private final List<ICommand> commands = new ArrayList<>();
private final List<IPrefixCommand> prefixCommands = new ArrayList<>();
private final List<ISlashCommand> slashCommands = new ArrayList<>();
public CommandRegistry() {}
public void addCommand(ICommand cmd) {
boolean nameFound = commands.stream().anyMatch(it -> it.getName().equalsIgnoreCase(cmd.getName()));
if (nameFound) {
throw new IllegalArgumentException("A command with that name is already present!");
}
commands.add(cmd);
if (cmd instanceof IPrefixCommand) {
prefixCommands.add((IPrefixCommand) cmd);
}
if (cmd instanceof ISlashCommand) {
slashCommands.add((ISlashCommand) cmd);
}
}
public void registerSlashCommands() {
if (slashCommands.size() > 0) {
List<CommandData> cmdDataList = new ArrayList<>();
for (ISlashCommand slashCmd : slashCommands) {
cmdDataList.add(slashCmd.getCommandData());
}
CoreBot.jda.updateCommands().addCommands(cmdDataList).queue();
}
}
@Nullable
public IPrefixCommand getPrefixCommand(String search) {
String searchLower = search.toLowerCase();
for (IPrefixCommand cmd : prefixCommands) {
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
return cmd;
}
}
return null;
}
@Nullable
public ISlashCommand getSlashCommand(String search) {
String searchLower = search.toLowerCase();
for (ISlashCommand cmd : slashCommands) {
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
return cmd;
}
}
return null;
}
public List<ICommand> getCommands() {
return commands;
}
public List<IPrefixCommand> getPrefixCommands() {
return prefixCommands;
}
public List<ISlashCommand> getSlashCommands() {
return slashCommands;
}
public boolean hasPermission(Member member, ICommand cmd) {
List<Role> memberRoles = member.getRoles();
int permLevel = 0;
for (Role memberRole : memberRoles) {
switch (memberRole.getName().toLowerCase()) {
case "helper", "1":
if (permLevel < 1) permLevel = 1;
continue;
case "moderator", "mod", "2":
if (permLevel < 2) permLevel = 2;
continue;
case "administrator", "admin", "3":
if (permLevel < 3) permLevel = 3;
continue;
case "owner", "4":
if (permLevel < 4) permLevel = 4;
}
}
return permLevel >= cmd.getPermissionLevel();
}
public void handle(GuildMessageReceivedEvent event) {
String[] split = event.getMessage().getContentRaw()
.replaceFirst("(?i)" + Pattern.quote(CoreBot.CORE_CONFIG.getConfig().getString("bot.prefix")), "")
.split("\\s+");
String invoke = split[0].toLowerCase();
IPrefixCommand cmd = getPrefixCommand(invoke);
assert event.getMember() != null;
if (!hasPermission(event.getMember(), cmd)) {
event.getMessage().reply("You do not have permission to use this command.").queue();
return;
}
if (cmd != null) {
event.getChannel().sendTyping().queue();
List<String> args = Arrays.asList(split).subList(1, split.length);
CommandContext ctx = new CommandContext(event, args);
cmd.handle(ctx);
}
}
public void handle(SlashCommandEvent event) {
String invoke = event.getName();
ISlashCommand cmd = getSlashCommand(invoke);
if (cmd != null) {
event.deferReply().queue();
assert event.getMember() != null;
if (!hasPermission(event.getMember(), cmd)) {
event.getHook().sendMessage("You do not have permission to use this command.").queue();
return;
}
CommandContext ctx = new CommandContext(event);
cmd.handleSlash(ctx);
}
}
}

View File

@ -1,4 +1,22 @@
package tech.nevets.modbot.api.commands;
import java.util.List;
public interface ICommand {
String getName();
/**
* @apiNote 0 = default, 1 = helper, 2 = moderator, 3 = admin, 4 = owner
* @return int
*/
default int getPermissionLevel() {
return 0;
}
String getHelp();
default List<String> getAliases(){
return List.of();
}
}

View File

@ -1,4 +1,9 @@
package tech.nevets.modbot.api.commands;
import tech.nevets.modbot.util.commands.CommandContext;
public interface IPrefixCommand extends ICommand {
void handle(CommandContext ctx);
}

View File

@ -1,4 +1,14 @@
package tech.nevets.modbot.api.commands;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import tech.nevets.modbot.util.commands.CommandContext;
public interface ISlashCommand extends ICommand {
void handleSlash(CommandContext ctx);
String getDescription();
CommandData getCommandData();
}

View File

@ -0,0 +1,36 @@
package tech.nevets.modbot.util.commands;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import java.util.List;
public class CommandContext implements ICommandContext {
private GuildMessageReceivedEvent event;
private SlashCommandEvent slashEvent;
private List<String> args;
public CommandContext(GuildMessageReceivedEvent event, List<String> args) {
this.event = event;
this.args = args;
}
public CommandContext(SlashCommandEvent slashEvent) {
this.slashEvent = slashEvent;
this.args = null;
}
@Override
public GuildMessageReceivedEvent getEvent() {
return this.event;
}
@Override
public SlashCommandEvent getSlashEvent() {
return this.slashEvent;
}
public List<String> getArgs() {
return this.args;
}
}

View File

@ -1,10 +0,0 @@
package tech.nevets.modbot.util.commands;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CommandListener extends ListenerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandListener.class);
private final CommandManager commandManager = new CommandManager();
}

View File

@ -1,9 +1,71 @@
package tech.nevets.modbot.util.commands;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.nevets.modbot.api.commands.CommandRegistry;
import tech.nevets.modbot.api.commands.ICommand;
import tech.nevets.modbot.api.commands.IPrefixCommand;
import tech.nevets.modbot.api.commands.ISlashCommand;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class CommandManager {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandManager.class);
public static final List<ICommand> ALL_COMMANDS = new ArrayList<>();
public static final List<IPrefixCommand> ALL_PREFIX_COMMANDS = new ArrayList<>();
public static final List<ISlashCommand> ALL_SLASH_COMMANDS = new ArrayList<>();
public static void addFromRegistry(CommandRegistry registry) {
ALL_COMMANDS.addAll(registry.getCommands());
ALL_PREFIX_COMMANDS.addAll(registry.getPrefixCommands());
ALL_SLASH_COMMANDS.addAll(registry.getSlashCommands());
}
@Nullable
public static ICommand getCommand(String search) {
String searchLower = search.toLowerCase();
for (ICommand cmd : ALL_COMMANDS) {
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
return cmd;
}
}
return null;
}
public static List<ICommand> getAllCommands() {
return ALL_COMMANDS;
}
@Nullable
public static IPrefixCommand getPrefixCommand(String search) {
String searchLower = search.toLowerCase();
for (IPrefixCommand cmd : ALL_PREFIX_COMMANDS) {
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
return cmd;
}
}
return null;
}
public static List<IPrefixCommand> getAllPrefixCommands() {
return ALL_PREFIX_COMMANDS;
}
@Nullable
public static ISlashCommand getSlashCommand(String search) {
String searchLower = search.toLowerCase();
for (ISlashCommand cmd : ALL_SLASH_COMMANDS) {
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
return cmd;
}
}
return null;
}
public static List<ISlashCommand> getAllSlashCommands() {
return ALL_SLASH_COMMANDS;
}
}

View File

@ -0,0 +1,52 @@
package tech.nevets.modbot.util.commands;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.nevets.modbot.CoreBot;
import tech.nevets.modbot.api.commands.CommandRegistry;
import javax.annotation.Nonnull;
public class CoreListener extends ListenerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(CoreListener.class);
private final CommandRegistry registry = new CommandRegistry();
{
registry.addCommand(new HelpCmd());
registry.addCommand(new TestCmd());
CommandManager.addFromRegistry(registry);
}
public CommandRegistry getCommandRegistry() {
return registry;
}
@Override
public void onReady(@Nonnull ReadyEvent event) {
LOGGER.info("BuzzBot is ready: " + event.getJDA().getSelfUser().getAsTag());
}
@Override
public void onGuildMessageReceived(@Nonnull GuildMessageReceivedEvent event) {
User user = event.getAuthor();
String raw = event.getMessage().getContentRaw();
if (user.isBot() || event.isWebhookMessage()) {
return;
}
if (raw.startsWith(CoreBot.CORE_CONFIG.getConfig().getString("bot.prefix"))) {
registry.handle(event);
}
}
@Override
public void onSlashCommand(@Nonnull SlashCommandEvent event) {
registry.handle(event);
}
}

View File

@ -0,0 +1,98 @@
package tech.nevets.modbot.util.commands;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.jetbrains.annotations.NotNull;
import tech.nevets.modbot.CoreBot;
import tech.nevets.modbot.api.commands.ICommand;
import tech.nevets.modbot.api.commands.IPrefixCommand;
import tech.nevets.modbot.api.commands.ISlashCommand;
import java.util.ArrayList;
import java.util.List;
public class HelpCmd implements ISlashCommand {
@Override
public void handleSlash(CommandContext ctx) {
StringBuilder sb = new StringBuilder();
EmbedBuilder eb = new EmbedBuilder();
OptionMapping option = ctx.getSlashEvent().getOption("command");
String cmd = null;
if (option != null) {
cmd = option.getAsString();
}
if (cmd == null){
IPrefixCommand prefixCommand = CommandManager.getPrefixCommand(ctx.getSlashEvent().getName());
ISlashCommand slashCommand = CommandManager.getSlashCommand(ctx.getSlashEvent().getName());
if (prefixCommand == null && slashCommand == null) {
ctx.getSlashEvent().getHook().sendMessage("Nothing found for " + ctx.getSlashEvent()).queue();
return;
}
eb.setTitle("Commands");
CommandManager.getAllCommands().stream().map(ICommand::getName).forEach(
(it) -> sb.append('`').append(CoreBot.CORE_CONFIG.getConfig().getString("bot.prefix")).append(it).append("`\n")
);
eb.addField("", sb.toString(), false);
} else {
ICommand command = CommandManager.getCommand(cmd);
if (command != null) {
eb.setTitle(command.getName());
eb.addField("", command.getHelp(), false);
} else {
eb.setTitle("Error");
eb.addField("", "Unknown command, run `/help` to see a list of available commands", false);
}
}
ctx.getSlashEvent().getHook().sendMessageEmbeds(eb.build()).queue();
}
@Override
public String getName() {
return "help";
}
@Override
public String getHelp() {
return "Shows the list of bot commands\n" +
"Usage: `" + CoreBot.CORE_CONFIG.getConfig().getString("bot.prefix") + "help [command]`";
}
@Override
public String getDescription() {
return "Lists bot commands";
}
@Override
public @NotNull CommandData getCommandData() {
List<Command.Choice> choices = new ArrayList<>();
for (IPrefixCommand cmd : CommandManager.getAllPrefixCommands()) {
choices.add(new Command.Choice(cmd.getName(), cmd.getName()));
}
for (ISlashCommand cmd : CommandManager.getAllSlashCommands()) {
choices.add(new Command.Choice(cmd.getName(), cmd.getName()));
}
OptionData option = new OptionData(OptionType.STRING, "command", "Gives info on specified command", false);
option.addChoices(choices);
return new CommandData(this.getName(), this.getDescription())
.addOptions(option);
}
}

View File

@ -0,0 +1,150 @@
package tech.nevets.modbot.util.commands;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.sharding.ShardManager;
public interface ICommandContext {
/**
* Returns the {@link net.dv8tion.jda.api.entities.Guild} for the current command/event
*
* @return the {@link net.dv8tion.jda.api.entities.Guild} for this command/event
*/
default Guild getGuild() {
if (this.getEvent() == null) {
return this.getSlashEvent().getGuild();
} else if (this.getSlashEvent() == null) {
return this.getEvent().getGuild();
}
return null;
}
/**
* Returns the {@link net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent message event} that was received for this instance
*
* @return the {@link net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent message event} that was received for this instance
*/
GuildMessageReceivedEvent getEvent();
/**
* Returns the {@link net.dv8tion.jda.api.events.interaction.SlashCommandEvent interaction event} that was received for this instance
*
* @returns the {@link net.dv8tion.jda.api.events.interaction.SlashCommandEvent interaction event} that was received for this instance
*/
SlashCommandEvent getSlashEvent();
/**
* Returns the {@link net.dv8tion.jda.api.entities.TextChannel channel} that the message for this event was send in
*
* @return the {@link net.dv8tion.jda.api.entities.TextChannel channel} that the message for this event was send in
*/
default TextChannel getChannel() {
if (this.getEvent() == null) {
return this.getSlashEvent().getTextChannel();
} else if (this.getSlashEvent() == null) {
return this.getEvent().getChannel();
}
return null;
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.Message message} that triggered this event
*
* @return the {@link net.dv8tion.jda.api.entities.Message message} that triggered this event
*/
default Message getMessage() {
if (this.getEvent() == null) {
return null;
} else if (this.getSlashEvent() == null) {
return this.getEvent().getMessage();
}
return null;
}
default String getCommandString() {
if (this.getEvent() == null) {
return this.getSlashEvent().getCommandString();
}
return null;
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.User author} of the message as user
*
* @return the {@link net.dv8tion.jda.api.entities.User author} of the message as user
*/
default User getAuthor() {
if (this.getEvent() == null) {
return this.getSlashEvent().getUser();
} else if (this.getSlashEvent() == null) {
return this.getEvent().getAuthor();
}
return null;
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.Member author} of the message as member
*
* @return the {@link net.dv8tion.jda.api.entities.Member author} of the message as member
*/
default Member getMember() {
if (this.getEvent() == null) {
return this.getSlashEvent().getMember();
} else if (this.getSlashEvent() == null) {
return this.getEvent().getMember();
}
return null;
}
/**
* Returns the {@link net.dv8tion.jda.api.interactions.InteractionHook interaction hook} of the slash command
*
* @return the {@link net.dv8tion.jda.api.interactions.InteractionHook interaction hook} of the slash command
*/
default InteractionHook getHook() {
return this.getSlashEvent().getHook();
}
/**
* Returns the current {@link net.dv8tion.jda.api.JDA jda} instance
*
* @return the current {@link net.dv8tion.jda.api.JDA jda} instance
*/
default JDA getJDA() {
if (this.getEvent() == null) {
return this.getSlashEvent().getJDA();
} else if (this.getSlashEvent() == null) {
return this.getEvent().getJDA();
}
return null;
}
/**
* Returns the current {@link net.dv8tion.jda.api.sharding.ShardManager} instance
*
* @return the current {@link net.dv8tion.jda.api.sharding.ShardManager} instance
*/
default ShardManager getShardManager() {
return this.getJDA().getShardManager();
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.User user} for the currently logged in account
*
* @return the {@link net.dv8tion.jda.api.entities.User user} for the currently logged in account
*/
default User getSelfUser() {
return this.getJDA().getSelfUser();
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.Member member} in the guild for the currently logged in account
*
* @return the {@link net.dv8tion.jda.api.entities.Member member} in the guild for the currently logged in account
*/
default Member getSelfMember() {
return this.getGuild().getSelfMember();
}
}

View File

@ -0,0 +1,44 @@
package tech.nevets.modbot.util.commands;
import net.dv8tion.jda.api.entities.IMentionable;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import tech.nevets.modbot.api.commands.ISlashCommand;
public class TestCmd implements ISlashCommand {
@Override
public void handleSlash(CommandContext ctx) {
long number = ctx.getSlashEvent().getOption("number").getAsLong();
IMentionable member = ctx.getSlashEvent().getOption("mention").getAsMentionable();
boolean testBoolean = ctx.getSlashEvent().getOption("boolean").getAsBoolean();
String commandString = ctx.getSlashEvent().getCommandString();
String content = "number: " + number + ", mention: " + member.getAsMention() + ", boolean: " + testBoolean + ", command string: " + commandString;
ctx.getHook().sendMessage(content).queue();
}
@Override
public String getDescription() {
return "Checks the ping between bot and API";
}
@Override
public CommandData getCommandData() {
return new CommandData(this.getName(), this.getDescription())
.addOption(OptionType.INTEGER, "number", "test number", true)
.addOption(OptionType.USER, "mention", "mentioned member or role", true)
.addOption(OptionType.BOOLEAN, "boolean", "test boolean", true);
}
@Override
public String getName() {
return "test";
}
@Override
public String getHelp() {
return "tests api ping";
}
}