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
22 changes: 8 additions & 14 deletions src/main/java/com/javadiscord/javabot/Bot.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.javadiscord.javabot;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import com.javadiscord.javabot.events.*;
import com.javadiscord.javabot.properties.MultiProperties;
import net.dv8tion.jda.api.JDA;
Expand All @@ -13,39 +13,33 @@
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import org.reflections.Reflections;

import java.lang.reflect.Modifier;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Properties;


public class Bot {

public static JDA jda;
public static EventWaiter waiter;

private static final Properties properties = new MultiProperties(Path.of("bot.props"));

public static void main(String[] args) throws Exception {
waiter = new EventWaiter();

CommandClientBuilder client = new CommandClientBuilder()
CommandClient client = new CommandClientBuilder()
.setOwnerId("374328434677121036")
.setCoOwnerIds("299555811804315648", "620615131256061972")
.setPrefix("!")
.setEmojis("✅", "⚠️", "❌")
.useHelpBuilder(false)
.addCommands(discoverCommands());

.addCommands(discoverCommands())
.build();

jda = JDABuilder.createDefault(properties.getProperty("token", "null"))
JDA jda = JDABuilder.createDefault(properties.getProperty("token", "null"))
.setStatus(OnlineStatus.DO_NOT_DISTURB)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.enableCache(CacheFlag.ACTIVITY)
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES)
.addEventListeners(client, new SlashCommands(client))
.build();

jda.addEventListener(waiter, client.build());

//EVENTS
jda.addEventListener(new GuildJoin());
jda.addEventListener(new UserJoin());
Expand All @@ -57,7 +51,6 @@ public static void main(String[] args) throws Exception {
jda.addEventListener(new CstmCmdListener());
jda.addEventListener(new AutoMod());
jda.addEventListener(new SubmissionListener());
jda.addEventListener(new SlashCommands());
//jda.addEventListener(new StarboardListener());
}

Expand Down Expand Up @@ -96,6 +89,7 @@ private static Command[] discoverCommands() {
return reflections.getSubTypesOf(Command.class).stream()
.map(type -> {
try {
if (Modifier.isAbstract(type.getModifiers())) return null;
return (Command) type.getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
Expand Down
49 changes: 39 additions & 10 deletions src/main/java/com/javadiscord/javabot/SlashCommands.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.javadiscord.javabot;

import com.javadiscord.javabot.commands.other.qotw.Correct;
import com.javadiscord.javabot.commands.user_commands.*;
import com.javadiscord.javabot.other.Constants;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jagrosh.jdautilities.command.CommandClient;
import com.javadiscord.javabot.commands.other.qotw.Correct;
import com.javadiscord.javabot.commands.user_commands.*;
import com.javadiscord.javabot.other.SlashEnabledCommand;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
Expand All @@ -22,16 +23,35 @@
import net.dv8tion.jda.api.requests.restaction.CommandUpdateAction;
import org.bson.Document;

import java.util.HashMap;
import java.util.Map;

import static com.javadiscord.javabot.events.Startup.mongoClient;
import static net.dv8tion.jda.api.interactions.commands.OptionType.STRING;
import static net.dv8tion.jda.api.interactions.commands.OptionType.USER;

public class SlashCommands extends ListenerAdapter {
/**
* Maps every command name and alias to an instance of the command, for
* constant-time lookup.
*/
private final Map<String, SlashEnabledCommand> commandsIndex;

public SlashCommands(CommandClient commandClient) {
this.commandsIndex = new HashMap<>();
this.registerSlashCommands(commandClient);
}

@Override
public void onSlashCommand(SlashCommandEvent event) {
if (event.getGuild() == null) return;

var command = this.commandsIndex.get(event.getName());
if (command != null) {
command.execute(event);
return;
}

switch (event.getName()) {

case "avatar":
Expand Down Expand Up @@ -73,10 +93,6 @@ public void onSlashCommand(SlashCommandEvent event) {
Lmgtfy.exCommand(event, event.getOption("text").getAsString());
break;

case "ping":
Ping.exCommand(event);
break;

case "profile":

OptionMapping profileOption = event.getOption("user");
Expand All @@ -101,12 +117,25 @@ public void onSlashCommand(SlashCommandEvent event) {
}
}


/**
* Registers all slash commands in the command index.
* @param commandClient The command client to register commands from.
*/
private void registerSlashCommands(CommandClient commandClient) {
for (var cmd : commandClient.getCommands()) {
if (cmd instanceof SlashEnabledCommand) {
var slashCommand = (SlashEnabledCommand) cmd;
this.commandsIndex.put(slashCommand.getName(), slashCommand);
for (var alias : slashCommand.getAliases()) {
this.commandsIndex.put(alias, slashCommand);
}
}
}
}

@Override
public void onReady(ReadyEvent event){

CommandUpdateAction commands = Bot.jda.getGuilds().get(0).updateCommands();
CommandUpdateAction commands = event.getJDA().getGuilds().get(0).updateCommands();

// Simple reply Commands
commands.addCommands(
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/javadiscord/javabot/StatusUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void onReady(ReadyEvent event) {
Activity.listening(event.getJDA().getGuilds().get(0).getMemberCount() + " members" + " | " + new Version().getVersion()),
Activity.watching("!help" + " | " + new Version().getVersion())};

Bot.jda.getPresence().setStatus(OnlineStatus.DO_NOT_DISTURB);
Bot.jda.getPresence().setActivity(activities[currentIndex]);
event.getJDA().getPresence().setStatus(OnlineStatus.DO_NOT_DISTURB);
event.getJDA().getPresence().setActivity(activities[currentIndex]);
currentIndex = (currentIndex + 1) % activities.length;
}, 0, 35, TimeUnit.SECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,26 @@
package com.javadiscord.javabot.commands.user_commands;

import com.javadiscord.javabot.events.Startup;
import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import com.javadiscord.javabot.other.Constants;
import com.javadiscord.javabot.other.SlashEnabledCommand;
import com.javadiscord.javabot.other.SlashEnabledCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;

import java.awt.*;

public class Ping extends Command {

public static void exCommand (CommandEvent event) {

long gatewayPing = event.getJDA().getGatewayPing();
String botImage = Startup.bot.getAvatarUrl();

EmbedBuilder eb = new EmbedBuilder()
.setAuthor(gatewayPing + "ms", null, botImage)
.setColor(new Color(0x2F3136));
event.reply(eb.build());
}

public static void exCommand (SlashCommandEvent event) {

long gatewayPing = event.getJDA().getGatewayPing();
String botImage = Startup.bot.getAvatarUrl();

EmbedBuilder eb = new EmbedBuilder()
.setAuthor(gatewayPing + "ms", null, botImage)
.setColor(new Color(0x2F3136));

event.replyEmbeds(eb.build()).queue();
}

public class Ping extends SlashEnabledCommand {
public Ping() {

this.name = "ping";
this.aliases = new String[]{"pong"};
this.category = new Category("USER COMMANDS");
this.help = "Checks Java's gateway ping";
}

protected void execute(CommandEvent event) {

exCommand(event);
@Override
protected void execute(SlashEnabledCommandEvent event) {
long gatewayPing = event.getJDA().getGatewayPing();
String botImage = event.getJDA().getSelfUser().getAvatarUrl();
var e = new EmbedBuilder()
.setAuthor(gatewayPing + "ms", null, botImage)
.setColor(Constants.GRAY)
.build();
event.reply(e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.javadiscord.javabot.other;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;

public abstract class SlashEnabledCommand extends Command {
@Override
protected void execute(CommandEvent event) {
this.execute(new SlashEnabledCommandEvent(event));
}

public void execute(SlashCommandEvent event) {
this.execute(new SlashEnabledCommandEvent(event));
}

protected abstract void execute(SlashEnabledCommandEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.javadiscord.javabot.other;

import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;

import java.util.Optional;

public class SlashEnabledCommandEvent {
private final CommandEvent messageCommandEvent;
private final SlashCommandEvent slashCommandEvent;

private final JDA jda;
private final MessageChannel channel;
private final User user;

public SlashEnabledCommandEvent(CommandEvent messageCommandEvent) {
this.messageCommandEvent = messageCommandEvent;
this.slashCommandEvent = null;
this.jda = messageCommandEvent.getJDA();
this.channel = messageCommandEvent.getChannel();
this.user = messageCommandEvent.getAuthor();
}

public SlashEnabledCommandEvent(SlashCommandEvent slashCommandEvent) {
this.slashCommandEvent = slashCommandEvent;
this.messageCommandEvent = null;
this.jda = slashCommandEvent.getJDA();
this.channel = slashCommandEvent.getChannel();
this.user = slashCommandEvent.getUser();
}

public Optional<CommandEvent> getMessageCommandEvent() {
return Optional.ofNullable(messageCommandEvent);
}

public Optional<SlashCommandEvent> getSlashCommandEvent() {
return Optional.ofNullable(slashCommandEvent);
}

public JDA getJDA() {
return jda;
}

public MessageChannel getChannel() {
return channel;
}

public User getUser() {
return user;
}

public void reply(MessageEmbed embed) {
if (this.messageCommandEvent != null) {
this.messageCommandEvent.reply(embed);
} else {
this.slashCommandEvent.replyEmbeds(embed).queue();
}
}
}