Fixed more bugs, allowed for conversations with rixa to be toggled

This commit is contained in:
Savvy 2017-09-30 23:43:48 -04:00
parent bf746fd5ae
commit 001ddf5d30
8 changed files with 45 additions and 30 deletions

View File

@ -6,7 +6,6 @@ import me.savvy.rixa.commands.handlers.CommandType;
import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.commands.handlers.RixaPermission;
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.levels.LevelsModule;
import me.savvy.rixa.modules.music.MusicModule; import me.savvy.rixa.modules.music.MusicModule;
import me.savvy.rixa.utils.MessageBuilder; import me.savvy.rixa.utils.MessageBuilder;
import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.ChannelType;
@ -172,30 +171,33 @@ public class ConfigCommand implements CommandExec {
new MessageBuilder("Successfully set music role to " + role.getName() + "!").setColor(event.getMember().getColor()).queue(event.getChannel()); new MessageBuilder("Successfully set music role to " + role.getName() + "!").setColor(event.getMember().getColor()).queue(event.getChannel());
} }
} else if (messages[1].equalsIgnoreCase("enable")) { } else if (messages[1].equalsIgnoreCase("enable")) {
if (messages[2].equalsIgnoreCase("music")) { rixaGuild.getModules().keySet().forEach(moduleName -> {
((MusicModule) rixaGuild.getModule("Music")).setEnabled(true); if (messages[2].equalsIgnoreCase(moduleName)) {
new MessageBuilder("Successfully enabled the music module!").setColor(event.getMember().getColor()).queue(event.getChannel()); rixaGuild.getModule(moduleName).setEnabled(true);
} else if (messages[2].equalsIgnoreCase("levels")) { new MessageBuilder("Successfully enabled the `" + moduleName + "` module!").setColor(event.getMember().getColor()).queue(event.getChannel());
((LevelsModule) rixaGuild.getModule("Levels")).setEnabled(true); }
event.getChannel().sendMessage("Successfully enabled the levels module").queue(); });
} else if (messages[2].equalsIgnoreCase("joinverification")) { if (messages[2].equalsIgnoreCase("joinverification")) {
Guilds.getGuild(event.getGuild()).getGuildSettings().setJoinVerification(true); Guilds.getGuild(event.getGuild()).getGuildSettings().setJoinVerification(true);
new MessageBuilder("Successfully enabled Join Verification!").setColor(event.getMember().getColor()).queue(event.getChannel()); new MessageBuilder("Successfully enabled Join Verification!").setColor(event.getMember().getColor()).queue(event.getChannel());
} }
} else if (messages[1].equalsIgnoreCase("disable")) { } else if (messages[1].equalsIgnoreCase("disable")) {
if (messages[2].equalsIgnoreCase("music")) {
((MusicModule) rixaGuild.getModule("Music")).setEnabled(false); rixaGuild.getModules().keySet().forEach(moduleName -> {
new MessageBuilder("Successfully disabled the music module!").setColor(event.getMember().getColor()).queue(event.getChannel()); if (messages[2].equalsIgnoreCase(moduleName)) {
} else if (messages[2].equalsIgnoreCase("levels")) { rixaGuild.getModule(moduleName).setEnabled(false);
((MusicModule) rixaGuild.getModule("Music")).setEnabled(false); new MessageBuilder("Successfully disabled the `" + moduleName + "` module!").setColor(event.getMember().getColor()).queue(event.getChannel());
event.getChannel().sendMessage("Successfully disabled the levels module").queue(); }
} else if (messages[2].equalsIgnoreCase("joinverification")) { });
if (messages[2].equalsIgnoreCase("joinverification")) {
Guilds.getGuild(event.getGuild()).getGuildSettings().setJoinVerification(false); Guilds.getGuild(event.getGuild()).getGuildSettings().setJoinVerification(false);
new MessageBuilder("Successfully disabled Join Verification!").setColor(event.getMember().getColor()).queue(event.getChannel()); new MessageBuilder("Successfully disabled Join Verification!").setColor(event.getMember().getColor()).queue(event.getChannel());
} else if (messages[2].equalsIgnoreCase("joinmessage")) { } else if (messages[2].equalsIgnoreCase("joinmessage")) {
Guilds.getGuild(event.getGuild()).getGuildSettings().setJoinMessageChannel("default_value"); Guilds.getGuild(event.getGuild()).getGuildSettings().setJoinMessageChannel("default_value");
new MessageBuilder("Successfully disabled Join messages!").setColor(event.getMember().getColor()).queue(event.getChannel());
} else if (messages[2].equalsIgnoreCase("quitmessage")) { } else if (messages[2].equalsIgnoreCase("quitmessage")) {
Guilds.getGuild(event.getGuild()).getGuildSettings().setQuitMessageChannel("default_value"); Guilds.getGuild(event.getGuild()).getGuildSettings().setQuitMessageChannel("default_value");
new MessageBuilder("Successfully disabled Quit messages!").setColor(event.getMember().getColor()).queue(event.getChannel());
} }
} else if (messages[1].equalsIgnoreCase("addperm") || messages[1].equalsIgnoreCase("addpermission") || messages[1].equalsIgnoreCase("aperm")) { } else if (messages[1].equalsIgnoreCase("addperm") || messages[1].equalsIgnoreCase("addpermission") || messages[1].equalsIgnoreCase("aperm")) {
String permission = "notFound"; String permission = "notFound";

View File

@ -12,8 +12,7 @@ public enum DatabaseTables {
" `experience` int(90) NOT NULL\n" + " `experience` int(90) NOT NULL\n" +
");"), ");"),
MODULES("CREATE TABLE IF NOT EXISTS `modules` ( `guild_id` varchar(255) DEFAULT NULL, `levels` tinyint(1) NOT NULL DEFAULT '1'\n" + MODULES("CREATE TABLE IF NOT EXISTS `modules` (`guild_id` varchar(255) DEFAULT NULL, `levels` tinyint(1) NOT NULL DEFAULT '1', `conversations` tinyint(1) NOT NULL DEFAULT '1');"),
")"),
MUSIC("CREATE TABLE IF NOT EXISTS `music` ( `guild_id` varchar(255) NOT NULL UNIQUE PRIMARY KEY, `music_role` varchar(255) NOT NULL DEFAULT 'default_value'," + MUSIC("CREATE TABLE IF NOT EXISTS `music` ( `guild_id` varchar(255) NOT NULL UNIQUE PRIMARY KEY, `music_role` varchar(255) NOT NULL DEFAULT 'default_value'," +
" `skip_amount` int(5) NOT NULL DEFAULT '0', `max_playlist_amount` int(5) NOT NULL DEFAULT '100', `enabled` tinyint(1) NOT NULL DEFAULT '0'\n" + " `skip_amount` int(5) NOT NULL DEFAULT '0', `max_playlist_amount` int(5) NOT NULL DEFAULT '100', `enabled` tinyint(1) NOT NULL DEFAULT '0'\n" +

View File

@ -56,7 +56,9 @@ public class MessageEvent {
new MessageBuilder(event.getGuild().getName() + "'s server prefix is `" + rixaGuild.getGuildSettings().getPrefix() + "`.").queue(event.getChannel()); new MessageBuilder(event.getGuild().getName() + "'s server prefix is `" + rixaGuild.getGuildSettings().getPrefix() + "`.").queue(event.getChannel());
return; return;
} }
s = ((ConversationModule) rixaGuild.getModule("Conversations")).getChatBotSession().think(s); ConversationModule conversationModule = (ConversationModule) rixaGuild.getModule("Conversations");
if (!conversationModule.isEnabled()) return;
s = conversationModule.getChatBotSession().think(s);
if (s.isEmpty()) return; if (s.isEmpty()) return;
event.getChannel().sendMessage(s).queue(); event.getChannel().sendMessage(s).queue();
return; return;

View File

@ -185,7 +185,7 @@ public class RixaGuild {
} }
} }
public RixaModule getModule(String levels) { public RixaModule getModule(String module) {
return this.modules.get(levels); return this.modules.get(module);
} }
} }

View File

@ -5,6 +5,7 @@ import lombok.Setter;
import me.savvy.rixa.Rixa; import me.savvy.rixa.Rixa;
import me.savvy.rixa.data.database.sql.SQLBuilder; import me.savvy.rixa.data.database.sql.SQLBuilder;
import me.savvy.rixa.enums.Result; import me.savvy.rixa.enums.Result;
import me.savvy.rixa.utils.DatabaseUtils;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.entities.TextChannel;
@ -153,14 +154,7 @@ public class GuildSettings {
} }
private void update(String table, String setting, String key, Object placeholder, Object placeholder2) throws SQLException { private void update(String table, String setting, String key, Object placeholder, Object placeholder2) throws SQLException {
PreparedStatement statement = db.getPreparedStatement("UPDATE ? SET ? = ? WHERE ? = ?;"); DatabaseUtils.update(table, setting, key, placeholder, placeholder2);
statement.setString(1, table);
statement.setString(2, setting);
statement.setObject(3, placeholder);
statement.setString(4, key);
statement.setObject(5, placeholder2);
statement.executeUpdate();
statement.close();
} }
public void setQuitMessageChannel(TextChannel quitMessageChannel) { public void setQuitMessageChannel(TextChannel quitMessageChannel) {

View File

@ -13,5 +13,7 @@ public interface RixaModule {
void load(); void load();
void setEnabled(boolean b);
void save(); void save();
} }

View File

@ -1,8 +1,14 @@
package me.savvy.rixa.modules.conversations; package me.savvy.rixa.modules.conversations;
import com.google.code.chatterbotapi.*; import com.google.code.chatterbotapi.*;
import lombok.Getter;
import lombok.Setter;
import me.savvy.rixa.Rixa;
import me.savvy.rixa.guild.RixaGuild; import me.savvy.rixa.guild.RixaGuild;
import me.savvy.rixa.modules.RixaModule; import me.savvy.rixa.modules.RixaModule;
import me.savvy.rixa.utils.DatabaseUtils;
import java.sql.PreparedStatement;
public class ConversationModule implements RixaModule { public class ConversationModule implements RixaModule {
@ -10,6 +16,10 @@ public class ConversationModule implements RixaModule {
private ChatterBotFactory factory; private ChatterBotFactory factory;
private ChatterBotSession chatBotSession; private ChatterBotSession chatBotSession;
private ChatterBot chatBot; private ChatterBot chatBot;
@Getter
@Setter
private boolean enabled;
public ConversationModule(RixaGuild rixaGuild) { public ConversationModule(RixaGuild rixaGuild) {
this.rixaGuild = rixaGuild; this.rixaGuild = rixaGuild;
@ -27,12 +37,15 @@ public class ConversationModule implements RixaModule {
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
return true; return enabled;
} }
@Override @Override
public void load() { public void load() {
try { try {
PreparedStatement ps = Rixa.getDatabase().getPreparedStatement("SELECT `conversations` FROM `modules` WHERE `guild_id` = ?");
ps.setString(1, rixaGuild.getGuild().getId());
this.enabled = Rixa.getDatabase().getBoolean(ps, "enabled");
factory = new ChatterBotFactory(); factory = new ChatterBotFactory();
chatBot = factory.create(ChatterBotType.PANDORABOTS, "b0dafd24ee35a477"); chatBot = factory.create(ChatterBotType.PANDORABOTS, "b0dafd24ee35a477");
chatBotSession = chatBot.createSession(); chatBotSession = chatBot.createSession();
@ -42,7 +55,9 @@ public class ConversationModule implements RixaModule {
} }
@Override @Override
public void save() {} public void save() {
DatabaseUtils.update("modules", "conversations", "guild_id", enabled, rixaGuild.getGuild().getId());
}
public ChatterBotSession getChatBotSession() { public ChatterBotSession getChatBotSession() {
return chatBotSession; return chatBotSession;

View File

@ -18,6 +18,7 @@ public class DatabaseUtils {
statement.setObject(2, placeholder); statement.setObject(2, placeholder);
statement.setObject(3, placeholder2); statement.setObject(3, placeholder2);
statement.executeUpdate(); statement.executeUpdate();
statement.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
return Result.FALSE; return Result.FALSE;