Skip to content

Commit

Permalink
Merge pull request #529 from beanbeanjuice/integration
Browse files Browse the repository at this point in the history
Ready for Integration to v3.1.2
  • Loading branch information
beanbeanjuice authored Oct 14, 2022
2 parents dd02da6 + e4a1714 commit 59c5cf5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>

Expand Down
19 changes: 7 additions & 12 deletions src/main/java/com/beanbeanjuice/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
Expand Down Expand Up @@ -53,8 +51,6 @@ public class Bot {

// Internal Items
private static LogManager logger;
private static Guild homeGuild;
private static TextChannel homeGuildLogChannel;
private static final String HOME_GUILD_ID = System.getenv("CAFEBOT_GUILD_ID");
private static final String HOME_GUILD_LOG_CHANNEL_ID = System.getenv("CAFEBOT_GUILD_LOG_CHANNEL_ID");
private static final String HOME_GUILD_WEBHOOK_URL = System.getenv("CAFEBOT_GUILD_WEBHOOK_URL");
Expand All @@ -66,8 +62,8 @@ public class Bot {
public static int commandsRun = 0;
public static final String DISCORD_AVATAR_URL = "https://cdn.beanbeanjuice.com/images/cafeBot/cafeBot.gif";

public Bot() throws LoginException, InterruptedException {
logger = new LogManager("cafeBot Logging System", homeGuildLogChannel, "logs/");
public Bot() throws LoginException {
logger = new LogManager("cafeBot Logging System", HOME_GUILD_ID, HOME_GUILD_LOG_CHANNEL_ID, "logs/");
Helper.startCafeAPIRefreshTimer(location);

logger.addWebhookURL(HOME_GUILD_WEBHOOK_URL);
Expand All @@ -86,13 +82,13 @@ public Bot() throws LoginException, InterruptedException {
)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.setChunkingFilter(ChunkingFilter.ALL)
.build()
.awaitReady();
.build();
// .awaitReady(); // TODO: Remove if working.

logger.enableDiscordLogging();

TwitchHandler.start(); // Start twitch handler prior to guild handler.
GuildHandler.start(); // Starting hte guild handler.
homeGuild = bot.getGuildById(HOME_GUILD_ID);
homeGuildLogChannel = homeGuild.getTextChannelById(HOME_GUILD_LOG_CHANNEL_ID);

logger.log(Bot.class, LogLevel.LOADING, "Adding commands...", false, false);
commandHandler = new CommandHandler(bot);
Expand All @@ -107,7 +103,6 @@ public Bot() throws LoginException, InterruptedException {
new CommandAutoCompleteHandler() // Listens for auto complete interactions
);

logger.setLogChannel(homeGuildLogChannel);
logger.log(Bot.class, LogLevel.INFO, "Enabled Discord Logging...", true, true);

// Helpers that need to be instantiated.
Expand All @@ -128,7 +123,7 @@ public Bot() throws LoginException, InterruptedException {
Helper.startUpdateTimer();
}

public static void main(String[] args) throws LoginException, InterruptedException {
public static void main(String[] args) throws LoginException {
new Bot();
}

Expand Down
32 changes: 25 additions & 7 deletions src/main/java/com/beanbeanjuice/command/fun/AvatarCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.beanbeanjuice.utility.command.ICommand;
import com.beanbeanjuice.utility.helper.Helper;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
Expand All @@ -22,19 +23,33 @@ public class AvatarCommand implements ICommand {

@Override
public void handle(@NotNull SlashCommandInteractionEvent event) {
User user = event.getUser();
String type = event.getOption("type").getAsString();

if (event.getOption("user") != null)
user = event.getOption("user").getAsUser();
String name = "";
String url = "";

event.getHook().sendMessageEmbeds(avatarEmbed(user)).queue();
if (type.equals("USER")) {
User user = event.getUser();
if (event.getOption("user") != null)
user = event.getOption("user").getAsUser();
name = user.getName();
url = user.getAvatarUrl();
} else {
Member member = event.getMember();
if (event.getOption("user") != null)
member = event.getOption("user").getAsMember();
name = member.getUser().getName();
url = member.getAvatarUrl();
}

event.getHook().sendMessageEmbeds(avatarEmbed(name, url)).queue();
}

@NotNull
private MessageEmbed avatarEmbed(@NotNull User user) {
private MessageEmbed avatarEmbed(@NotNull String name, @NotNull String avatarURL) {
return new EmbedBuilder()
.setTitle(user.getName() + "'s Avatar")
.setImage(user.getAvatarUrl() + "?size=512")
.setTitle(name + "'s Avatar")
.setImage(avatarURL + "?size=512")
.setColor(Helper.getRandomColor())
.build();
}
Expand All @@ -55,6 +70,9 @@ public String exampleUsage() {
@Override
public ArrayList<OptionData> getOptions() {
ArrayList<OptionData> options = new ArrayList<>();
options.add(new OptionData(OptionType.STRING, "type", "Get their user or server avatar.", true, false)
.addChoice("User Avatar", "USER")
.addChoice("Server Avatar", "SERVER"));
options.add(new OptionData(OptionType.USER, "user", "The user to get the avatar of.", false, false));
return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,6 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
return;
}
}

if (event.getMessage().getContentRaw().startsWith(guildInformation.getPrefix())) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setAuthor("ATTENTION!", "https://youtu.be/4XxcpBxSCiU")
.setDescription("Everything has been transitioned to slash commands! Everything has remained " +
"the same except for slash commands. Use the commands as you normally would, but put a slash in " +
"front of it instead! If you haven't already, click the link below to add slash commands for **CafeBot**.")
.setColor(Helper.getRandomColor())
.setFooter("Sorry for the inconvenience!");
event.getChannel().sendMessageEmbeds(embedBuilder.build()).setActionRow(
Button.link("https://discord.com/api/oauth2/authorize?client_id=787162619504492554&permissions=8&scope=bot%20applications.commands",
"Add Slash Commands").withEmoji(Emoji.fromFormatted("<a:wowowow:886217210010431508>"))
).queue();
}
}

}
39 changes: 23 additions & 16 deletions src/main/java/com/beanbeanjuice/utility/logging/LogManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.beanbeanjuice.utility.logging;

import com.beanbeanjuice.Bot;
import com.beanbeanjuice.utility.helper.Helper;
import com.beanbeanjuice.utility.exception.WebhookException;
import com.beanbeanjuice.utility.webhook.Webhook;
import com.beanbeanjuice.cafeapi.utility.Time;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -31,22 +31,27 @@ public class LogManager {
private final Time time;

private final String name;
private TextChannel logChannel;
private final String guildID;
private final String logChannelID;
private final ArrayList<String> webhookURLs;
private String currentLogFileName;
private final String filePath;
private String logFileTime;
private boolean discordLogging = false;

/**
* Create a {@link LogManager LogManager} instance.
* @param name The name for the {@link LogManager LogManager}.
* @param logChannel The {@link TextChannel TextChannel} to be used for logging.
* @param guildID The {@link String guildID} to log to.
* @param logChannelID The {@link String logChannelID} to log to.
*/
public LogManager(@NotNull String name, @Nullable TextChannel logChannel, @NotNull String filePath) {
public LogManager(@NotNull String name, @NotNull String guildID, @NotNull String logChannelID,
@NotNull String filePath) {
time = new Time();

this.name = name;
this.logChannel = logChannel;
this.guildID = guildID;
this.logChannelID = logChannelID;
this.filePath = filePath;

webhookURLs = new ArrayList<>(); // Creates the ArrayList
Expand Down Expand Up @@ -269,14 +274,6 @@ private void compress(@NotNull String logDirectory, @NotNull String fileName, @N
fileInputStream.close();
}

/**
* Sets the log channel for the {@link LogManager}.
* @param logChannel The {@link TextChannel logChannel} for the {@link Guild}.
*/
public void setLogChannel(@NotNull TextChannel logChannel) {
this.logChannel = logChannel;
}

/**
* Log to discord, webhook, and file.
* @param c The class that called the log.
Expand Down Expand Up @@ -342,7 +339,7 @@ public void log(@NotNull Class<?> c, @NotNull LogLevel logLevel, @NotNull String
logToWebhook(c, logLevel, message, time);

if (logToLogChannel)
logToLogChannel(c, logLevel, message, time);
logToLogChannel(c, logLevel, message);

}

Expand Down Expand Up @@ -377,7 +374,10 @@ private void logToWebhook(@NotNull Class<?> c, @NotNull LogLevel logLevel, @NotN
* @param logLevel The current {@link LogLevel} of the log to be created.
* @param message The message contents for the log.
*/
private void logToLogChannel(@NotNull Class<?> c, @NotNull LogLevel logLevel, @NotNull String message, @NotNull Time time) {
private void logToLogChannel(@NotNull Class<?> c, @NotNull LogLevel logLevel, @NotNull String message) {
if (!discordLogging)
return;

EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setAuthor(logLevel.toString());
embedBuilder.setThumbnail(logLevel.getImageURL());
Expand All @@ -387,7 +387,7 @@ private void logToLogChannel(@NotNull Class<?> c, @NotNull LogLevel logLevel, @N
embedBuilder.setTimestamp(new Date().toInstant());

try {
logChannel.sendMessageEmbeds(embedBuilder.build()).complete();
Bot.getBot().getGuildById(guildID).getTextChannelById(logChannelID).sendMessageEmbeds(embedBuilder.build()).complete();
} catch (NullPointerException ignored) {}
}

Expand All @@ -399,4 +399,11 @@ public void addWebhookURL(@NotNull String url) {
webhookURLs.add(url);
}

/**
* Enables logging to the Discord {@link TextChannel log channel}.
*/
public void enableDiscordLogging() {
discordLogging = true;
}

}

0 comments on commit 59c5cf5

Please sign in to comment.