Skip to content

Commit

Permalink
Create warn command
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Jan 21, 2018
1 parent 8082386 commit 29fff86
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 144 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ plugins {

group = 'ml.duncte123.skybot'

version '3.57.8'
version '3.57.9'

sourceCompatibility = 1.8

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ml/duncte123/skybot/BotListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void onReady(ReadyEvent event){
if (!unbanTimerRunning && AirUtils.nonsqlite) {
logger.info("Starting the unban timer.");
//Register the timer for the auto unbans
unbanService.scheduleAtFixedRate(() -> AirUtils.checkUnbans(event.getJDA().asBot().getShardManager()),10, 10, TimeUnit.MINUTES);
unbanService.scheduleAtFixedRate(() -> ModerationUtils.checkUnbans(event.getJDA().asBot().getShardManager()),10, 10, TimeUnit.MINUTES);
unbanTimerRunning = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import ml.duncte123.skybot.objects.command.Command;
import ml.duncte123.skybot.objects.command.CommandCategory;
import ml.duncte123.skybot.utils.AirUtils;
import ml.duncte123.skybot.utils.GuildUtils;
import ml.duncte123.skybot.utils.ModerationUtils;
import ml.duncte123.skybot.utils.Settings;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.User;
Expand Down Expand Up @@ -77,7 +77,7 @@ public void executeCommand( String invoke, String[] args, GuildMessageReceivedEv
String newReason = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " ");
event.getGuild().getController().ban(toBan.getId(), 1, reason).queue(
(__) -> {
AirUtils.modLog(event.getAuthor(), toBan, "banned", newReason, event.getGuild());
ModerationUtils.modLog(event.getAuthor(), toBan, "banned", newReason, event.getGuild());
sendSuccess(event.getMessage());
}
);
Expand Down Expand Up @@ -143,18 +143,18 @@ public void executeCommand( String invoke, String[] args, GuildMessageReceivedEv
event.getGuild().getController().ban(toBan.getId(), 1, reason).queue(
(voidMethod) -> {
if (finalBanTime > 0) {
AirUtils.addBannedUserToDb(event.getAuthor().getId(), toBan.getName(), toBan.getDiscriminator(), toBan.getId(), finalUnbanDate, event.getGuild().getId());
ModerationUtils.addBannedUserToDb(event.getAuthor().getId(), toBan.getName(), toBan.getDiscriminator(), toBan.getId(), finalUnbanDate, event.getGuild().getId());

AirUtils.modLog(event.getAuthor(), toBan, "banned", reason, args[1], event.getGuild());
ModerationUtils.modLog(event.getAuthor(), toBan, "banned", reason, args[1], event.getGuild());
} else {
final String newReason = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " ");
AirUtils.modLog(event.getAuthor(), toBan, "banned", newReason, event.getGuild());
ModerationUtils.modLog(event.getAuthor(), toBan, "banned", newReason, event.getGuild());
}
}
);
} else {
event.getGuild().getController().ban(toBan.getId(), 1, "No reason was provided").queue(
(v) -> AirUtils.modLog(event.getAuthor(), toBan, "banned", "*No reason was provided.*", event.getGuild())
(v) -> ModerationUtils.modLog(event.getAuthor(), toBan, "banned", "*No reason was provided.*", event.getGuild())
);
}
} catch (HierarchyException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import ml.duncte123.skybot.objects.command.Command;
import ml.duncte123.skybot.objects.command.CommandCategory;
import ml.duncte123.skybot.utils.AirUtils;
import ml.duncte123.skybot.utils.ModerationUtils;
import ml.duncte123.skybot.utils.Settings;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.User;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void executeCommand(String invoke, String[] args, GuildMessageReceivedEve
String reason = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " ");
event.getGuild().getController().kick(toKick.getId(), "Kicked by " + event.getAuthor().getName() + "\nReason: " + reason).queue(
(noting) -> {
AirUtils.modLog(event.getAuthor(), toKick, "kicked", reason, event.getGuild());
ModerationUtils.modLog(event.getAuthor(), toKick, "kicked", reason, event.getGuild());
sendSuccess(event.getMessage());
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import ml.duncte123.skybot.objects.command.Command;
import ml.duncte123.skybot.objects.command.CommandCategory;
import ml.duncte123.skybot.utils.AirUtils;
import ml.duncte123.skybot.utils.ModerationUtils;
import ml.duncte123.skybot.utils.Settings;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.User;
Expand Down Expand Up @@ -64,7 +64,7 @@ public void executeCommand(String invoke, String[] args, GuildMessageReceivedEve
String reason = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), " ");
event.getGuild().getController().ban(toBan.getId(), 1, "Kicked by: " + event.getAuthor().getName() + "\nReason: " + reason).queue(
nothing -> {
AirUtils.modLog(event.getAuthor(), toBan, "kicked", reason, event.getGuild());
ModerationUtils.modLog(event.getAuthor(), toBan, "kicked", reason, event.getGuild());
sendSuccess(event.getMessage());
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import ml.duncte123.skybot.objects.command.Command;
import ml.duncte123.skybot.objects.command.CommandCategory;
import ml.duncte123.skybot.utils.AirUtils;
import ml.duncte123.skybot.utils.ModerationUtils;
import ml.duncte123.skybot.utils.Settings;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Guild;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void executeCommand(String invoke, String[] args, GuildMessageReceivedEve
if (bannedUser.getName().equalsIgnoreCase(args[0])) {
guild.getController().unban(bannedUser).reason("Unbanned by " + event.getAuthor().getName()).queue();
event.getChannel().sendMessage("User " + bannedUser.getName() + " unbanned.").queue();
AirUtils.modLog(event.getAuthor(), bannedUser, "unbanned", event.getGuild());
ModerationUtils.modLog(event.getAuthor(), bannedUser, "unbanned", event.getGuild());
return;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/ml/duncte123/skybot/config/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public File getConfigFile() {

@Override
public void save() throws Exception {
final String rawJson = this.config.toString();
final String json = new Ason(rawJson).toString(4);
final String json = this.config.toString();
try {
final BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(this.configFile), "UTF-8"));
Expand Down
123 changes: 0 additions & 123 deletions src/main/java/ml/duncte123/skybot/utils/AirUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,129 +111,6 @@ public static String convertStatus(OnlineStatus status) {
}
}

/**
* This will send a message to a channel called modlog
*
* @param mod The mod that performed the punishment
* @param punishedUser The user that got punished
* @param punishment The type of punishment
* @param reason The reason of the punishment
* @param time How long it takes for the punishment to get removed
* @param g A instance of the {@link Guild}
*/
public static void modLog(User mod, User punishedUser, String punishment, String reason, String time, Guild g){
TextChannel logChannel = getLogChannel(GuildSettingsUtils.getGuild(g).getLogChannel(), g);
if(logChannel==null || !logChannel.getGuild().getSelfMember().hasPermission(logChannel, Permission.MESSAGE_WRITE, Permission.MESSAGE_READ)) return;
String length = "";
if (time != null && !time.isEmpty()) {
length = " lasting " + time + "";
}

String punishedUserMention = "<@" + punishedUser.getId() + ">";

logChannel.sendMessage(EmbedUtils.embedField(punishedUser.getName() + " " + punishment, punishment
+ " by " + mod.getName() + length + (reason.isEmpty()?"":" for " + reason))).queue(
msg -> msg.getTextChannel().sendMessage("_Relevant user: " + punishedUserMention + "_").queue()
);
}

/**
* A version of {@link AirUtils#modLog(User, User, String, String, String, Guild)} but without the time
*
* @param mod The mod that performed the punishment
* @param punishedUser The user that got punished
* @param punishment The type of punishment
* @param reason The reason of the punishment
* @param g A instance of the {@link Guild}
*/
public static void modLog(User mod, User punishedUser, String punishment, String reason, Guild g) {
modLog(mod, punishedUser, punishment, reason, "", g);
}

/**
* To log a unban or a unmute
*
* @param mod The mod that permed the executeCommand
* @param unbannedUser The user that the executeCommand is for
* @param punishment The type of punishment that got removed
* @param g A instance of the {@link Guild}
*/
public static void modLog(User mod, User unbannedUser, String punishment, Guild g) {
modLog(mod, unbannedUser, punishment, "", g);
}

/**
* Add the banned user to the database
*
* @param modID The user id from the mod
* @param userName The username from the banned user
* @param userDiscriminator the discriminator from the user
* @param userId the id from the banned users
* @param unbanDate When we need to unban the user
* @param guildId What guild the user got banned in
*/
public static void addBannedUserToDb(String modID, String userName, String userDiscriminator, String userId, String unbanDate, String guildId) {
Map<String, Object> postFields = new TreeMap<>();
postFields.put("modId", modID);
postFields.put("username", userName);
postFields.put("discriminator", userDiscriminator);
postFields.put("userId", userId);
postFields.put("unbanDate", unbanDate);
postFields.put("guildId", guildId);

try {
WebUtils.postRequest(Settings.apiBase + "/ban/json", postFields).close();
} catch (NullPointerException e) {
e.printStackTrace();
}
}

/**
* This will check if there are users that can be unbanned
*
* @param jda the current shard manager for this bot
*/
public static void checkUnbans(ShardManager jda) {
logger.debug("Checking for users to unban");
int usersUnbanned = 0;
Connection database = db.getConnManager().getConnection();

try {

Statement smt = database.createStatement();

ResultSet res = smt.executeQuery("SELECT * FROM " + db.getName() + ".bans");

while (res.next()) {
java.util.Date unbanDate = res.getTimestamp("unban_date");
java.util.Date currDate = new java.util.Date();

if (currDate.after(unbanDate)) {
usersUnbanned++;
logger.info("Unbanning " + res.getString("Username"));
jda.getGuildCache().getElementById(res.getString("guildId")).getController()
.unban(res.getString("userId")).reason("Ban expired").queue();
modLog(new ConsoleUser(),
new FakeUser(res.getString("Username"),
res.getString("userId"),
res.getString("discriminator")),
"unbanned",
jda.getGuildById(res.getString("guildId")));
database.createStatement().executeUpdate("DELETE FROM " + db.getName() + ".bans WHERE id=" + res.getInt("id") + "");
}
}
logger.debug("Checking done, unbanned " + usersUnbanned + " users.");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
database.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
}
}

public static final Pattern URL_REGEX = Pattern.compile("[-a-zA-Z0-9@:%._+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&//=]*)");

/**
Expand Down
Loading

0 comments on commit 29fff86

Please sign in to comment.