Skip to content

Commit

Permalink
Fix bugs + make messages work in threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Reasonlesss committed Apr 20, 2024
1 parent 76473f5 commit 4a05cef
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.diamondfire.helpbot.util.*;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.interactions.components.buttons.Button;

Expand All @@ -24,7 +25,7 @@

public abstract class AbstractSingleQueryCommand extends Command {

public static void sendMultipleMessage(List<CodeObject> actions, TextChannel channel, long userToWait, BiConsumer<CodeObject, TextChannel> onChosen) {
public static void sendMultipleMessage(List<CodeObject> actions, GuildMessageChannel channel, long userToWait, BiConsumer<CodeObject, GuildMessageChannel> onChosen) {
// This here is to determine if all the duplicate types are the same. If not, we need to make sure that we filter those out first..
CodeObject referenceData = actions.get(0);
Class<? extends CodeObject> classReference = referenceData.getClass();
Expand Down Expand Up @@ -67,7 +68,7 @@ public static void sendMultipleMessage(List<CodeObject> actions, TextChannel cha

// when msg is deleted causes nullpointer when tries to remove reactions! FIX
CodeObject object = buttonMap.get(event.getComponentId());
onChosen.accept(object, message.getChannel().asTextChannel());
onChosen.accept(object, message.getChannel().asGuildMessageChannel());
});
});

Expand All @@ -85,9 +86,9 @@ public void run(CommandEvent event) {
getData(event, onDataReceived());
}

public abstract BiConsumer<CodeObject, TextChannel> onDataReceived();
public abstract BiConsumer<CodeObject, GuildMessageChannel> onDataReceived();

protected void getData(CommandEvent event, BiConsumer<CodeObject, TextChannel> onChosen) {
protected void getData(CommandEvent event, BiConsumer<CodeObject, GuildMessageChannel> onChosen) {
String name = event.getArgument("name");
PresetBuilder preset = new PresetBuilder();

Expand Down Expand Up @@ -120,9 +121,9 @@ protected void getData(CommandEvent event, BiConsumer<CodeObject, TextChannel> o

// If none, proceed. Else we need to special case that.
if (sameActions.size() == 1) {
onChosen.accept(sameActions.get(0), event.getChannel().asTextChannel());
onChosen.accept(sameActions.get(0), event.getChannel().asGuildMessageChannel());
} else if (sameActions.size() > 1) {
sendMultipleMessage(sameActions, event.getChannel().asTextChannel(), event.getMember().getIdLong(), onChosen);
sendMultipleMessage(sameActions, event.getChannel().asGuildMessageChannel(), event.getMember().getIdLong(), onChosen);
}

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.diamondfire.helpbot.util.Util;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import net.dv8tion.jda.api.utils.FileUpload;

import java.io.File;
Expand All @@ -15,7 +16,7 @@

public class CodeCommand extends AbstractSingleQueryCommand {

public static <T extends CodeObject> void sendHelpMessage(T data, TextChannel channel) {
public static <T extends CodeObject> void sendHelpMessage(T data, GuildMessageChannel channel) {
EmbedBuilder builder = data.getEnum().getEmbedBuilder().generateEmbed(data);
String customHead = data.getItem().getHead();

Expand Down Expand Up @@ -57,7 +58,7 @@ public void run(CommandEvent event) {
}

@Override
public BiConsumer<CodeObject, TextChannel> onDataReceived() {
public BiConsumer<CodeObject, GuildMessageChannel> onDataReceived() {
return CodeCommand::sendHelpMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import com.diamondfire.helpbot.util.StringUtil;
import com.google.gson.*;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;

import java.util.function.BiConsumer;


public class RawCommand extends AbstractSingleQueryCommand {

private static void sendRawMessage(CodeObject data, TextChannel channel) {
private static void sendRawMessage(CodeObject data, GuildMessageChannel channel) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(data.getJson());

Expand Down Expand Up @@ -55,7 +56,7 @@ public void run(CommandEvent event) {
}

@Override
public BiConsumer<CodeObject, TextChannel> onDataReceived() {
public BiConsumer<CodeObject, GuildMessageChannel> onDataReceived() {
return RawCommand::sendRawMessage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.diamondfire.helpbot.util.Util;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import net.dv8tion.jda.api.utils.FileUpload;

import java.io.File;
Expand All @@ -15,7 +16,7 @@

public class TagsCommand extends AbstractSingleQueryCommand {

private static void sendTagMessage(CodeObject data, TextChannel channel) {
private static void sendTagMessage(CodeObject data, GuildMessageChannel channel) {
EmbedBuilder builder = new EmbedBuilder();
ActionData actionData;

Expand Down Expand Up @@ -92,7 +93,7 @@ public void run(CommandEvent event) {
}

@Override
public BiConsumer<CodeObject, TextChannel> onDataReceived() {
public BiConsumer<CodeObject, GuildMessageChannel> onDataReceived() {
return TagsCommand::sendTagMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import net.dv8tion.jda.api.utils.FileUpload;

import java.io.*;
Expand Down Expand Up @@ -61,7 +62,7 @@ public Permission getPermission() {
public void run(CommandEvent event) {
List<String> flags = event.getArgument("flag");
if (flags == null) {
setup(event.getChannel().asTextChannel());
setup(event.getChannel().asGuildMessageChannel());
} else {
boolean includeColors = false;
boolean updateDb = true;
Expand All @@ -72,15 +73,15 @@ public void run(CommandEvent event) {
updateDb = false;
}
}
setup(event.getChannel().asTextChannel(), includeColors, updateDb);
setup(event.getChannel().asGuildMessageChannel(), includeColors, updateDb);
}
}

public void setup(TextChannel channel) {
public void setup(GuildMessageChannel channel) {
setup(channel, false, true);
}

public void setup(TextChannel channel, boolean includeColors, boolean updateDb) {
public void setup(GuildMessageChannel channel, boolean includeColors, boolean updateDb) {
EmbedBuilder builder = new EmbedBuilder();

builder.setTitle("Fetching Code Database...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import net.dv8tion.jda.api.utils.FileUpload;

import java.awt.*;
Expand Down Expand Up @@ -44,7 +45,7 @@ public Permission getPermission() {

@Override
public void run(CommandEvent event) {
TextChannel channel = event.getChannel().asTextChannel();
GuildMessageChannel channel = event.getChannel().asGuildMessageChannel();
PresetBuilder attachNbsMsg = new PresetBuilder().withPreset(new InformativeReply(InformativeReplyType.ERROR,"You need to attach an nbs file!"));
PresetBuilder errorMsg = new PresetBuilder().withPreset(new InformativeReply(InformativeReplyType.ERROR,"Something went wrong while generating!"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.diamondfire.helpbot.sys.externalfile.ExternalFileUtil;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import net.dv8tion.jda.api.utils.FileUpload;

import java.io.File;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void run(CommandEvent event) {
);
event.reply(builder);
} else {
TextChannel channel = event.getChannel().asTextChannel();
GuildMessageChannel channel = event.getChannel().asGuildMessageChannel();
channel.getHistory().retrievePast(messagesToRemove).queue((messages) -> {
// Adds the messages to the messageBuilder object
StringBuilder stringBuilder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
import net.dv8tion.jda.api.utils.messages.MessageRequest;

public class ReplyHandler {

private final TextChannel channel;
private final GuildMessageChannel channel;

public ReplyHandler(TextChannel channel) {
public ReplyHandler(GuildMessageChannel channel) {
this.channel = channel;
}

public TextChannel getChannel() {
public GuildMessageChannel getChannel() {
return channel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class CommandEvent extends MessageReceivedEvent {

private Command command;
private final ReplyHandler replyHandler = new ReplyHandler(getChannel().asTextChannel());
private final ReplyHandler replyHandler = new ReplyHandler(getChannel().asGuildMessageChannel());
//TODO Cleanup and refactor this.
// I'd like to see stuff like replying be put into it's whole own section and refactored as well.
private ParsedArgumentSet parsedArgumentSet = null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/diamondfire/helpbot/df/ranks/Rank.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public enum Rank {
this.rankName = rankName;
this.number = number;
this.category = category;
this.emote = HelpBotInstance.getJda().getGuildById(615846886414483465L).getEmojiById(emote);
this.emote = HelpBotInstance.getJda().getGuildById(615846886414483465L).getEmojisByName(emote, false).get(0);
}

public String getRankName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public boolean accept(Message message) {
try {
// Get Tag and send response
TagHandler.getTag(parsedText)
.sendResponse(message.getChannel().asTextChannel(), message.getAuthor());
.sendResponse(message.getChannel().asGuildMessageChannel(), message.getAuthor());

} catch (TagDoesNotExistException | IOException ignored) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/diamondfire/helpbot/sys/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void setImage(String image) {
this.image = image;
}

public void sendResponse(TextChannel channel, @NotNull User requester) {
public void sendResponse(GuildMessageChannel channel, @NotNull User requester) {

EmbedBuilder embed = new EmbedBuilder()
.setTitle(getTitle())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void run() {
.queue();
return;
}
if (member.getRoles().contains(role)) {
if (!vipIds.contains(member.getIdLong()) && member.getRoles().contains(role)) {
guild.removeRoleFromMember(member, role)
.reason("User's VIP Pass has expired")
.queue();
Expand Down

0 comments on commit 4a05cef

Please sign in to comment.