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

View File

@ -45,10 +45,13 @@ public class Listener extends ListenerAdapter {
switch (profanityLevel) { switch (profanityLevel) {
case 1: case 1:
if (profanityFilter.containsProfanity(raw, "mild")) profanityFilter.handle(event); if (profanityFilter.containsProfanity(raw, "mild")) profanityFilter.handle(event);
break;
case 2: case 2:
if (profanityFilter.containsProfanity(raw, "moderate")) profanityFilter.handle(event); if (profanityFilter.containsProfanity(raw, "moderate")) profanityFilter.handle(event);
break;
case 3: case 3:
profanityFilter.handle(event); profanityFilter.handle(event);
break;
} }
} }
@ -68,10 +71,13 @@ public class Listener extends ListenerAdapter {
switch (profanityLevel) { switch (profanityLevel) {
case 1: case 1:
if (profanityFilter.containsProfanity(raw, "mild")) profanityFilter.handleEdit(event); if (profanityFilter.containsProfanity(raw, "mild")) profanityFilter.handleEdit(event);
break;
case 2: case 2:
if (profanityFilter.containsProfanity(raw, "moderate")) profanityFilter.handleEdit(event); if (profanityFilter.containsProfanity(raw, "moderate")) profanityFilter.handleEdit(event);
break;
case 3: case 3:
profanityFilter.handleEdit(event); 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.dv8tion.jda.api.utils.cache.CacheFlag;
import net.nevet5gi.buzzbot.database.SqlDB; import net.nevet5gi.buzzbot.database.SqlDB;
import net.nevet5gi.buzzbot.objects.BanData; import net.nevet5gi.buzzbot.objects.BanData;
import net.nevet5gi.buzzbot.objects.GuildData;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.sql.Date; import java.sql.Date;
@ -17,6 +16,7 @@ import java.util.EnumSet;
public class Test { public class Test {
private static JDA jda; private static JDA jda;
public static void main(String[] args) { public static void main(String[] args) {
Config.loadConfig(); Config.loadConfig();
initJda(); initJda();

View File

@ -75,8 +75,8 @@ public class BanCmd implements ICommand {
ban.setServerId(ctx.getGuild().getIdLong()); ban.setServerId(ctx.getGuild().getIdLong());
ban.setServerName(ctx.getGuild().getName()); ban.setServerName(ctx.getGuild().getName());
SqlDB db = new SqlDB(); //SqlDB db = new SqlDB();
db.insertBan(ban, "master_ban_list"); //db.insertBan(ban, "master_ban_record");
//ctx.getEvent().getGuild().ban(cmdf, 1 , "").submit(); //ctx.getEvent().getGuild().ban(cmdf, 1 , "").submit();
//ctx.getMessage().reply("yes ban"); //ctx.getMessage().reply("yes ban");
@ -87,6 +87,11 @@ public class BanCmd implements ICommand {
return "ban"; return "ban";
} }
@Override
public int getPermissionLevel() {
return 3;
}
@Override @Override
public String getHelp() { public String getHelp() {
return "Bans the specified user"; 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.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.nevet5gi.buzzbot.Config; import net.nevet5gi.buzzbot.Config;
import net.nevet5gi.buzzbot.commands.utils.CommandContext; import net.nevet5gi.buzzbot.commands.utils.CommandContext;
import net.nevet5gi.buzzbot.commands.utils.ICommand; import net.nevet5gi.buzzbot.commands.utils.ICommand;
import net.nevet5gi.buzzbot.commands.utils.ISlashCommand;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@ -15,26 +16,25 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
public class PandaCmd implements ICommand { public class BeeCmd implements ICommand, ISlashCommand {
public static String url; public static String url;
@Override @Override
public void handle(CommandContext ctx) { public void handle(CommandContext ctx) {
try { getHttpConnection(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } try { getHttpConnection(); } catch (IOException | InterruptedException e) { e.printStackTrace(); }
EmbedBuilder eb = new EmbedBuilder(); EmbedBuilder eb = new EmbedBuilder();
ctx.getChannel().sendTyping().queue();
eb.setImage(url.replace("\"","")); eb.setImage(url.replace("\"",""));
ctx.getChannel().sendMessageEmbeds(eb.build()).queue(); ctx.getChannel().sendMessageEmbeds(eb.build()).queue();
} }
@Override @Override
public String getName() { public String getName() {
return "panda"; return "bee";
} }
@Override @Override
public String getHelp() { public String getHelp() {
return "Sends a picture of a panda!\n" + return "Sends a picture of a bee!\n" +
"Usage: `" + Config.getConfig().getString("bot.prefix") + "panda`"; "Usage: `" + Config.getConfig().getString("bot.prefix") + "bee`";
} }
public static void getHttpConnection() throws IOException, InterruptedException { public static void getHttpConnection() throws IOException, InterruptedException {
@ -42,23 +42,36 @@ public class PandaCmd implements ICommand {
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.GET() .GET()
.header("accept", "application/json") .header("accept", "application/json")
.uri(URI.create("https://some-random-api.ml/img/panda")) .uri(URI.create("https://api.nevets.tech/bee"))
.build(); .build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body) .thenApply(HttpResponse::body)
.thenApply(PandaCmd::parse) .thenApply(BeeCmd::parse)
.join(); .join();
} }
public static String parse(String response) { public static String parse(String response) {
String mod = "[ " + response + " ]"; JsonElement je = new Gson().fromJson(response, JsonElement.class);
JsonArray ja = new Gson().fromJson(mod, JsonArray.class); url = je.getAsJsonObject().get("link").toString();
for (int i = 0; i < ja.getAsJsonArray().size(); i++) {
JsonElement jo = ja.get(i);
url = jo.getAsJsonObject().get("link").toString();
}
return null; 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"; return "mute";
} }
@Override
public int getPermissionLevel() {
return 2;
}
@Override @Override
public String getHelp() { public String getHelp() {
return "Mutes the specified user"; return "Mutes the specified user";

View File

@ -1,17 +1,18 @@
package net.nevet5gi.buzzbot.commands; package net.nevet5gi.buzzbot.commands;
import net.dv8tion.jda.api.JDA; 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.Config;
import net.nevet5gi.buzzbot.commands.utils.CommandContext; import net.nevet5gi.buzzbot.commands.utils.CommandContext;
import net.nevet5gi.buzzbot.commands.utils.ICommand; 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 @Override
public void handle(CommandContext ctx) { public void handle(CommandContext ctx) {
JDA jda = ctx.getJDA(); JDA jda = ctx.getJDA();
jda.getRestPing().queue((ping) -> ctx.getChannel().sendMessageFormat("Rest API Ping: %sms\nWebSocket Ping: %sms", ping, jda.getGatewayPing()).queue()); jda.getRestPing().queue((ping) -> ctx.getChannel().sendMessageFormat("Rest API Ping: %sms\nWebSocket Ping: %sms", ping, jda.getGatewayPing()).queue());
} }
@Override @Override
@ -24,4 +25,20 @@ public class PingCmd implements ICommand {
public String getName() { public String getName() {
return "ping"; 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 { public class UnbanCmd implements ICommand {
@Override @Override
public void handle(CommandContext ctx) { 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(); ctx.getMessage().reply("This command doesn't work yet").queue();
} }
@ -14,6 +17,11 @@ public class UnbanCmd implements ICommand {
return "unban"; return "unban";
} }
@Override
public int getPermissionLevel() {
return 3;
}
@Override @Override
public String getHelp() { public String getHelp() {
return "Unbans the specified user"; return "Unbans the specified user";

View File

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

View File

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

View File

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

View File

@ -1,6 +1,5 @@
package net.nevet5gi.buzzbot.commands.utils; 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.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;

View File

@ -1,5 +1,7 @@
package net.nevet5gi.buzzbot.commands.utils; 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.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.CommandData;
@ -23,9 +25,9 @@ public class CommandManager {
public CommandManager() { public CommandManager() {
//Add to this list in alphabetical order //Add to this list in alphabetical order
addCommand(new BanCmd()); addCommand(new BanCmd());
addCommand(new BeeCmd());
addCommand(new HelpCmd(this)); addCommand(new HelpCmd(this));
addCommand(new MuteCmd()); addCommand(new MuteCmd());
addCommand(new PandaCmd());
addCommand(new PingCmd()); addCommand(new PingCmd());
addCommand(new TestCmd()); addCommand(new TestCmd());
addCommand(new UnbanCmd()); addCommand(new UnbanCmd());
@ -36,7 +38,7 @@ public class CommandManager {
} }
private void addCommand(ICmdGeneric cmd) { 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) { if (nameFound) {
throw new IllegalArgumentException("A command with this name is already present"); throw new IllegalArgumentException("A command with this name is already present");
} }
@ -77,6 +79,31 @@ public class CommandManager {
return null; 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 @Nullable
public ISlashCommand getSlashCommand(String search) { public ISlashCommand getSlashCommand(String search) {
String searchLower = search.toLowerCase(); String searchLower = search.toLowerCase();
@ -98,6 +125,11 @@ public class CommandManager {
String invoke = split[0].toLowerCase(); String invoke = split[0].toLowerCase();
ICommand cmd = getCommand(invoke); 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) { if (cmd != null) {
event.getChannel().sendTyping().queue(); event.getChannel().sendTyping().queue();
List<String> args = Arrays.asList(split).subList(1, split.length); List<String> args = Arrays.asList(split).subList(1, split.length);
@ -112,6 +144,11 @@ public class CommandManager {
String invoke = event.getName(); String invoke = event.getName();
ISlashCommand cmd = getSlashCommand(invoke); 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) { if (cmd != null) {
event.deferReply().queue(); event.deferReply().queue();
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();

View File

@ -6,6 +6,14 @@ public interface ICmdGeneric {
String getName(); String getName();
/**
* @apiNote 0 = default, 1 = helper, 2 = moderator, 3 = admin, 4 = owner
* @return int
*/
default int getPermissionLevel() {
return 0;
}
String getHelp(); String getHelp();
default List<String> getAliases(){ 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) { public void insertBan(BanData ban, String table) {
try { 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) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -33,12 +33,18 @@ public class SqlDB {
close(); close();
} }
public void insertUnban(long userId, String table) {
}
public void insertMute(MuteData mute, String table) { public void insertMute(MuteData mute, String table) {
try { 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() + ")"); 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) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
close();
} }
public void insertWarn(WarnData warn, String table) { public void insertWarn(WarnData warn, String table) {
@ -47,14 +53,14 @@ public class SqlDB {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
close();
} }
public BanData queryBan(long userId, String table) { public BanData queryBan(long userId, String table) {
BanData ban = new BanData(); BanData ban = new BanData();
try { 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); resultSet = statement.executeQuery("SELECT * FROM " + table + " WHERE userId=" + userId);
while (resultSet.next()) { while (resultSet.next()) {
@ -94,6 +100,7 @@ public class SqlDB {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
close();
return mute; return mute;
} }
@ -111,6 +118,7 @@ public class SqlDB {
e.printStackTrace(); e.printStackTrace();
} }
close();
return warn; return warn;
} }
@ -140,6 +148,7 @@ public class SqlDB {
e.printStackTrace(); e.printStackTrace();
} }
close();
return guild; return guild;
} }