[DISCORD] Lots and lots of bugfixes

- Fixes bug where sending any messages through Discord would result in errors about roles
- Fixes bug where attempting to send any messages to Discord just didn't work
- Discord integration now shows main chat messages in color
This commit is contained in:
Video 2023-03-11 02:38:33 -07:00
parent 071d19c5e0
commit a5135ef641
8 changed files with 114 additions and 142 deletions

View File

@ -86,7 +86,7 @@ public class Bot
public Mono<Guild> getGuildById()
{
return client.getGuildById(SnowflakeEntry.serverID);
return client.getGuildById(SnowflakeEntry.SERVER_ID.getSnowflake());
}
public GatewayDiscordClient getClient()
@ -112,7 +112,7 @@ public class Bot
return false;
}
Guild server = client.getGuildById(SnowflakeEntry.serverID).block();
Guild server = client.getGuildById(SnowflakeEntry.SERVER_ID.getSnowflake()).block();
if (server == null)
{
FLog.severe("The Discord server ID specified is invalid, or the bot is not on the server.");

View File

@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory;
public class TFD4J extends JavaPlugin
{
private final Logger slf4j = LoggerFactory.getLogger("TFD4J");
private final Logger slf4j = getSLF4JLogger();
private Bot bot;
private MinecraftListener mc;
private AdminChatListener ac;

View File

@ -62,11 +62,11 @@ public class TFD4JCommonsImpl implements TFD4JCommons
Mono<Message> sentMessage = tfd4J
.getBot()
.getClient()
.getChannelById(SnowflakeEntry.adminChatChannelID)
.getChannelById(SnowflakeEntry.ADMIN_CHAT_CHANNEL_ID.getSnowflake())
.ofType(TextChannel.class)
.flatMap(c -> c.createMessage(spec));
insert(sentMessage);
sentMessage.subscribe();
}
}
@ -104,11 +104,11 @@ public class TFD4JCommonsImpl implements TFD4JCommons
Mono<Message> sentMessage = tfd4J
.getBot()
.getClient()
.getChannelById(SnowflakeEntry.chatChannelID)
.getChannelById(SnowflakeEntry.CHAT_CHANNEL_ID.getSnowflake())
.ofType(TextChannel.class)
.flatMap(c -> c.createMessage(spec));
insert(sentMessage);
sentMessage.subscribe();
}
}
@ -159,11 +159,11 @@ public class TFD4JCommonsImpl implements TFD4JCommons
.blockOptional()
.orElseThrow();
Role adminRole = server.getRoleById(SnowflakeEntry.adminRoleID)
Role adminRole = server.getRoleById(SnowflakeEntry.ADMIN_ROLE_ID.getSnowflake())
.blockOptional()
.orElseThrow();
Role senioradminRole = server.getRoleById(SnowflakeEntry.seniorRoleID)
Role senioradminRole = server.getRoleById(SnowflakeEntry.SENIOR_ADMIN_ROLE_ID.getSnowflake())
.blockOptional()
.orElseThrow();
@ -282,7 +282,7 @@ public class TFD4JCommonsImpl implements TFD4JCommons
if (server == null) return false;
final TextChannel channel = server.getChannelById(SnowflakeEntry.reportChannelID)
final TextChannel channel = server.getChannelById(SnowflakeEntry.REPORT_CHANNEL_ID.getSnowflake())
.ofType(TextChannel.class)
.blockOptional()
.orElseThrow();
@ -324,7 +324,7 @@ public class TFD4JCommonsImpl implements TFD4JCommons
final Guild server = tfd4J.getBot()
.getClient()
.getGuildById(SnowflakeEntry.serverID)
.getGuildById(SnowflakeEntry.SERVER_ID.getSnowflake())
.block();
if (server == null)
@ -333,7 +333,7 @@ public class TFD4JCommonsImpl implements TFD4JCommons
return false;
}
final TextChannel channel = server.getChannelById(SnowflakeEntry.reportChannelID)
final TextChannel channel = server.getChannelById(SnowflakeEntry.REPORT_CHANNEL_ID.getSnowflake())
.ofType(TextChannel.class)
.blockOptional()
.orElseThrow();

View File

@ -49,7 +49,7 @@ public class AdminChatListener
.blockOptional()
.orElseThrow()
.getId()
.equals(SnowflakeEntry.adminChatChannelID))
.equals(SnowflakeEntry.ADMIN_CHAT_CHANNEL_ID.getSnowflake()))
.filter(m -> !m.getMessage()
.getAuthor()
.orElseThrow()
@ -61,7 +61,7 @@ public class AdminChatListener
public void createMessageSpec(MessageCreateEvent m)
{
Member member = m.getMember().orElseThrow(IllegalAccessError::new);
String tag = tfd4j.getMinecraftListener().getDisplay(member);
String tag = tfd4j.getBot().getTFM().getDisplay(member);
Message msg = m.getMessage();
String mediamessage = ChatColor.YELLOW + "[Media]";
@ -129,28 +129,35 @@ public class AdminChatListener
assert server != null;
return member.getRoles().map(role ->
{
if (role.getId().equals(SnowflakeEntry.ownerRoleID))
if (role.getId().equals(SnowflakeEntry.OWNER_ROLE_ID.getSnowflake()))
{
return Title.OWNER;
} else if (role.getId().equals(SnowflakeEntry.developerRoleID))
}
else if (role.getId().equals(SnowflakeEntry.DEVELOPER_ROLE_ID.getSnowflake()))
{
return Title.DEVELOPER;
} else if (role.getId().equals(SnowflakeEntry.executiveRoleID))
}
else if (role.getId().equals(SnowflakeEntry.EXECUTIVE_ROLE_ID.getSnowflake()))
{
return Title.EXECUTIVE;
} else if (role.getId().equals(SnowflakeEntry.assistantRoleID))
}
else if (role.getId().equals(SnowflakeEntry.ASSISTANT_EXECUTIVE_ROLE_ID.getSnowflake()))
{
return Title.ASSTEXEC;
} else if (role.getId().equals(SnowflakeEntry.seniorRoleID))
}
else if (role.getId().equals(SnowflakeEntry.SENIOR_ADMIN_ROLE_ID.getSnowflake()))
{
return Rank.SENIOR_ADMIN;
} else if (role.getId().equals(SnowflakeEntry.adminRoleID))
}
else if (role.getId().equals(SnowflakeEntry.ADMIN_ROLE_ID.getSnowflake()))
{
return Rank.ADMIN;
} else if (role.getId().equals(SnowflakeEntry.builderRoleID))
}
else if (role.getId().equals(SnowflakeEntry.MASTERBUILDER_ROLE_ID.getSnowflake()))
{
return Title.MASTER_BUILDER;
} else
}
else
{
return null;
}

View File

@ -2,7 +2,6 @@ package me.totalfreedom.discord.listener;
import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.Attachment;
import discord4j.core.object.entity.Guild;
import discord4j.core.object.entity.Member;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.channel.TextChannel;
@ -11,17 +10,14 @@ import me.totalfreedom.discord.TFD4J;
import me.totalfreedom.discord.util.SnowflakeEntry;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.rank.Title;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public class MinecraftListener
{
@ -52,7 +48,7 @@ public class MinecraftListener
.blockOptional()
.orElseThrow(IllegalAccessError::new)
.getId()
.equals(SnowflakeEntry.chatChannelID))
.equals(SnowflakeEntry.CHAT_CHANNEL_ID.getSnowflake()))
.filter(m ->
{
Boolean b = m.getMessage()
@ -67,98 +63,44 @@ public class MinecraftListener
{
Member member = m.getMember().orElseThrow();
Message msg = m.getMessage();
String tag = getDisplay(member);
TextComponent[] emsg = {Component.empty()}; // We are using a single value arrays here to silence SonarLint's "return value never used" bullshit.
String tag = bot.getTFM().getDisplay(member);
emsg[0] = emsg[0].append(Component.text("[", NamedTextColor.DARK_GRAY));
TextComponent[] inviteLink = {Component.text("Discord")};
inviteLink[0] = inviteLink[0].color(NamedTextColor.DARK_AQUA);
HoverEvent<Component> hoverEvent = HoverEvent.showText(Component.text("Click to join our Discord server!"));
ClickEvent clickEvent = ClickEvent.openUrl(ConfigEntry.DISCORD_INVITE_LINK.getString());
inviteLink[0] = inviteLink[0].hoverEvent(hoverEvent);
inviteLink[0] = inviteLink[0].clickEvent(clickEvent);
emsg[0] = emsg[0].append(inviteLink[0]);
emsg[0] = emsg[0].append(Component.text("] ", NamedTextColor.DARK_GRAY));
TextComponent.Builder builder = Component.text();
TextComponent prefix = Component.text("[", NamedTextColor.DARK_GRAY)
.append(Component.text("Discord", NamedTextColor.DARK_AQUA)
.hoverEvent(HoverEvent.showText(Component.text("Click to join our Discord server!")))
.clickEvent(ClickEvent.openUrl(ConfigEntry.DISCORD_INVITE_LINK.getString())))
.append(Component.text("] ", NamedTextColor.DARK_GRAY));
TextComponent user = Component.empty();
// Tag (if they have one)
if (tag != null)
if (tag != null && !tag.isEmpty())
{
emsg[0] = emsg[0].append(Component.text(tag));
user = LegacyComponentSerializer.legacyAmpersand().deserialize(tag).append(Component.space());
}
emsg[0] = emsg[0].append(Component.space());
user = user.append(Component.text(FUtil.stripColors(member.getDisplayName()).trim(), NamedTextColor.RED));
// User
TextComponent[] user = {Component.text(FUtil.stripColors(member.getDisplayName()))};
user[0] = user[0].color(NamedTextColor.RED);
emsg[0] = emsg[0].append(user[0]);
TextComponent message = Component.text(": ", NamedTextColor.DARK_GRAY)
.append(Component.text(FUtil.stripColors(msg.getContent()), NamedTextColor.WHITE));
// Message
emsg[0] = emsg[0].append(Component.text(": ", NamedTextColor.DARK_GRAY));
emsg[0] = emsg[0].append(Component.text(FUtil.stripColors(msg.getContent()), NamedTextColor.WHITE));
// Attachments
if (!msg.getAttachments().isEmpty())
{
if (!msg.getContent().isEmpty())
emsg[0] = emsg[0].append(Component.space());
{
message = message.append(Component.space());
}
for (Attachment attachment : msg.getAttachments())
{
TextComponent[] media = {Component.text("[Media] ")};
media[0] = media[0].color(NamedTextColor.YELLOW);
HoverEvent<Component> hover = HoverEvent.showText(Component.text(attachment.getUrl()));
ClickEvent click = ClickEvent.openUrl(attachment.getUrl());
media[0] = media[0].clickEvent(click);
media[0] = media[0].hoverEvent(hover);
emsg[0] = emsg[0].append(media[0]);
message = message.append(Component.text("[Media] ", NamedTextColor.YELLOW)
.hoverEvent(HoverEvent.showText(Component.text(attachment.getUrl())))
.clickEvent(ClickEvent.openUrl(attachment.getUrl())));
}
}
for (Player player : Bukkit.getOnlinePlayers())
{
if (TotalFreedomMod.getPlugin().pl.getData(player).doesDisplayDiscord())
{
player.sendMessage(emsg[0]);
}
}
FLog.info(emsg[0].content(), true);
}
public String getDisplay(Member member)
{
Guild server = tfd4j.getBot().getGuildById().block();
// Server Owner
assert server != null;
return member.getRoles().map(role ->
{
if (role.getId().equals(SnowflakeEntry.ownerRoleID))
{
return Title.OWNER.getColoredTag();
} else if (role.getId().equals(SnowflakeEntry.developerRoleID))
{
return Title.DEVELOPER.getColoredTag();
} else if (role.getId().equals(SnowflakeEntry.executiveRoleID))
{
return Title.EXECUTIVE.getColoredTag();
} else if (role.getId().equals(SnowflakeEntry.assistantRoleID))
{
return Title.ASSTEXEC.getColoredTag();
} else if (role.getId().equals(SnowflakeEntry.seniorRoleID))
{
return Rank.SENIOR_ADMIN.getColoredTag();
} else if (role.getId().equals(SnowflakeEntry.adminRoleID))
{
return Rank.ADMIN.getColoredTag();
} else if (role.getId().equals(SnowflakeEntry.builderRoleID))
{
return Title.MASTER_BUILDER.getColoredTag();
} else
{
return null;
}
}).blockFirst();
Bukkit.broadcast(builder.append(prefix, user, message).build());
}
}

View File

@ -4,12 +4,10 @@ import discord4j.core.event.domain.message.ReactionAddEvent;
import discord4j.core.object.Embed;
import discord4j.core.object.entity.Member;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.channel.Channel;
import discord4j.core.object.entity.channel.TextChannel;
import discord4j.discordjson.json.MessageCreateRequest;
import me.totalfreedom.discord.TFD4J;
import me.totalfreedom.discord.util.SnowflakeEntry;
import me.totalfreedom.totalfreedommod.util.FLog;
public class ReactionListener
{
@ -34,7 +32,7 @@ public class ReactionListener
.equals(tfd4j.getBot().getClient().getSelfId()))
.filter(r -> !r.getChannel()
.blockOptional()
.orElseThrow().getId().equals(SnowflakeEntry.reportChannelID))
.orElseThrow().getId().equals(SnowflakeEntry.REPORT_CHANNEL_ID.getSnowflake()))
.filter(r -> r.getEmoji()
.asUnicodeEmoji()
.orElseThrow(UnsupportedOperationException::new)
@ -47,7 +45,7 @@ public class ReactionListener
{
final TextChannel archiveChannel = tfd4j.getBot()
.getClient()
.getChannelById(SnowflakeEntry.archiveChannelID)
.getChannelById(SnowflakeEntry.ARCHIVE_REPORT_CHANNEL_ID.getSnowflake())
.ofType(TextChannel.class)
.blockOptional()
.orElseThrow();

View File

@ -3,18 +3,33 @@ package me.totalfreedom.discord.util;
import discord4j.common.util.Snowflake;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
public class SnowflakeEntry
public enum SnowflakeEntry
{
public static final Snowflake adminChatChannelID = Snowflake.of(ConfigEntry.DISCORD_ADMINCHAT_CHANNEL_ID.getString());
public static final Snowflake chatChannelID = Snowflake.of(ConfigEntry.DISCORD_CHAT_CHANNEL_ID.getString());
public static final Snowflake serverID = Snowflake.of(ConfigEntry.DISCORD_SERVER_ID.getString());
public static final Snowflake ownerRoleID = Snowflake.of(ConfigEntry.DISCORD_SERVER_OWNER_ROLE_ID.getString());
public static final Snowflake developerRoleID = Snowflake.of(ConfigEntry.DISCORD_DEVELOPER_ROLE_ID.getString());
public static final Snowflake executiveRoleID = Snowflake.of(ConfigEntry.DISCORD_EXECUTIVE_ROLE_ID.getString());
public static final Snowflake assistantRoleID = Snowflake.of(ConfigEntry.DISCORD_ASSISTANT_EXECUTIVE_ROLE_ID.getString());
public static final Snowflake seniorRoleID = Snowflake.of(ConfigEntry.DISCORD_SENIOR_ADMIN_ROLE_ID.getString());
public static final Snowflake adminRoleID = Snowflake.of(ConfigEntry.DISCORD_NEW_ADMIN_ROLE_ID.getString());
public static final Snowflake builderRoleID = Snowflake.of(ConfigEntry.DISCORD_MASTER_BUILDER_ROLE_ID.getString());
public static final Snowflake reportChannelID = Snowflake.of(ConfigEntry.DISCORD_REPORT_CHANNEL_ID.getString());
public static final Snowflake archiveChannelID = Snowflake.of(ConfigEntry.DISCORD_REPORT_ARCHIVE_CHANNEL_ID.getString());
ADMIN_CHAT_CHANNEL_ID(ConfigEntry.DISCORD_ADMINCHAT_CHANNEL_ID),
CHAT_CHANNEL_ID(ConfigEntry.DISCORD_CHAT_CHANNEL_ID),
REPORT_CHANNEL_ID(ConfigEntry.DISCORD_REPORT_CHANNEL_ID),
ARCHIVE_REPORT_CHANNEL_ID(ConfigEntry.DISCORD_REPORT_ARCHIVE_CHANNEL_ID),
//--
SERVER_ID(ConfigEntry.DISCORD_SERVER_ID),
//--
OWNER_ROLE_ID(ConfigEntry.DISCORD_SERVER_OWNER_ROLE_ID),
DEVELOPER_ROLE_ID(ConfigEntry.DISCORD_DEVELOPER_ROLE_ID),
EXECUTIVE_ROLE_ID(ConfigEntry.DISCORD_EXECUTIVE_ROLE_ID),
ASSISTANT_EXECUTIVE_ROLE_ID(ConfigEntry.DISCORD_ASSISTANT_EXECUTIVE_ROLE_ID),
SENIOR_ADMIN_ROLE_ID(ConfigEntry.DISCORD_SENIOR_ADMIN_ROLE_ID),
ADMIN_ROLE_ID(ConfigEntry.DISCORD_NEW_ADMIN_ROLE_ID),
MASTERBUILDER_ROLE_ID(ConfigEntry.DISCORD_MASTER_BUILDER_ROLE_ID);
private ConfigEntry entry;
SnowflakeEntry(ConfigEntry entry)
{
this.entry = entry;
}
public Snowflake getSnowflake()
{
return Snowflake.of(entry.getString());
}
}

View File

@ -31,36 +31,46 @@ public class TFM_Bridge
{
Guild server = bot.getGuildById().block();
// Server Owner
if (server == null)
{
throw new IllegalStateException();
}
Snowflake ownerID = Snowflake.of(ConfigEntry.DISCORD_SERVER_OWNER_ROLE_ID.getString());
Snowflake developerID = Snowflake.of(ConfigEntry.DISCORD_DEVELOPER_ROLE_ID.getString());
Snowflake executiveID = Snowflake.of(ConfigEntry.DISCORD_EXECUTIVE_ROLE_ID.getString());
Snowflake assistantExecutiveID = Snowflake.of(ConfigEntry.DISCORD_ASSISTANT_EXECUTIVE_ROLE_ID.getString());
Snowflake seniorAdminID = Snowflake.of(ConfigEntry.DISCORD_SENIOR_ADMIN_ROLE_ID.getString());
Snowflake adminID = Snowflake.of(ConfigEntry.DISCORD_NEW_ADMIN_ROLE_ID.getString());
Snowflake masterBuilderID = Snowflake.of(ConfigEntry.DISCORD_MASTER_BUILDER_ROLE_ID.getString());
Snowflake[] ids = {ownerID, developerID, executiveID, assistantExecutiveID, seniorAdminID, adminID, masterBuilderID};
String[] titles = {Title.OWNER.getColoredTag(), Title.DEVELOPER.getColoredTag(), Title.EXECUTIVE.getColoredTag(), Title.ASSTEXEC.getColoredTag(), Rank.SENIOR_ADMIN.getColoredTag(), Rank.ADMIN.getColoredTag(), Title.MASTER_BUILDER.getColoredTag()};
assert server != null;
return member.getRoles().map(role ->
{
for (int i = 0; i < ids.length; i++)
if (role.getId().equals(SnowflakeEntry.OWNER_ROLE_ID.getSnowflake()))
{
if (role.getId().equals(ids[i]))
return Title.OWNER.getColoredTag();
}
else if (role.getId().equals(SnowflakeEntry.DEVELOPER_ROLE_ID.getSnowflake()))
{
return titles[i];
return Title.DEVELOPER.getColoredTag();
}
else if (role.getId().equals(SnowflakeEntry.EXECUTIVE_ROLE_ID.getSnowflake()))
{
return Title.EXECUTIVE.getColoredTag();
}
else if (role.getId().equals(SnowflakeEntry.ASSISTANT_EXECUTIVE_ROLE_ID.getSnowflake()))
{
return Title.ASSTEXEC.getColoredTag();
}
else if (role.getId().equals(SnowflakeEntry.SENIOR_ADMIN_ROLE_ID.getSnowflake()))
{
return Rank.SENIOR_ADMIN.getColoredTag();
}
else if (role.getId().equals(SnowflakeEntry.ADMIN_ROLE_ID.getSnowflake()))
{
return Rank.ADMIN.getColoredTag();
}
else if (role.getId().equals(SnowflakeEntry.MASTERBUILDER_ROLE_ID.getSnowflake()))
{
return Title.MASTER_BUILDER.getColoredTag();
}
else
{
return "";
}
return null;
}).blockFirst();
}
public TotalFreedomMod getCommons() {
public TotalFreedomMod getCommons()
{
return commons;
}
}