Allowed multiple users to be added/removed to/from multiple roles, fixed outstanding bugs/errors, moved pandorabots api to module, made pandorabots api per server rather than global, added shutdown command which saves all data and safely ends JDA process

This commit is contained in:
Savvy 2017-09-28 20:45:38 -04:00
parent 634d96b426
commit 46d1c70da6
15 changed files with 149 additions and 68 deletions

View File

@ -38,4 +38,4 @@ compileJava.options.encoding = 'UTF-8'
compileJava.options.fork = true compileJava.options.fork = true
// Change this if you are getting errors building // Change this if you are getting errors building
compileJava.options.forkOptions.executable = 'C:\\Program Files\\Java\\jdk1.8.0_131\\bin\\javac.exe' compileJava.options.forkOptions.executable = 'C:\\Program Files\\Java\\jdk1.8.0_144\\bin\\javac.exe'

View File

@ -19,6 +19,7 @@ import me.savvy.rixa.commands.mod.DeleteMessagesCommand;
import me.savvy.rixa.commands.mod.MuteCommand; import me.savvy.rixa.commands.mod.MuteCommand;
import me.savvy.rixa.commands.mod.PurgeMessagesCommand; import me.savvy.rixa.commands.mod.PurgeMessagesCommand;
import me.savvy.rixa.commands.mod.RaidModeCommand; import me.savvy.rixa.commands.mod.RaidModeCommand;
import me.savvy.rixa.commands.owner.OwnerCommand;
import me.savvy.rixa.data.database.sql.other.DatabaseTables; import me.savvy.rixa.data.database.sql.other.DatabaseTables;
import me.savvy.rixa.data.filemanager.ConfigManager; import me.savvy.rixa.data.filemanager.ConfigManager;
import me.savvy.rixa.data.filemanager.LanguageManager; import me.savvy.rixa.data.filemanager.LanguageManager;
@ -27,6 +28,7 @@ import me.savvy.rixa.events.MemberEvent;
import me.savvy.rixa.events.MessageEvent; import me.savvy.rixa.events.MessageEvent;
import me.savvy.rixa.events.Shutdown; import me.savvy.rixa.events.Shutdown;
import me.savvy.rixa.events.VoiceChannel; import me.savvy.rixa.events.VoiceChannel;
import me.savvy.rixa.guild.RixaGuild;
import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.guild.management.Guilds;
import me.savvy.rixa.modules.reactions.handlers.React; import me.savvy.rixa.modules.reactions.handlers.React;
import me.savvy.rixa.modules.reactions.handlers.ReactionManager; import me.savvy.rixa.modules.reactions.handlers.ReactionManager;
@ -75,10 +77,6 @@ public class Rixa {
@Setter @Setter
private ScheduledExecutorService executorService; private ScheduledExecutorService executorService;
private static ChatterBotFactory factory;
private static ChatterBotSession chatBotSession;
private static ChatterBot chatBot;
// String search = event.getMessage().getContent().substring(event.getMessage().getContent().indexOf(" ") + 1); // String search = event.getMessage().getContent().substring(event.getMessage().getContent().indexOf(" ") + 1);
public static void main(String[] args) { public static void main(String[] args) {
instance = new Rixa(); instance = new Rixa();
@ -142,15 +140,8 @@ public class Rixa {
new BatchMoveCommand(), new MuteCommand(), new MusicCommand(), new BatchMoveCommand(), new MuteCommand(), new MusicCommand(),
new ConfigCommand(), new UrbanDictionaryCommand(), new YoutubeCommand(), new ConfigCommand(), new UrbanDictionaryCommand(), new YoutubeCommand(),
new AddRoleCommand(), new RemoveRoleCommand(), new LevelsCommand(), new AddRoleCommand(), new RemoveRoleCommand(), new LevelsCommand(),
new LeaderboardCommand(), new RaidModeCommand()); new LeaderboardCommand(), new RaidModeCommand(), new OwnerCommand());
register(new HelpReaction(), new ConfigReaction(), new LeaderboardReaction()); register(new HelpReaction(), new ConfigReaction(), new LeaderboardReaction());
try {
factory = new ChatterBotFactory();
chatBot = factory.create(ChatterBotType.PANDORABOTS, "b0dafd24ee35a477");
chatBotSession = chatBot.createSession();
} catch (Exception e) {
e.printStackTrace();
}
} }
private static void register(CommandExec... commandExecs) { private static void register(CommandExec... commandExecs) {
@ -165,10 +156,6 @@ public class Rixa {
} }
} }
public static ChatterBotSession getChatBotSession() {
return chatBotSession;
}
public Logger getLogger() { public Logger getLogger() {
return Logger.getLogger("Rixa"); return Logger.getLogger("Rixa");
} }
@ -179,7 +166,7 @@ public class Rixa {
public void close() { public void close() {
try { try {
Guilds.getGuilds().values().parallelStream().forEach((rixaGuild) -> rixaGuild.save()); Guilds.getGuilds().values().parallelStream().forEach(RixaGuild::save);
Thread.sleep(1200); Thread.sleep(1200);
database.close(); database.close();
Thread.sleep(200); Thread.sleep(200);

View File

@ -40,9 +40,9 @@ public class AddRoleCommand implements CommandExec {
} }
try { try {
List<Role> roles = event.getMessage().getMentionedRoles(); List<Role> roles = event.getMessage().getMentionedRoles();
User user = event.getMessage().getMentionedUsers().get(0); int users = event.getMessage().getMentionedUsers().size();
event.getGuild().getController().addRolesToMember(event.getGuild().getMember(user), roles).queue(); event.getMessage().getMentionedUsers().forEach(user -> event.getGuild().getController().addRolesToMember(event.getGuild().getMember(user), roles).queue());
new MessageBuilder("Successfully given " + user.getAsMention() + " `" + roles.size() + "` role(s)").setColor(event.getMember().getColor()).queue(event.getChannel()); new MessageBuilder("Successfully given " + users + " `" + roles.size() + "` role(s)").setColor(event.getMember().getColor()).queue(event.getChannel());
} catch (PermissionException ex) { } catch (PermissionException ex) {
new MessageBuilder(event.getMember().getAsMention() + ", sorry I do not have permission for this!").setColor(event.getMember().getColor()).queue(event.getChannel()); new MessageBuilder(event.getMember().getAsMention() + ", sorry I do not have permission for this!").setColor(event.getMember().getColor()).queue(event.getChannel());
} }

View File

@ -40,9 +40,9 @@ public class RemoveRoleCommand implements CommandExec {
} }
try { try {
List<Role> roles = event.getMessage().getMentionedRoles(); List<Role> roles = event.getMessage().getMentionedRoles();
User user = event.getMessage().getMentionedUsers().get(0); int users = event.getMessage().getMentionedUsers().size();
event.getGuild().getController().removeRolesFromMember(event.getGuild().getMember(user), roles).queue(); event.getMessage().getMentionedUsers().forEach(user -> event.getGuild().getController().removeRolesFromMember(event.getGuild().getMember(user), roles).queue());
new MessageBuilder("Successfully removed `" + roles.size() + "` role(s) from " + user.getAsMention() + "!").setColor(event.getMember().getColor()).queue(event.getChannel()); new MessageBuilder("Successfully removed `" + roles.size() + "` role(s) from " + users + " user(s)!").setColor(event.getMember().getColor()).queue(event.getChannel());
} catch (PermissionException ex) { } catch (PermissionException ex) {
new MessageBuilder(event.getMember().getAsMention() + ", sorry I do not have permission for this!").setColor(event.getMember().getColor()).queue(event.getChannel()); new MessageBuilder(event.getMember().getAsMention() + ", sorry I do not have permission for this!").setColor(event.getMember().getColor()).queue(event.getChannel());
} }

View File

@ -0,0 +1,29 @@
package me.savvy.rixa.commands.owner;
import me.savvy.rixa.Rixa;
import me.savvy.rixa.commands.handlers.Command;
import me.savvy.rixa.commands.handlers.CommandExec;
import me.savvy.rixa.commands.handlers.CommandType;
import me.savvy.rixa.utils.MessageBuilder;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
public class OwnerCommand implements CommandExec {
@Override
@Command(mainCommand = "shutdown",
description = "Shutdown Rixa instance, save all data.",
channelType = ChannelType.TEXT,
showInHelp = false,
type = CommandType.BOT_OWNER)
public void execute(GuildMessageReceivedEvent event) {
if (!Rixa.getConfig().getJsonObject().getJSONArray("botAdmins").toList().contains(event.getMember().getUser().getId())) {
new MessageBuilder(event.getMember().getAsMention() + ", you do not have permission for this command.")
.setColor(event.getMember().getColor()).queue(event.getChannel());
return;
}
Rixa.getInstance().close();
Rixa.getInstance().exit();
}
}

View File

@ -6,6 +6,7 @@ import me.savvy.rixa.commands.handlers.CommandHandler;
import me.savvy.rixa.commands.handlers.CommandRegistrar; import me.savvy.rixa.commands.handlers.CommandRegistrar;
import me.savvy.rixa.guild.RixaGuild; import me.savvy.rixa.guild.RixaGuild;
import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.guild.management.Guilds;
import me.savvy.rixa.modules.conversations.ConversationModule;
import me.savvy.rixa.modules.levels.LevelsModule; import me.savvy.rixa.modules.levels.LevelsModule;
import me.savvy.rixa.modules.reactions.handlers.ReactRegistrar; import me.savvy.rixa.modules.reactions.handlers.ReactRegistrar;
import me.savvy.rixa.modules.reactions.handlers.ReactionManager; import me.savvy.rixa.modules.reactions.handlers.ReactionManager;
@ -43,13 +44,15 @@ public class MessageEvent {
if (event.getGuild() == null) return; if (event.getGuild() == null) return;
if (event.getAuthor().isBot()) return; if (event.getAuthor().isBot()) return;
RixaGuild rixaGuild = Guilds.getGuild(event.getGuild());
if (event.getMessage().getContent().startsWith if (event.getMessage().getContent().startsWith
("@" + event.getGuild().getSelfMember().getEffectiveName())) { ("@" + event.getGuild().getSelfMember().getEffectiveName())) {
try { try {
String s = event.getMessage().getContent().replace String s = event.getMessage().getContent().replace
("@" + event.getGuild().getSelfMember().getEffectiveName() + " ", ""); ("@" + event.getGuild().getSelfMember().getEffectiveName() + " ", "");
if (s.isEmpty()) return; if (s.isEmpty()) return;
s = Rixa.getChatBotSession().think(s); s = ((ConversationModule) rixaGuild.getModule("Conversations")).getChatBotSession().think(s);
if (s.isEmpty()) return; if (s.isEmpty()) return;
event.getChannel().sendMessage(s).queue(); event.getChannel().sendMessage(s).queue();
return; return;
@ -57,15 +60,12 @@ public class MessageEvent {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
RixaGuild rixaGuild = Guilds.getGuild(event.getGuild());
String prefix = rixaGuild String prefix = rixaGuild
.getGuildSettings() .getGuildSettings()
.getPrefix(); .getPrefix();
//checkMessage(event.getMessage()); //checkMessage(event.getMessage());
if (!event.getMessage().getContent().startsWith(prefix)) { if (!event.getMessage().getContent().startsWith(prefix)) {
if (!(((LevelsModule) rixaGuild.getModule("Levels")).isEnabled())) { if (!(rixaGuild.getModule("Levels").isEnabled())) {
return; return;
} }
/*if(!event.getAuthor().getId().equalsIgnoreCase("202944101333729280") && /*if(!event.getAuthor().getId().equalsIgnoreCase("202944101333729280") &&

View File

@ -11,6 +11,7 @@ import me.savvy.rixa.enums.Result;
import me.savvy.rixa.guild.management.GuildSettings; import me.savvy.rixa.guild.management.GuildSettings;
import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.guild.management.Guilds;
import me.savvy.rixa.modules.RixaModule; import me.savvy.rixa.modules.RixaModule;
import me.savvy.rixa.modules.conversations.ConversationModule;
import me.savvy.rixa.modules.levels.LevelsModule; import me.savvy.rixa.modules.levels.LevelsModule;
import me.savvy.rixa.modules.music.MusicModule; import me.savvy.rixa.modules.music.MusicModule;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Guild;
@ -45,8 +46,9 @@ public class RixaGuild {
this.guild = guild; this.guild = guild;
this.modules = new HashMap<>(); this.modules = new HashMap<>();
this.db = Rixa.getDatabase(); this.db = Rixa.getDatabase();
modules.put("Music", new MusicModule()); modules.put("Music", new MusicModule(this));
modules.put("Levels", new LevelsModule()); modules.put("Levels", new LevelsModule(this));
modules.put("Conversations", new ConversationModule(this));
load(); load();
} }

View File

@ -60,6 +60,7 @@ public class GuildSettings {
if (!optional.isPresent()) return; if (!optional.isPresent()) return;
if (!(optional.get() instanceof ResultSet)) return; if (!(optional.get() instanceof ResultSet)) return;
ResultSet set = (ResultSet) optional.get(); ResultSet set = (ResultSet) optional.get();
if (set.next()) {
this.prefix = (set.getString("prefix")); this.prefix = (set.getString("prefix"));
this.defaultRole = (set.getString("defaultRole")); this.defaultRole = (set.getString("defaultRole"));
this.joinMessage = (set.getString("joinMessage")); this.joinMessage = (set.getString("joinMessage"));
@ -73,14 +74,17 @@ public class GuildSettings {
if (!set.getString("farewell").equalsIgnoreCase("default_value")) { if (!set.getString("farewell").equalsIgnoreCase("default_value")) {
quitMessageChannel = guild.getTextChannelById(set.getString("farewell")); quitMessageChannel = guild.getTextChannelById(set.getString("farewell"));
} }
}
query = new Query("SELECT * FROM `core` WHERE `guild_id` = ?"); query = new Query("SELECT * FROM `core` WHERE `guild_id` = ?");
query.setString(guild.getId()); query.setString(guild.getId());
optional = Rixa.getDatabase().send(query); optional = Rixa.getDatabase().send(query);
if (!optional.isPresent()) return; if (!optional.isPresent()) return;
if (!(optional.get() instanceof ResultSet)) return; if (!(optional.get() instanceof ResultSet)) return;
set = (ResultSet) optional.get(); set = (ResultSet) optional.get();
if (set.next()) {
this.description = (set.getString("description")); this.description = (set.getString("description"));
this.enlisted = (set.getBoolean("enlisted")); this.enlisted = (set.getBoolean("enlisted"));
}
this.raidMode = false; this.raidMode = false;
} }

View File

@ -1,7 +1,5 @@
package me.savvy.rixa.modules; package me.savvy.rixa.modules;
import me.savvy.rixa.guild.RixaGuild;
/** /**
* Created by Timber on 5/23/2017. * Created by Timber on 5/23/2017.
*/ */
@ -13,7 +11,7 @@ public interface RixaModule {
boolean isEnabled(); boolean isEnabled();
void load(RixaGuild guild); void load();
void save(); void save();
} }

View File

@ -0,0 +1,50 @@
package me.savvy.rixa.modules.conversations;
import com.google.code.chatterbotapi.*;
import me.savvy.rixa.guild.RixaGuild;
import me.savvy.rixa.modules.RixaModule;
public class ConversationModule implements RixaModule {
private final RixaGuild rixaGuild;
private ChatterBotFactory factory;
private ChatterBotSession chatBotSession;
private ChatterBot chatBot;
public ConversationModule(RixaGuild rixaGuild) {
this.rixaGuild = rixaGuild;
load();
}
@Override
public String getName() {
return "ConversationModule";
}
@Override
public String getDescription() {
return "Conversation API - PandoraBots";
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public void load() {
try {
factory = new ChatterBotFactory();
chatBot = factory.create(ChatterBotType.PANDORABOTS, "b0dafd24ee35a477");
chatBotSession = chatBot.createSession();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void save() {}
public ChatterBotSession getChatBotSession() {
return chatBotSession;
}
}

View File

@ -10,6 +10,9 @@ import java.util.Map;
public class Economy implements RixaModule { public class Economy implements RixaModule {
public Economy(RixaGuild rixaGuild) {
this.rixaGuild = rixaGuild;
}
@Getter @Getter
private RixaGuild rixaGuild; private RixaGuild rixaGuild;
@Getter @Getter
@ -33,9 +36,7 @@ public class Economy implements RixaModule {
} }
@Override @Override
public void load(RixaGuild rixaGuild) { public void load() { }
this.rixaGuild = rixaGuild;
}
@Override @Override
public void save() { public void save() {
@ -44,6 +45,6 @@ public class Economy implements RixaModule {
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
this.enabled = enabled; this.enabled = enabled;
DatabaseUtils.update("modules", "levels", "guild_id", enabled, rixaGuild.getGuild().getId()); DatabaseUtils.update("modules", "x", "guild_id", enabled, rixaGuild.getGuild().getId());
} }
} }

View File

@ -13,6 +13,7 @@ import me.savvy.rixa.utils.DatabaseUtils;
import me.savvy.rixa.utils.MessageBuilder; import me.savvy.rixa.utils.MessageBuilder;
import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Member;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
@ -30,12 +31,42 @@ public class LevelsModule implements RixaModule {
@Setter @Setter
private boolean enabled; private boolean enabled;
public LevelsModule(RixaGuild rixaGuild) {
this.rixaGuild = rixaGuild;
load();
}
@Override
public void load() {
try {
Query query = new Query("SELECT * FROM `modules` WHERE `guild_id`=?;");
query.setString(rixaGuild.getGuild().getId());
Optional<?> o = Rixa.getDatabase().send(query);
if (!o.isPresent()) return;
else if (!(o.get() instanceof ResultSet)) return;
ResultSet set = (ResultSet) o.get();
if (set.next()) {
setEnabled(set.getBoolean("levels"));
} else {
Update update = new Update("INSERT INTO `modules` (`guild_id`) VALUES (?);");
update.setString(rixaGuild.getGuild().getId());
Rixa.getDatabase().send(update);
setEnabled(true);
}
set.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private List<UserData> leaderboard(Member member) { private List<UserData> leaderboard(Member member) {
Database db = Rixa.getDatabase(); Database db = Rixa.getDatabase();
ResultSet rs = null; ResultSet rs = null;
try { try {
rs = db.getConnection().get().prepareStatement(String.format("SELECT * FROM `levels` WHERE `guild_id` = '%s' ORDER BY `experience` DESC;", member.getGuild().getId())).executeQuery(); PreparedStatement ps = db.getConnection().get().prepareStatement("SELECT * FROM `levels` WHERE `guild_id` = ? ORDER BY `experience` DESC;");
ps.setString(1, member.getGuild().getId());
rs = ps.executeQuery();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -98,30 +129,6 @@ public class LevelsModule implements RixaModule {
return "Rixa levels module."; return "Rixa levels module.";
} }
@Override
public void load(RixaGuild rixaGuild) {
try {
this.rixaGuild = rixaGuild;
Query query = new Query("SELECT * FROM `modules` WHERE `guild_id`=?;");
query.setString(rixaGuild.getGuild().getId());
Optional<?> o = Rixa.getDatabase().send(query);
if (!o.isPresent()) return;
else if (!(o.get() instanceof ResultSet)) return;
ResultSet set = (ResultSet) o.get();
if (set.next()) {
setEnabled(set.getBoolean("levels"));
} else {
Update update = new Update("INSERT INTO `modules` (`guild_id`) VALUES (?);");
update.setString(rixaGuild.getGuild().getId());
Rixa.getDatabase().send(update);
setEnabled(true);
}
set.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override @Override
public void save() { public void save() {
DatabaseUtils.update("modules", "levels", "guild_id", enabled, rixaGuild.getGuild().getId()); DatabaseUtils.update("modules", "levels", "guild_id", enabled, rixaGuild.getGuild().getId());

View File

@ -20,6 +20,9 @@ import java.util.Optional;
*/ */
public class MusicModule implements RixaModule { public class MusicModule implements RixaModule {
public MusicModule(RixaGuild rixaGuild) {
this.guild = rixaGuild.getGuild();
}
private Database db; private Database db;
@Getter @Getter
@Setter @Setter
@ -31,9 +34,8 @@ public class MusicModule implements RixaModule {
private Guild guild; private Guild guild;
@Override @Override
public void load(RixaGuild rixaGuild) { public void load() {
try { try {
this.guild = rixaGuild.getGuild();
this.enabled = false; this.enabled = false;
this.musicRole = "default_value"; this.musicRole = "default_value";
db = Rixa.getDatabase(); db = Rixa.getDatabase();

View File

@ -62,7 +62,7 @@ public class TwitterModule implements RixaModule {
} }
@Override @Override
public void load(RixaGuild guild) { } public void load() { }
@Override @Override
public void save() { } public void save() { }

View File

@ -13,6 +13,7 @@ import java.util.Optional;
public class DatabaseUtils { public class DatabaseUtils {
public static Result update(String table, String setting, String key, Object placeholder, Object placeholder2) { public static Result update(String table, String setting, String key, Object placeholder, Object placeholder2) {
Update update = new Update("UPDATE `" + table + "` SET `" + setting + "` = ? WHERE `" + key + "` = ?;"); Update update = new Update("UPDATE `" + table + "` SET `" + setting + "` = ? WHERE `" + key + "` = ?;");
update.setObject(placeholder); update.setObject(placeholder);
update.setObject(placeholder2); update.setObject(placeholder2);