Skip to content

Commit

Permalink
Updated dependencies: lavaplayer 1.3.78 -> 2.0.3, JDA 4.4.1 -> 5.0.0,…
Browse files Browse the repository at this point in the history
… JDA-Utilities 3.0.5 -> integrated and a bunch of smaller stuff too
  • Loading branch information
a9lim committed Nov 19, 2023
1 parent 6d8d6f5 commit 9856f5c
Show file tree
Hide file tree
Showing 32 changed files with 323 additions and 380 deletions.
13 changes: 9 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.4.1_353</version>
<version>5.0.0-beta.17</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.jagrosh</groupId>-->
Expand Down Expand Up @@ -82,17 +82,22 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.9</version>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.2</version>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
<version>1.16.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
</dependencies>

Expand Down
45 changes: 4 additions & 41 deletions src/main/java/hayashi/jdautilities/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,10 @@
import java.util.function.Predicate;
import java.util.regex.Pattern;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.GuildVoiceState;
import net.dv8tion.jda.api.entities.channel.*;
import net.dv8tion.jda.api.entities.channel.concrete.*;

/**
*
* <p>The internal inheritance for Commands used in JDA-Utilities is that of the Command object.
*
* <p>Classes created inheriting this class gain the unique traits of commands operated using the Commands Extension.
* <br>Using several fields, a command can define properties that make it unique and complex while maintaining
* a low level of development.
* <br>All Commands extending this class can define any number of these fields in a object constructor and then
* create the command action/response in the abstract
* {@link Command#execute(CommandEvent) #execute(CommandEvent)} body:
*
* <pre><code> public class ExampleCmd extends Command {
*
* public ExampleCmd() {
* this.name = "example";
* this.aliases = new String[]{"test","demo"};
* this.help = "gives an example of commands do";
* }
*
* {@literal @Override}
* protected void execute(CommandEvent) {
* event.reply("Hey look! This would be the bot's reply if this was a command!");
* }
*
* }</code></pre>
* <p>
* Execution is with the provision of a MessageReceivedEvent-CommandClient wrapper called a
* {@link CommandEvent CommandEvent} and is performed in two steps:
* <ul>
* <li>{@link Command#run(CommandEvent) run} - The command runs
* through a series of conditionals, automatically terminating the command instance if one is not met,
* and possibly providing an error response.</li>
*
* <li>{@link Command#execute(CommandEvent) execute} - The command,
* now being cleared to run, executes and performs whatever lies in the abstract body method.</li>
* </ul>
*
* @author John Grosh (jagrosh)
*/
public abstract class Command {
public static final Pattern COMPILE = Pattern.compile("\\s+");

Expand Down Expand Up @@ -146,7 +109,7 @@ public final void run(CommandEvent event) {
if (p.isChannel()) {
if (p.name().startsWith("VOICE")) {
GuildVoiceState gvc = event.getMember().getVoiceState();
VoiceChannel vc = gvc == null ? null : gvc.getChannel();
VoiceChannel vc = gvc == null ? null : gvc.getChannel().asVoiceChannel();
if (vc == null) {
terminate(event, event.getClient().getError() + " You must be in a voice channel to use that!");
return;
Expand Down
32 changes: 19 additions & 13 deletions src/main/java/hayashi/jdautilities/command/CommandEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
import hayashi.jdautilities.command.impl.CommandClientImpl;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.exceptions.PermissionException;
import net.dv8tion.jda.api.utils.FileUpload;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.api.entities.channel.*;
import net.dv8tion.jda.api.entities.channel.concrete.*;
import net.dv8tion.jda.api.entities.channel.middleman.*;

public class CommandEvent {
private static final Pattern COMPILE = Pattern.compile("<a?:(.+):(\\d+)>");
Expand Down Expand Up @@ -99,22 +105,22 @@ public void reply(MessageEmbed embed, Consumer<Message> success, Consumer<Throwa
}, failure);
}

public void reply(Message message) {
public void reply(MessageCreateData message) {
event.getChannel().sendMessage(message).queue(m -> {
if (event.isFromType(ChannelType.TEXT))
linkId(m);
});
}

public void reply(Message message, Consumer<Message> success) {
public void reply(MessageCreateData message, Consumer<Message> success) {
event.getChannel().sendMessage(message).queue(m -> {
if (event.isFromType(ChannelType.TEXT))
linkId(m);
success.accept(m);
});
}

public void reply(Message message, Consumer<Message> success, Consumer<Throwable> failure) {
public void reply(MessageCreateData message, Consumer<Message> success, Consumer<Throwable> failure) {
event.getChannel().sendMessage(message).queue(m -> {
if (event.isFromType(ChannelType.TEXT))
linkId(m);
Expand All @@ -123,11 +129,11 @@ public void reply(Message message, Consumer<Message> success, Consumer<Throwable
}

public void reply(File file, String filename) {
event.getChannel().sendFile(file, filename).queue();
event.getChannel().sendFiles(FileUpload.fromData(file, filename)).queue();
}

public void reply(String message, File file, String filename) {
event.getChannel().sendFile(file, filename).content(message).queue();
event.getChannel().sendFiles(FileUpload.fromData(file, filename)).setContent(message).queue();
}

public void replyFormatted(String format, Object... args) {
Expand All @@ -144,7 +150,7 @@ public void replyOrAlternate(MessageEmbed embed, String alternateMessage) {

public void replyOrAlternate(String message, File file, String filename, String alternateMessage) {
try {
event.getChannel().sendFile(file, filename).content(message).queue();
event.getChannel().sendFiles(FileUpload.fromData(file, filename)).setContent(message).queue();
} catch (Exception e) {
reply(alternateMessage);
}
Expand Down Expand Up @@ -192,21 +198,21 @@ public void replyInDm(MessageEmbed embed, Consumer<Message> success, Consumer<Th
event.getAuthor().openPrivateChannel().queue(pc -> pc.sendMessageEmbeds(embed).queue(success, failure), failure);
}

public void replyInDm(Message message) {
public void replyInDm(MessageCreateData message) {
if (event.isFromType(ChannelType.PRIVATE))
reply(message);
else
event.getAuthor().openPrivateChannel().queue(pc -> pc.sendMessage(message).queue());
}

public void replyInDm(Message message, Consumer<Message> success) {
public void replyInDm(MessageCreateData message, Consumer<Message> success) {
if (event.isFromType(ChannelType.PRIVATE))
getPrivateChannel().sendMessage(message).queue(success);
else
event.getAuthor().openPrivateChannel().queue(pc -> pc.sendMessage(message).queue(success));
}

public void replyInDm(Message message, Consumer<Message> success, Consumer<Throwable> failure) {
public void replyInDm(MessageCreateData message, Consumer<Message> success, Consumer<Throwable> failure) {
if (event.isFromType(ChannelType.PRIVATE))
getPrivateChannel().sendMessage(message).queue(success, failure);
else
Expand All @@ -217,7 +223,7 @@ public void replyInDm(String message, File file, String filename) {
if (event.isFromType(ChannelType.PRIVATE))
reply(message, file, filename);
else
event.getAuthor().openPrivateChannel().queue(pc -> pc.sendFile(file, filename).content(message).queue());
event.getAuthor().openPrivateChannel().queue(pc -> pc.sendFiles(FileUpload.fromData(file, filename)).setContent(message).queue());
}

public void replySuccess(String message) {
Expand Down Expand Up @@ -267,7 +273,7 @@ public void async(Runnable runnable) {
private void react(String reaction) {
if (reaction != null && !reaction.isEmpty())
try {
event.getMessage().addReaction(COMPILE.matcher(reaction).replaceAll("$1:$2")).queue();
event.getMessage().addReaction(Emoji.fromFormatted(COMPILE.matcher(reaction).replaceAll("$1:$2"))).queue();
} catch (PermissionException ignored) {}
}

Expand Down Expand Up @@ -396,15 +402,15 @@ public Message getMessage() {
}

public PrivateChannel getPrivateChannel() {
return event.getPrivateChannel();
return event.getChannel().asPrivateChannel();
}

public long getResponseNumber() {
return event.getResponseNumber();
}

public TextChannel getTextChannel() {
return event.getTextChannel();
return event.getChannel().asTextChannel();
}

public boolean isFromType(ChannelType channelType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,8 @@

import net.dv8tion.jda.api.entities.Guild;

import javax.annotation.Nullable;

/**
* An implementable frame for classes that handle Guild-Specific
* settings.
*
* <p>Standard implementations should be able to simply provide a
* type of {@link java.lang.Object Object} provided a non-null
* {@link net.dv8tion.jda.api.entities.Guild Guild}. Further
* customization of the implementation is allowed on the developer
* end.
*
* @param <T> The specific type of the settings object.
* @author Kaidan Gustave
* @implNote Unless in the event of a major breaking change to
* JDA, there is no chance of implementations of this
* interface being required to implement additional
* methods.
* <br>If in the future it is decided to add a method
* to this interface, the method will have a default
* implementation that doesn't require developer additions.
* @since 2.0
*/
@FunctionalInterface
public interface GuildSettingsManager<T> {
@Nullable
T getSettings(Guild guild);

default void init() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package hayashi.jdautilities.command;

import javax.annotation.Nullable;
import java.util.Collection;

public interface GuildSettingsProvider {
@Nullable
default Collection<String> getPrefixes() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.ShutdownEvent;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent;
import net.dv8tion.jda.api.events.session.*;
import net.dv8tion.jda.api.hooks.EventListener;
import net.dv8tion.jda.internal.utils.Checks;
import okhttp3.*;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.dv8tion.jda.api.entities.channel.*;
import java.io.IOException;
import java.io.Reader;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -68,6 +67,7 @@ public class CommandClientImpl implements CommandClient, EventListener {
private final ScheduledExecutorService executor;
private final AnnotatedModuleCompiler compiler;
private final GuildSettingsManager manager;
private final MediaType mediaType = MediaType.parse("application/json");

private String textPrefix;
private CommandListener listener;
Expand Down Expand Up @@ -367,7 +367,7 @@ public void onEvent(GenericEvent event) {
if (event instanceof MessageReceivedEvent e)
onMessageReceived(e);

else if (event instanceof GuildMessageDeleteEvent e && usesLinkedDeletion())
else if (event instanceof MessageDeleteEvent e && usesLinkedDeletion())
onMessageDelete(e);

else if (event instanceof GuildJoinEvent e) {
Expand Down Expand Up @@ -444,7 +444,7 @@ private void onMessageReceived(MessageReceivedEvent event) {
listener.onCompletedCommand(cevent, null);
return; // Help Consumer is done
}
if (event.isFromType(ChannelType.PRIVATE) || event.getTextChannel().canTalk()) {
if (event.isFromType(ChannelType.PRIVATE) || event.getChannel().canTalk()) {
final Command command; // this will be null if it's not a command
// this may be cleanable
synchronized (commandIndex) {
Expand Down Expand Up @@ -504,7 +504,7 @@ public void onFailure(Call call, IOException e) {
}

client.newCall(new Request.Builder()
.post(RequestBody.create(MediaType.parse("application/json"), body.toString()))
.post(RequestBody.create(body.toString(),mediaType))
.url("https://discord.bots.gg/api/v1/bots/" + jda.getSelfUser().getId() + "/stats")
.header("Authorization", botsKey)
.header("Content-Type", "application/json")
Expand Down Expand Up @@ -533,15 +533,15 @@ public void onFailure(Call call, IOException e) {
}
}

private void onMessageDelete(GuildMessageDeleteEvent event) {
private void onMessageDelete(MessageDeleteEvent event) {
// We don't need to cover whether or not this client usesLinkedDeletion() because
// that is checked in onEvent(Event) before this is even called.
synchronized (linkMap) {
if (linkMap.contains(event.getMessageIdLong())) {
Set<Message> messages = linkMap.get(event.getMessageIdLong());
if (messages.size() > 1 && event.getGuild().getSelfMember()
.hasPermission(event.getChannel(), Permission.MESSAGE_MANAGE))
event.getChannel().deleteMessages(messages).queue(unused -> {
.hasPermission(event.getChannel().asGuildMessageChannel(), Permission.MESSAGE_MANAGE))
event.getChannel().asGuildMessageChannel().deleteMessages(messages).queue(unused -> {
}, ignored -> {
});
else if (!messages.isEmpty())
Expand Down
Loading

0 comments on commit 9856f5c

Please sign in to comment.