This commit is contained in:
Steven Tracey 2022-05-25 22:31:29 -04:00
parent d8a43381d2
commit 716c6c139b
16 changed files with 247 additions and 30 deletions

View File

@ -7,7 +7,7 @@ plugins {
mainClassName = 'net.nevet5gi.buzzbot.Bot'
group 'net.nevet5gi'
version '0.3.0'
version '0.4.1'
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
@ -29,7 +29,7 @@ repositories {
dependencies {
implementation 'net.dv8tion:JDA:4.4.0_350'
implementation 'ch.qos.logback:logback-classic:1.2.11'
implementation 'me.duncte123:botCommons:2.3.8'
//implementation 'me.duncte123:botCommons:2.3.8'
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'me.carleslc.Simple-YAML:Simple-Yaml:1.7.2'
implementation 'mysql:mysql-connector-java:8.0.29'

View File

@ -45,10 +45,13 @@ public class Listener extends ListenerAdapter {
switch (profanityLevel) {
case 1:
if (profanityFilter.containsProfanity(raw, "mild")) profanityFilter.handle(event);
break;
case 2:
if (profanityFilter.containsProfanity(raw, "moderate")) profanityFilter.handle(event);
break;
case 3:
profanityFilter.handle(event);
break;
}
}
@ -68,10 +71,13 @@ public class Listener extends ListenerAdapter {
switch (profanityLevel) {
case 1:
if (profanityFilter.containsProfanity(raw, "mild")) profanityFilter.handleEdit(event);
break;
case 2:
if (profanityFilter.containsProfanity(raw, "moderate")) profanityFilter.handleEdit(event);
break;
case 3:
profanityFilter.handleEdit(event);
break;
}
}
}

View File

@ -5,7 +5,6 @@ import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.nevet5gi.buzzbot.database.SqlDB;
import net.nevet5gi.buzzbot.objects.BanData;
import net.nevet5gi.buzzbot.objects.GuildData;
import javax.security.auth.login.LoginException;
import java.sql.Date;
@ -17,6 +16,7 @@ import java.util.EnumSet;
public class Test {
private static JDA jda;
public static void main(String[] args) {
Config.loadConfig();
initJda();

View File

@ -75,8 +75,8 @@ public class BanCmd implements ICommand {
ban.setServerId(ctx.getGuild().getIdLong());
ban.setServerName(ctx.getGuild().getName());
SqlDB db = new SqlDB();
db.insertBan(ban, "master_ban_list");
//SqlDB db = new SqlDB();
//db.insertBan(ban, "master_ban_record");
//ctx.getEvent().getGuild().ban(cmdf, 1 , "").submit();
//ctx.getMessage().reply("yes ban");
@ -87,6 +87,11 @@ public class BanCmd implements ICommand {
return "ban";
}
@Override
public int getPermissionLevel() {
return 3;
}
@Override
public String getHelp() {
return "Bans the specified user";

View File

@ -2,12 +2,13 @@ package net.nevet5gi.buzzbot.commands;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.nevet5gi.buzzbot.Config;
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
import net.nevet5gi.buzzbot.commands.utils.ICommand;
import net.nevet5gi.buzzbot.commands.utils.ISlashCommand;
import java.io.IOException;
import java.net.URI;
@ -15,26 +16,25 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class PandaCmd implements ICommand {
public class BeeCmd implements ICommand, ISlashCommand {
public static String url;
@Override
public void handle(CommandContext ctx) {
try { getHttpConnection(); } catch (IOException | InterruptedException e) { e.printStackTrace(); }
EmbedBuilder eb = new EmbedBuilder();
ctx.getChannel().sendTyping().queue();
eb.setImage(url.replace("\"",""));
ctx.getChannel().sendMessageEmbeds(eb.build()).queue();
}
@Override
public String getName() {
return "panda";
return "bee";
}
@Override
public String getHelp() {
return "Sends a picture of a panda!\n" +
"Usage: `" + Config.getConfig().getString("bot.prefix") + "panda`";
return "Sends a picture of a bee!\n" +
"Usage: `" + Config.getConfig().getString("bot.prefix") + "bee`";
}
public static void getHttpConnection() throws IOException, InterruptedException {
@ -42,23 +42,36 @@ public class PandaCmd implements ICommand {
HttpRequest request = HttpRequest.newBuilder()
.GET()
.header("accept", "application/json")
.uri(URI.create("https://some-random-api.ml/img/panda"))
.uri(URI.create("https://api.nevets.tech/bee"))
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(PandaCmd::parse)
.thenApply(BeeCmd::parse)
.join();
}
public static String parse(String response) {
String mod = "[ " + response + " ]";
JsonArray ja = new Gson().fromJson(mod, JsonArray.class);
for (int i = 0; i < ja.getAsJsonArray().size(); i++) {
JsonElement jo = ja.get(i);
url = jo.getAsJsonObject().get("link").toString();
}
JsonElement je = new Gson().fromJson(response, JsonElement.class);
url = je.getAsJsonObject().get("link").toString();
return null;
}
@Override
public void handleSlash(CommandContext ctx) {
try { getHttpConnection(); } catch (IOException | InterruptedException e) { e.printStackTrace(); }
EmbedBuilder eb = new EmbedBuilder();
eb.setImage(url.replace("\"",""));
ctx.getSlashEvent().getHook().sendMessageEmbeds(eb.build()).queue();
}
@Override
public String getDescription() {
return "Responds with a picture of a bee!";
}
@Override
public CommandData getCommandData() {
return new CommandData(this.getName(), this.getDescription());
}
}

View File

@ -14,6 +14,11 @@ public class MuteCmd implements ICommand {
return "mute";
}
@Override
public int getPermissionLevel() {
return 2;
}
@Override
public String getHelp() {
return "Mutes the specified user";

View File

@ -1,17 +1,18 @@
package net.nevet5gi.buzzbot.commands;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.nevet5gi.buzzbot.Bot;
import net.nevet5gi.buzzbot.Config;
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
import net.nevet5gi.buzzbot.commands.utils.ICommand;
import net.nevet5gi.buzzbot.commands.utils.ISlashCommand;
public class PingCmd implements ICommand {
public class PingCmd implements ICommand, ISlashCommand {
@Override
public void handle(CommandContext ctx) {
JDA jda = ctx.getJDA();
jda.getRestPing().queue((ping) -> ctx.getChannel().sendMessageFormat("Rest API Ping: %sms\nWebSocket Ping: %sms", ping, jda.getGatewayPing()).queue());
}
@Override
@ -24,4 +25,20 @@ public class PingCmd implements ICommand {
public String getName() {
return "ping";
}
@Override
public void handleSlash(CommandContext ctx) {
JDA jda = Bot.jda;
jda.getRestPing().queue(ping -> ctx.getSlashEvent().getHook().sendMessageFormat("Rest API Ping: %sms\nWebSocket Ping: %sms", ping, jda.getGatewayPing()).queue());
}
@Override
public String getDescription() {
return "Shows the current ping from the bot to API";
}
@Override
public CommandData getCommandData() {
return new CommandData(this.getName(), this.getDescription());
}
}

View File

@ -6,6 +6,9 @@ import net.nevet5gi.buzzbot.commands.utils.ICommand;
public class UnbanCmd implements ICommand {
@Override
public void handle(CommandContext ctx) {
// Somehow keep track of bans
// Make sure a user can only be unbanned from the guild they were banned from
ctx.getMessage().reply("This command doesn't work yet").queue();
}
@ -14,6 +17,11 @@ public class UnbanCmd implements ICommand {
return "unban";
}
@Override
public int getPermissionLevel() {
return 3;
}
@Override
public String getHelp() {
return "Unbans the specified user";

View File

@ -14,6 +14,11 @@ public class UnmuteCmd implements ICommand {
return "unmute";
}
@Override
public int getPermissionLevel() {
return 2;
}
@Override
public String getHelp() {
return "Unmutes the specified user";

View File

@ -15,6 +15,11 @@ public class UnwarnCmd implements ICommand {
return "unwarn";
}
@Override
public int getPermissionLevel() {
return 2;
}
@Override
public String getHelp() {
return "Unwarns the specified user";

View File

@ -21,6 +21,11 @@ public class WarnCmd implements ICommand, ISlashCommand {
return "warn";
}
@Override
public int getPermissionLevel() {
return 2;
}
@Override
public String getDescription() {
return "Warns specified user";

View File

@ -1,6 +1,5 @@
package net.nevet5gi.buzzbot.commands.utils;
import me.duncte123.botcommons.commands.ICommandContext;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;

View File

@ -1,5 +1,7 @@
package net.nevet5gi.buzzbot.commands.utils;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
@ -23,9 +25,9 @@ public class CommandManager {
public CommandManager() {
//Add to this list in alphabetical order
addCommand(new BanCmd());
addCommand(new BeeCmd());
addCommand(new HelpCmd(this));
addCommand(new MuteCmd());
addCommand(new PandaCmd());
addCommand(new PingCmd());
addCommand(new TestCmd());
addCommand(new UnbanCmd());
@ -36,7 +38,7 @@ public class CommandManager {
}
private void addCommand(ICmdGeneric cmd) {
boolean nameFound = commands.stream().anyMatch((it) -> it.getName().equalsIgnoreCase(cmd.getName()));
boolean nameFound = commands.stream().anyMatch(it -> it.getName().equalsIgnoreCase(cmd.getName()));
if (nameFound) {
throw new IllegalArgumentException("A command with this name is already present");
}
@ -77,6 +79,31 @@ public class CommandManager {
return null;
}
public boolean hasPermission(Member member, ICmdGeneric cmd) {
//TODO Make this get roles from db
List<Role> roles = member.getRoles();
int permLevel = 0;
for (Role role : roles) {
switch (role.getName().toLowerCase()) {
case "helper":
if (permLevel < 1) permLevel = 1;
continue;
case "moderator", "mod":
if (permLevel < 2) permLevel = 2;
continue;
case "administrator", "admin":
if (permLevel < 3) permLevel = 3;
continue;
case "owner":
if (permLevel < 4) permLevel = 4;
}
}
return permLevel >= cmd.getPermissionLevel();
}
@Nullable
public ISlashCommand getSlashCommand(String search) {
String searchLower = search.toLowerCase();
@ -98,6 +125,11 @@ public class CommandManager {
String invoke = split[0].toLowerCase();
ICommand cmd = getCommand(invoke);
if (!hasPermission(event.getMember(), cmd)) {
event.getMessage().reply("You do not have permission to use this command.").queue();
return;
}
if (cmd != null) {
event.getChannel().sendTyping().queue();
List<String> args = Arrays.asList(split).subList(1, split.length);
@ -112,6 +144,11 @@ public class CommandManager {
String invoke = event.getName();
ISlashCommand cmd = getSlashCommand(invoke);
if (!hasPermission(event.getMember(), cmd)) {
event.getHook().sendMessage("You do not have permission to use this command.").queue();
return;
}
if (cmd != null) {
event.deferReply().queue();
List<String> args = new ArrayList<>();

View File

@ -6,6 +6,14 @@ public interface ICmdGeneric {
String getName();
/**
* @apiNote 0 = default, 1 = helper, 2 = moderator, 3 = admin, 4 = owner
* @return int
*/
default int getPermissionLevel() {
return 0;
}
String getHelp();
default List<String> getAliases(){

View File

@ -1,2 +1,97 @@
package net.nevet5gi.buzzbot.commands.utils;public class ICommandContext {
package net.nevet5gi.buzzbot.commands.utils;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.sharding.ShardManager;
public interface ICommandContext {
/**
* Returns the {@link net.dv8tion.jda.api.entities.Guild} for the current command/event
*
* @return the {@link net.dv8tion.jda.api.entities.Guild} for this command/event
*/
default Guild getGuild() {
return this.getEvent().getGuild();
}
/**
* Returns the {@link net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent message event} that was received for this instance
*
* @return the {@link net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent message event} that was received for this instance
*/
GuildMessageReceivedEvent getEvent();
/**
* Returns the {@link net.dv8tion.jda.api.entities.TextChannel channel} that the message for this event was send in
*
* @return the {@link net.dv8tion.jda.api.entities.TextChannel channel} that the message for this event was send in
*/
default TextChannel getChannel() {
return this.getEvent().getChannel();
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.Message message} that triggered this event
*
* @return the {@link net.dv8tion.jda.api.entities.Message message} that triggered this event
*/
default Message getMessage() {
return this.getEvent().getMessage();
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.User author} of the message as user
*
* @return the {@link net.dv8tion.jda.api.entities.User author} of the message as user
*/
default User getAuthor() {
return this.getEvent().getAuthor();
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.Member author} of the message as member
*
* @return the {@link net.dv8tion.jda.api.entities.Member author} of the message as member
*/
default Member getMember() {
return this.getEvent().getMember();
}
/**
* Returns the current {@link net.dv8tion.jda.api.JDA jda} instance
*
* @return the current {@link net.dv8tion.jda.api.JDA jda} instance
*/
default JDA getJDA() {
return this.getEvent().getJDA();
}
/**
* Returns the current {@link net.dv8tion.jda.api.sharding.ShardManager} instance
*
* @return the current {@link net.dv8tion.jda.api.sharding.ShardManager} instance
*/
default ShardManager getShardManager() {
return this.getJDA().getShardManager();
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.User user} for the currently logged in account
*
* @return the {@link net.dv8tion.jda.api.entities.User user} for the currently logged in account
*/
default User getSelfUser() {
return this.getJDA().getSelfUser();
}
/**
* Returns the {@link net.dv8tion.jda.api.entities.Member member} in the guild for the currently logged in account
*
* @return the {@link net.dv8tion.jda.api.entities.Member member} in the guild for the currently logged in account
*/
default Member getSelfMember() {
return this.getGuild().getSelfMember();
}
}

View File

@ -25,7 +25,7 @@ public class SqlDB {
public void insertBan(BanData ban, String table) {
try {
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + ban.getUserId() + ", '" + ban.getUserName() + "', '" + ban.getDate() + "', '" + ban.getTime() + "', " + ban.getBanType() + ", " + ban.getBanLength() + ", '" + ban.getBanReason() + "', '" + ban.getModName() + "', " + ban.getModId() + ", '" + ban.getServerName() + "', " + ban.getServerId() + ")");
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + ban.getUserId() + ", '" + ban.getUserName() + "', '" + ban.getDate() + "', '" + ban.getTime() + "', " + ban.getBanType() + ", " + ban.getBanLength() + ", '" + ban.getBanReason() + "', '" + ban.getModName() + "', " + ban.getModId() + ", '" + ban.getServerName() + "', " + ban.getServerId() + ", 1)");
} catch (SQLException e) {
e.printStackTrace();
}
@ -33,12 +33,18 @@ public class SqlDB {
close();
}
public void insertUnban(long userId, String table) {
}
public void insertMute(MuteData mute, String table) {
try {
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + mute.getUserId() + ", '" + mute.getUserName() + "', '" + mute.getDate() + "', '" + mute.getTime() + "', " + mute.getMuteLength() + ", '" + mute.getMuteReason() + "', '" + mute.getModName() + "', " + mute.getModId() + ", '" + mute.getServerName() + "', " + mute.getServerId() + ")");
} catch (SQLException e) {
e.printStackTrace();
}
close();
}
public void insertWarn(WarnData warn, String table) {
@ -47,14 +53,14 @@ public class SqlDB {
} catch (SQLException e) {
e.printStackTrace();
}
close();
}
public BanData queryBan(long userId, String table) {
BanData ban = new BanData();
try {
//TODO Make this use GuildUtils in order to get the proper group for the server
resultSet = statement.executeQuery("SELECT * FROM " + table + " WHERE userId=" + userId);
while (resultSet.next()) {
@ -94,6 +100,7 @@ public class SqlDB {
throw new RuntimeException(e);
}
close();
return mute;
}
@ -111,6 +118,7 @@ public class SqlDB {
e.printStackTrace();
}
close();
return warn;
}
@ -140,6 +148,7 @@ public class SqlDB {
e.printStackTrace();
}
close();
return guild;
}