Added Color Command, added JDA Version to Info Command, added shutdown message to shutdown command, fixed permissions bug (again)

This commit is contained in:
Savvy 2017-10-02 17:21:48 -04:00
parent 001ddf5d30
commit d4f6a52d44
13 changed files with 180 additions and 32 deletions

View File

@ -136,7 +136,7 @@ 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 OwnerCommand()); new LeaderboardCommand(), new RaidModeCommand(), new OwnerCommand(), new ColorCommand());
register(new HelpReaction(), new ConfigReaction(), new LeaderboardReaction()); register(new HelpReaction(), new ConfigReaction(), new LeaderboardReaction());
} }

View File

@ -0,0 +1,77 @@
package me.savvy.rixa.commands.general;
import me.savvy.rixa.commands.handlers.Command;
import me.savvy.rixa.commands.handlers.CommandExec;
import me.savvy.rixa.guild.RixaGuild;
import me.savvy.rixa.guild.management.Guilds;
import me.savvy.rixa.old.RixaColor;
import me.savvy.rixa.utils.MessageBuilder;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import java.awt.*;
import java.io.File;
public class ColorCommand implements CommandExec {
@Override
@Command(mainCommand = "color",
description = "View a HEX/RGB Color!",
channelType = ChannelType.TEXT)
public void execute(GuildMessageReceivedEvent event) {
RixaGuild rixaGuild = Guilds.getGuild(event.getGuild());
String message = event.getMessage().getContent();
message = message.replace(rixaGuild.getGuildSettings().getPrefix() + "color ", "");
RixaColor rixaColor = RixaColor.getInstance();
Color color = null;
if (message.startsWith("#")) {
try {
color = Color.decode(message);
} catch (NumberFormatException ex) {
new MessageBuilder(message + " is not a number! Example: `" +
rixaGuild.getGuildSettings().getPrefix() + "color #212121`").setColor(event.getMember().getColor()).queue(event.getChannel());
return;
}
} else {
String[] args = message.split(" ");
if (args.length != 3) {
new MessageBuilder(message + " is not a number! Example: `" +
rixaGuild.getGuildSettings().getPrefix() + "color 6 2 9`").setColor(event.getMember().getColor()).queue(event.getChannel());
return;
}
int r = 0, g = 0, b = 0;
for(int i = 0; i < args.length; i++) {
Integer x;
try {
x = Integer.parseInt(args[i]);
} catch (NumberFormatException ex) {
new MessageBuilder(args[i] + " is not a number! Example: `" +
rixaGuild.getGuildSettings().getPrefix() + "color 6 2 9`").setColor(event.getMember().getColor()).queue(event.getChannel());
return;
}
switch (i) {
case 0:
r = x;
break;
case 1:
g = x;
break;
case 2:
b = x;
}
}
try {
color = new Color(r, g, b);
} catch (IllegalArgumentException ex) {
new MessageBuilder(ex.getMessage()).setColor(event.getMember().getColor()).queue(event.getChannel());
return;
}
}
File file = rixaColor.coverIMG(color);
String hex = String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
net.dv8tion.jda.core.MessageBuilder msg = new net.dv8tion.jda.core.MessageBuilder();
msg.setEmbed(new MessageBuilder("Hex: " + hex + ", RGB: " + color.getRed() + ", " + color.getGreen() + ", " + color.getBlue()).getBuilder().build());
event.getChannel().sendFile(file, msg.build()).queue();
}
}

View File

@ -6,6 +6,7 @@ import me.savvy.rixa.commands.handlers.CommandExec;
import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.commands.handlers.CommandType;
import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDAInfo;
import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.MessageEmbed; import net.dv8tion.jda.core.entities.MessageEmbed;
@ -57,6 +58,7 @@ public class InfoCommand implements CommandExec {
.addField("Bot Uptime ", "Uptime: " + day + " days " + hours + " hours " + minute + " minutes " + second + " seconds.", true) .addField("Bot Uptime ", "Uptime: " + day + " days " + hours + " hours " + minute + " minutes " + second + " seconds.", true)
.addField("Total Guilds", String.valueOf(guildCount), true) .addField("Total Guilds", String.valueOf(guildCount), true)
.addField("Total Users", String.valueOf(userCount), true) .addField("Total Users", String.valueOf(userCount), true)
.addField("JDA Version", JDAInfo.VERSION, true)
.addField("Rixa Developer", botOwner.getName() + "#" + botOwner.getDiscriminator(), true) .addField("Rixa Developer", botOwner.getName() + "#" + botOwner.getDiscriminator(), true)
.setFooter("Requested by " + event.getAuthor().getName() + "#" + event.getAuthor().getDiscriminator(), event.getAuthor().getAvatarUrl()); .setFooter("Requested by " + event.getAuthor().getName() + "#" + event.getAuthor().getDiscriminator(), event.getAuthor().getAvatarUrl());
event.getChannel().sendMessage(messageEmbed.build()).queue(); event.getChannel().sendMessage(messageEmbed.build()).queue();

View File

@ -7,7 +7,6 @@ import me.savvy.rixa.commands.handlers.CommandType;
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.utils.MessageBuilder; import me.savvy.rixa.utils.MessageBuilder;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
@ -27,6 +26,7 @@ public class OwnerCommand implements CommandExec {
} }
try { try {
new MessageBuilder("Shutting down...").queue(event.getChannel());
for (RixaGuild rixaGuild : Guilds.getGuilds().values()) { for (RixaGuild rixaGuild : Guilds.getGuilds().values()) {
Thread.sleep(50); Thread.sleep(50);
rixaGuild.save(); rixaGuild.save();

View File

@ -61,8 +61,8 @@ public class SQLBuilder {
} }
public Connection getConnection() { public Connection getConnection() throws SQLException {
if (connection == null) { if (connection == null || connection.isClosed()) {
connect(); connect();
} }
return connection; return connection;

View File

@ -50,7 +50,7 @@ public class RixaGuild {
load(); load();
} }
public void load() { private void load() {
if (!(checkExists())) { if (!(checkExists())) {
try { try {
PreparedStatement ps = db.getPreparedStatement("INSERT INTO `core` (`guild_id`, `guild_name`, `description`, `keywords`) VALUES (?, ?, 'Description not set.', 'No Keywords Found.')\""); PreparedStatement ps = db.getPreparedStatement("INSERT INTO `core` (`guild_id`, `guild_name`, `description`, `keywords`) VALUES (?, ?, 'Description not set.', 'No Keywords Found.')\"");
@ -105,9 +105,8 @@ public class RixaGuild {
boolean b = false; boolean b = false;
try { try {
PreparedStatement ps = db.getPreparedStatement("SELECT ? FROM `permissions` WHERE `role_id` = ?"); PreparedStatement ps = db.getPreparedStatement("SELECT `" + permission.toString().toUpperCase() + "` FROM `permissions` WHERE `role_id` = ?");
ps.setString(1, permission.toString().toUpperCase()); ps.setString(1, role.getId());
ps.setString(2, role.getId());
ResultSet set = ps.executeQuery(); ResultSet set = ps.executeQuery();
if (set.next()) { if (set.next()) {
b = set.getBoolean(permission.toString().toUpperCase()); b = set.getBoolean(permission.toString().toUpperCase());
@ -135,11 +134,10 @@ public class RixaGuild {
} }
} }
try { try {
PreparedStatement ps = db.getPreparedStatement("UPDATE `permissions` SET ? = ? WHERE `guild_id` = ? AND `role_id` = ?;"); PreparedStatement ps = db.getPreparedStatement("UPDATE `permissions` SET `" + permission.toString().toUpperCase() + "` = ? WHERE `guild_id` = ? AND `role_id` = ?;");
ps.setString(1, permission.toString().toUpperCase()); ps.setBoolean(1, value);
ps.setBoolean(2, value); ps.setString(2, guild.getId());
ps.setString(3, guild.getId()); ps.setString(3, role.getId());
ps.setString(4, role.getId());
db.executeUpdate(ps); db.executeUpdate(ps);
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -148,10 +146,9 @@ public class RixaGuild {
private boolean permissionExists(Role role) { private boolean permissionExists(Role role) {
try { try {
PreparedStatement query = db.getPreparedStatement("SELECT ? FROM `permissions` WHERE `guild_id` = ? AND `role_id` = ?"); PreparedStatement query = db.getPreparedStatement("SELECT `" + RixaPermission.values()[0].toString().toUpperCase() + "` FROM `permissions` WHERE `guild_id` = ? AND `role_id` = ?");
query.setString(1, RixaPermission.values()[0].toString().toUpperCase()); query.setString(1, guild.getId());
query.setString(2, guild.getId()); query.setString(2, role.getId());
query.setString(3, role.getId());
ResultSet set = query.executeQuery(); ResultSet set = query.executeQuery();
boolean b = set.next(); boolean b = set.next();
query.close(); query.close();

View File

@ -131,7 +131,7 @@ public class UserData {
statement.setString(1, guild.getId()); statement.setString(1, guild.getId());
statement.setString(2, user.getId()); statement.setString(2, user.getId());
statement.setInt(3, 0); statement.setInt(3, 0);
statement.executeUpdate(); Rixa.getDatabase().executeUpdate(statement);
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -145,7 +145,7 @@ public class UserData {
ps.setInt(1, experience); ps.setInt(1, experience);
ps.setString(2, guild.getId()); ps.setString(2, guild.getId());
ps.setString(3, user.getId()); ps.setString(3, user.getId());
ps.executeUpdate(); Rixa.getDatabase().executeUpdate(ps);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -35,17 +35,12 @@ public class ConversationModule implements RixaModule {
return "Conversation API - PandoraBots"; return "Conversation API - PandoraBots";
} }
@Override
public boolean isEnabled() {
return enabled;
}
@Override @Override
public void load() { public void load() {
try { try {
PreparedStatement ps = Rixa.getDatabase().getPreparedStatement("SELECT `conversations` FROM `modules` WHERE `guild_id` = ?"); PreparedStatement ps = Rixa.getDatabase().getPreparedStatement("SELECT `conversations` FROM `modules` WHERE `guild_id` = ?");
ps.setString(1, rixaGuild.getGuild().getId()); ps.setString(1, rixaGuild.getGuild().getId());
this.enabled = Rixa.getDatabase().getBoolean(ps, "enabled"); this.enabled = Rixa.getDatabase().getBoolean(ps, "conversations");
factory = new ChatterBotFactory(); factory = new ChatterBotFactory();
chatBot = factory.create(ChatterBotType.PANDORABOTS, "b0dafd24ee35a477"); chatBot = factory.create(ChatterBotType.PANDORABOTS, "b0dafd24ee35a477");
chatBotSession = chatBot.createSession(); chatBotSession = chatBot.createSession();

View File

@ -15,7 +15,7 @@ import twitter4j.conf.ConfigurationBuilder;
*/ */
public class TwitterModule implements RixaModule { public class TwitterModule implements RixaModule {
@Getter private boolean enabled; @Getter @Setter private boolean enabled;
@Getter @Getter
private final TwitterStream twitterStream; private final TwitterStream twitterStream;
@Getter @Getter

View File

@ -0,0 +1,45 @@
package me.savvy.rixa.old;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class RixaColor {
private static RixaColor instance;
private File file;
public RixaColor() {
instance = this;
this.file = new File("Rixa/color.jpg");
}
public File coverIMG(Color color) {
int rgb = color.getRGB();
try {
BufferedImage img = null;
try {
img = ImageIO.read(file);
} catch (IOException ex) {
return null;
}
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
img.setRGB(i, j, rgb);
}
}
File outputfile = new File("Rixa/color.jpg");
ImageIO.write(img, "jpg", outputfile);
return outputfile;
} catch (Exception ignored) {}
return null;
}
public static RixaColor getInstance() {
return (instance == null ? new RixaColor() : instance);
}
}

View File

@ -13,10 +13,9 @@ 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) {
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
statement = Rixa.getDatabase().getPreparedStatement("UPDATE `" + table + "` SET ? = ? WHERE `" + key + "` = ?;"); statement = Rixa.getDatabase().getPreparedStatement("UPDATE `" + table + "` SET `" + setting + "` = ? WHERE `" + key + "` = ?;");
statement.setString(1, setting); statement.setObject(1, placeholder);
statement.setObject(2, placeholder); statement.setObject(2, placeholder2);
statement.setObject(3, placeholder2);
statement.executeUpdate(); statement.executeUpdate();
statement.close(); statement.close();
} catch (SQLException e) { } catch (SQLException e) {

View File

@ -60,8 +60,8 @@ public class MessageBuilder {
channel.sendMessage(builder.build()).queue(); channel.sendMessage(builder.build()).queue();
} }
public void complete(TextChannel channel) { public Message complete(TextChannel channel) {
channel.sendMessage(builder.build()).complete(); return channel.sendMessage(builder.build()).complete();
} }
public void send(User member) { public void send(User member) {

View File

@ -1,5 +1,12 @@
package me.savvy.rixa.utils; package me.savvy.rixa.utils;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Role;
import java.util.ArrayList;
import java.util.List;
/** /**
* Created by Timber on 5/23/2017. * Created by Timber on 5/23/2017.
*/ */
@ -13,4 +20,30 @@ public class Utils {
return false; return false;
} }
} }
public static List<Member> memberSearch(Guild guild, String string, boolean bots) {
List<Member> members = new ArrayList<>();
String finalString = string.toLowerCase();
for (Member member : guild.getMembers()) {
if ((member.getUser().getName().toLowerCase() + "#" + member.getUser().getDiscriminator()).contains(finalString)
|| (member.getEffectiveName().toLowerCase().contains(finalString))
|| finalString.contains(member.getUser().getId())) {
if (!bots && member.getUser().isBot()) continue;
members.add(member);
}
}
return members;
}
public static List<Role> roleSearch(Guild guild, String string) {
List<Role> roles = new ArrayList<>();
guild.getRoles().forEach(role -> {
if (role.getName().toLowerCase().contains(string.toLowerCase())
|| string.contains(role.getId()))
roles.add(role);
});
return roles;
}
} }