Skip to content

Commit

Permalink
Merge pull request #278 from beanbeanjuice/integration
Browse files Browse the repository at this point in the history
Ready for Release v2.6.1
  • Loading branch information
beanbeanjuice committed Jun 5, 2021
2 parents 04aae94 + 2c7f2ce commit 9b03468
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 43 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -87,6 +87,7 @@
`cafeBot`, formerly `beanBot` is a general purpose bot that has many features. Many features work across Discord servers. Some features include;
* Global Birthday Checker
* Global Currency
* Global Counting Leaderboard
* Interaction Commands
* Moderation Commands
* Poll/Raffle Commands
Expand All @@ -108,7 +109,7 @@ This is an example of how to list things you need to use the software and how to

## Installation

1. Click this [link](https://discord.com/api/oauth2/authorize?client_id=787162619504492554&permissions=8&scope=bot).
1. Click this [link](https://discord.com/api/oauth2/authorize?client_id=787162619504492554&permissions=305654886&scope=bot).
2. Give the bot administrative access. `I promise it's safe. You can view all the code for yourself.`
3. Enjoy!

Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.beanbeanjuice.utility.command.usage.categories.CategoryType;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import org.jetbrains.annotations.NotNull;
Expand All @@ -26,23 +27,34 @@ public void handle(CommandContext ctx, ArrayList<String> args, User user, GuildM
Integer currentNumber = CafeBot.getCountingHelper().getLastNumber(event.getGuild());

if (highestNumber == null || currentNumber == null) {
event.getChannel().sendMessage(CafeBot.getGeneralHelper().errorEmbed(
"Error Getting Counting Statistics",
"There was an error getting counting statistics. There are either no counting statistics " +
"for the server on this bot, or there is an SQL server error."
)).queue();
sendSQLError(event.getChannel());
return;
}

event.getChannel().sendMessage(countingStatisticsEmbed(highestNumber, currentNumber)).queue();
Integer leaderboardPlace = CafeBot.getCountingHelper().getCountingLeaderboardPlace(highestNumber);
if (leaderboardPlace == null) {
sendSQLError(event.getChannel());
return;
}

event.getChannel().sendMessage(countingStatisticsEmbed(highestNumber, currentNumber, leaderboardPlace)).queue();
}

private void sendSQLError(@NotNull TextChannel textChannel) {
textChannel.sendMessage(CafeBot.getGeneralHelper().errorEmbed(
"Error Getting Counting Statistics",
"There was an error getting counting statistics. There are either no counting statistics " +
"for the server on this bot, or there is an SQL server error."
)).queue();
}

@NotNull
private MessageEmbed countingStatisticsEmbed(@NotNull Integer highestNumber, @NotNull Integer currentNumber) {
private MessageEmbed countingStatisticsEmbed(@NotNull Integer highestNumber, @NotNull Integer currentNumber, @NotNull Integer leaderboardPlace) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Current Number");
embedBuilder.addField("Highest Number", highestNumber.toString(), true);
embedBuilder.addField("Current Number", currentNumber.toString(), true);
embedBuilder.setDescription("Your current place in the global server leaderboard is `#" + leaderboardPlace + "/" + CafeBot.getJDA().getGuilds().size() + "`.");
embedBuilder.setColor(CafeBot.getGeneralHelper().getRandomColor());
embedBuilder.setFooter("These statistics are for the current server only.");
return embedBuilder.build();
Expand All @@ -63,6 +75,8 @@ public ArrayList<String> getAliases() {
arrayList.add("countingstatistics");
arrayList.add("counting-stats");
arrayList.add("countingstats");
arrayList.add("countingleaderboard");
arrayList.add("counting-leaderboard");
return arrayList;
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/beanbeanjuice/command/generic/PingCommand.java
Expand Up @@ -44,11 +44,17 @@ private MessageEmbed messageEmbed(@NotNull Long botPing, @NotNull Long gatewayPi
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle(getName() + "!", "https://www.beanbeanjuice.com/cafeBot.html");
StringBuilder descriptionBuilder = new StringBuilder();
double cpuLoad = (double) Math.round((ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class).getCpuLoad()*100) * 100) / 100;
long systemMemoryTotal = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class).getTotalMemorySize()/1048576;
long systemMemoryUsage = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class).getCommittedVirtualMemorySize()/1048576;
long dedicatedMemoryTotal = Runtime.getRuntime().maxMemory()/1048576;
long dedicatedMemoryUsage = Runtime.getRuntime().totalMemory()/1048576;
descriptionBuilder.append("**Rest Ping** - `").append(botPing).append("`\n")
.append("**Gateway Ping** - `").append(gatewayPing).append("`\n")
.append("**Current Version** - `").append(CafeBot.getBotVersion()).append("`\n")
.append("**CPU Usage** - `").append((double) Math.round((ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class).getCpuLoad()*100) * 100) / 100).append("%`\n")
.append("**Memory Usage** - `").append(ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class).getCommittedVirtualMemorySize()/1048576).append("` mb / `").append(Runtime.getRuntime().maxMemory()/1048576).append("` mb\n")
.append("**CPU Usage** - `").append(cpuLoad).append("%`\n")
.append("**OS Memory Usage** - `").append(systemMemoryUsage).append("` mb / `").append(systemMemoryTotal).append("` mb\n")
.append("**Bot Memory Usage** - `").append(dedicatedMemoryUsage).append("` mb / `").append(dedicatedMemoryTotal).append("` mb\n")
.append("**Bot Uptime** - `").append(CafeBot.getGeneralHelper().formatTime(ManagementFactory.getRuntimeMXBean().getUptime())).append("`\n\n")
.append("Hello there! How are you? Would you like to order some coffee?");
embedBuilder.setDescription(descriptionBuilder.toString());
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/com/beanbeanjuice/command/moderation/BanCommand.java
Expand Up @@ -13,6 +13,7 @@
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.exceptions.HierarchyException;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
Expand Down Expand Up @@ -52,7 +53,16 @@ public void handle(CommandContext ctx, ArrayList<String> args, User user, GuildM
try {
ctx.getGuild().getMember(punishee).ban(0, reason.toString()).queue();
} catch (HierarchyException e) {
event.getChannel().sendMessage(hierarchyErrorEmbed()).queue();
event.getChannel().sendMessage(CafeBot.getGeneralHelper().errorEmbed(
"Unable to Ban User",
"The user you are trying to ban has a higher role than the bot!"
)).queue();
return;
} catch (InsufficientPermissionException e) {
event.getChannel().sendMessage(CafeBot.getGeneralHelper().errorEmbed(
"Unable to Ban User",
"I do not have the proper permissions to ban users."
)).queue();
return;
}

Expand All @@ -73,15 +83,6 @@ private MessageEmbed successfulBanWithReasonEmbed(@NotNull User punishee, @NotNu
return embedBuilder.build();
}

@NotNull
private MessageEmbed hierarchyErrorEmbed() {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Color.red);
embedBuilder.setTitle("Unable to Ban User");
embedBuilder.setDescription("The user you are trying to ban has a higher role than the bot.");
return embedBuilder.build();
}

@Override
public String getName() {
return "ban";
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/beanbeanjuice/command/moderation/KickCommand.java
Expand Up @@ -13,6 +13,7 @@
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.exceptions.HierarchyException;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
Expand Down Expand Up @@ -52,14 +53,23 @@ public void handle(CommandContext ctx, ArrayList<String> args, User user, GuildM
try {
ctx.getGuild().getMember(punishee).kick(reason.toString()).queue();
} catch (HierarchyException e) {
event.getChannel().sendMessage(hierarchyErrorEmbed()).queue();
event.getChannel().sendMessage(CafeBot.getGeneralHelper().errorEmbed(
"Unable to Kick User",
"The user you are trying to kick has a higher role than the bot."
)).queue();
return;
} catch (InsufficientPermissionException e) {
event.getChannel().sendMessage(CafeBot.getGeneralHelper().errorEmbed(
"Unable to Kick User",
"I do not have the proper permissions to kick users."
)).queue();
return;
}

CafeBot.getGeneralHelper().pmUser(punishee, "You have been kicked: " + reason.toString());
CafeBot.getGeneralHelper().pmUser(punishee, "You have been kicked: " + reason);
event.getChannel().sendMessage(successfulKickEmbed(punishee, user, reason.toString())).queue();
CafeBot.getGuildHandler().getCustomGuild(event.getGuild()).log(this, LogLevel.INFO, "User Kicked", "`" + punishee.getAsTag() + "` was kicked by " +
user.getAsMention() + " for: `" + reason.toString() + "`");
user.getAsMention() + " for: `" + reason + "`");
}

@NotNull
Expand All @@ -73,15 +83,6 @@ private MessageEmbed successfulKickEmbed(@NotNull User punishee, @NotNull User p
return embedBuilder.build();
}

@NotNull
private MessageEmbed hierarchyErrorEmbed() {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Color.red);
embedBuilder.setTitle("Unable to Kick User");
embedBuilder.setDescription("The user you are trying to kick has a higher role than the bot.");
return embedBuilder.build();
}

@Override
public String getName() {
return "kick";
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/com/beanbeanjuice/utility/helper/CountingHelper.java
@@ -1,6 +1,7 @@
package com.beanbeanjuice.utility.helper;

import com.beanbeanjuice.CafeBot;
import com.beanbeanjuice.utility.logger.LogLevel;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
Expand All @@ -10,10 +11,7 @@
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;

/**
* A class used for helping with counting.
Expand Down Expand Up @@ -80,6 +78,31 @@ public void checkNumber(@NotNull GuildMessageReceivedEvent event, @NotNull Integ
}
}

/**
* Get the leaderboard place for a specified {@link Integer} limit.
* @param limit The limit specified.
* @return The current place of that {@link Integer}.
*/
@Nullable
public Integer getCountingLeaderboardPlace(@NotNull Integer limit) {
Connection connection = CafeBot.getSQLServer().getConnection();
String arguments = "SELECT * FROM cafeBot.counting_information WHERE counting_information.highest_number>=(?) ORDER BY counting_information.highest_number DESC;";

try {
PreparedStatement statement = connection.prepareStatement(arguments);
statement.setInt(1, limit);
ResultSet resultSet = statement.executeQuery();
int count = 0;
while (resultSet.next()) {
count++;
}
return count;
} catch (SQLException e) {
CafeBot.getLogManager().log(CountingHelper.class, LogLevel.WARN, "Error Getting Leaderboard: " + e.getMessage());
return null;
}
}

/**
* Sets the last user ID for the counting {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}.
* @param guild The {@link Guild} specified.
Expand All @@ -88,7 +111,6 @@ public void checkNumber(@NotNull GuildMessageReceivedEvent event, @NotNull Integ
*/
@NotNull
private Boolean setLastUserID(@NotNull Guild guild, @NotNull String lastUserID) {

Connection connection = CafeBot.getSQLServer().getConnection();
String arguments = "UPDATE cafeBot.counting_information SET last_user_id = (?) WHERE guild_id = (?);";

Expand Down
Expand Up @@ -11,6 +11,7 @@
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import org.jetbrains.annotations.NotNull;

import java.net.URI;
Expand Down Expand Up @@ -133,7 +134,7 @@ public void contactGuilds() {

try {
mainChannel.sendMessage(owner.getAsMention() + " I've been updated!").embed(updateEmbed).queue();
} catch (NullPointerException ignored) {}
} catch (NullPointerException | InsufficientPermissionException | UnsupportedOperationException ignored) {}
}
});
}
Expand Down
Expand Up @@ -10,6 +10,7 @@
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

Expand All @@ -32,19 +33,22 @@ public void onReady(@Nonnull ReadyEvent event) {
public void onGuildLeave(@NotNull GuildLeaveEvent event) {
CafeBot.getGuildHandler().removeGuild(event.getGuild());
CafeBot.updateGuildPresence(); // Updates the amount of servers in the status.
CafeBot.getLogManager().log(Listener.class, LogLevel.INFO, "`" + event.getGuild().getName() + "` has removed me... :pleading_face:", false, true);
}

@Override
public void onGuildJoin(@NotNull GuildJoinEvent event) {
TextChannel channel = event.getGuild().getDefaultChannel();

if (channel != null) {
event.getGuild().getDefaultChannel().sendMessage(guildJoinEmbed()).queue();
try {
event.getGuild().getDefaultChannel().sendMessage(guildJoinEmbed()).queue();
} catch (InsufficientPermissionException ignored) {}
}

CafeBot.getGuildHandler().addGuild(event.getGuild());
CafeBot.updateGuildPresence(); // Updates the amount of servers in the status.
CafeBot.getLogManager().log(this.getClass(), LogLevel.INFO, event.getGuild().getName() + " has added me!", false, true);
CafeBot.getLogManager().log(this.getClass(), LogLevel.INFO, "`" + event.getGuild().getName() + "` has added me! :blush:", false, true);
}

@Override
Expand Down
Expand Up @@ -76,10 +76,22 @@ public void playlistLoaded(AudioPlaylist audioPlaylist) {

@Override
public void noMatches() {
try {
CafeBot.getGuildHandler().getCustomGuild(guild).getLastMusicChannel().sendMessage(CafeBot.getGeneralHelper().errorEmbed(
"Error Playing Song",
"There was an error playing: `" + trackURL + "`."
)).queue();
} catch (NullPointerException ignored) {}
}

@Override
public void loadFailed(FriendlyException e) {
try {
CafeBot.getGuildHandler().getCustomGuild(guild).getLastMusicChannel().sendMessage(CafeBot.getGeneralHelper().errorEmbed(
"Error Playing Song",
"There has been a catastrophic error playing: `" + trackURL + "`.\n\n**ERROR**: " + e.getMessage()
)).queue();
} catch (NullPointerException ignored) {}
}
});
}
Expand Down
Expand Up @@ -6,10 +6,7 @@
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
import net.dv8tion.jda.api.entities.Guild;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

Expand Down

0 comments on commit 9b03468

Please sign in to comment.