Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reset command #69

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions src/main/java/com/jagrosh/vortex/Vortex.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public Vortex() throws Exception
new ModroleCmd(this),
new PrefixCmd(this),
new SettingsCmd(this),
new ResetCmd(this),

// Automoderation
new AntiinviteCmd(this),
Expand Down
183 changes: 183 additions & 0 deletions src/main/java/com/jagrosh/vortex/commands/settings/ResetCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* Copyright 2018 John Grosh (john.a.grosh@gmail.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jagrosh.vortex.commands.settings;

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.menu.ButtonMenu;
import com.jagrosh.vortex.Vortex;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.exceptions.PermissionException;

import java.util.concurrent.TimeUnit;

/**
*
* @author Michaili K (mysteriouscursor+git@protonmail.com)
*/
public class ResetCmd extends Command
{
private final Vortex vortex;

private static final String CONFIRM_RESET_EMOJI = "\u2705"; // ✅
private static final String CANCEL_RESET_EMOJI = "\u274c"; // ❌

public ResetCmd(Vortex vortex)
{
this.vortex = vortex;
this.name = "reset";
this.help = "reset vortex settings/data to default";
this.arguments = "<automod|filters|settings|ignores|punishments|strikes|all>";
this.category = new Category("Settings");
this.guildOnly = true;
this.userPermissions = new Permission[]{Permission.MANAGE_SERVER};
MichailiK marked this conversation as resolved.
Show resolved Hide resolved
this.cooldown = 60;
}

@Override
protected void execute(CommandEvent event)
{
if(event.getArgs().isEmpty())
{
event.replyError("Please include the section to reset");
return;
}

ButtonMenu.Builder menuBuilder = new ButtonMenu.Builder()
.addChoice(CONFIRM_RESET_EMOJI)
.addChoice(CANCEL_RESET_EMOJI)
.setUsers(event.getAuthor())
.setTimeout(30, TimeUnit.SECONDS)
.setEventWaiter(vortex.getEventWaiter())
.setFinalAction(m -> {
try
{
m.clearReactions().queue();
}
catch (PermissionException ignored)
{
}
});

ThrowableConsumer<Guild> consumer;
String description;

switch (event.getArgs().toLowerCase())
{
case "automod":
description = "**Reset Automod**\n" +
"You are about to reset Automod.\n" +
"This will result in all Automod settings to be disabled.\n" +
"Filters aren't affected by this reset.\n\n" +
"Are you sure you want to reset all Automod settings?";

consumer = (guild) -> vortex.getDatabase().automod.reset(guild);
break;


case "filters":
description = "**Reset Filters**\n" +
"You are about to remove all filters.\n" +
"Are you sure you want to remove all Filters?";
consumer = (guild) -> vortex.getDatabase().filters.deleteAllFilters(guild.getIdLong());
break;


case "settings":
description = "**Reset Settings**\n" +
"You are about to reset all Server Settings.\n" +
"This will result in raid mode being disabled & Server Settings like mod role & logs to be reset.\n" +
"Punishments, Automod, Ignored roles/channels & Filters aren't affected by this reset.\n" +
"Are you sure you want to reset all Server Settings?";
consumer = (guild) -> vortex.getDatabase().settings.reset(guild);
break;


case "ignores":
description = "**Reset Ignores**\n" +
"You are about to reset ignored channels & roles.\n" +
"This will result in AutoMod no longer ignoring any channels & roles added to the ignore list.\n" +
"Are you sure you want to reset all ignored channels & roles?";
consumer = (guild) -> vortex.getDatabase().ignores.unignoreAll(guild);
break;


case "punishments":
description = "**Reset Punishments**\n" +
"You are about to reset all punishments.\n" +
"This will result in all punishments being removed & as such, strikes no longer mute, kick or ban members.\n" +
"Are you sure you want to reset all punishments?";
consumer = (guild) -> vortex.getDatabase().actions.removeAllActions(guild);
break;

case "strikes":
description = "**Reset Strikes**\n" +
"You are about to reset **__all__** strikes.\n" +
"This will result in all strikes being pardoned from all members.\n" +
"No bans or mutes will be lifted.\n" +
"Are you sure you want to reset all strikes?";
consumer = (guild) -> vortex.getDatabase().strikes.resetAllStrikes(guild);
break;

case "all":
description = "**Reset All**\n" +
"You are about to reset **__all__** settings & strikes.\n" +
"This will result in Automod, settings, filters, punishments & strikes to be reset.\n" +
"No bans or mutes will be lifted.\n" +
"Are you sure you want to reset all settings & strikes?";
consumer = (guild) ->
{
vortex.getDatabase().automod.reset(guild);
vortex.getDatabase().filters.deleteAllFilters(guild.getIdLong());
vortex.getDatabase().settings.reset(guild);
vortex.getDatabase().ignores.unignoreAll(guild);
vortex.getDatabase().actions.removeAllActions(guild);
vortex.getDatabase().strikes.resetAllStrikes(guild);
};
break;

default:
event.replyError("Unknown section");
return;
}

menuBuilder
.setDescription(description)
.setAction(reactionEmote ->
{
if (reactionEmote.getName().equals(CONFIRM_RESET_EMOJI))
{
try
{
consumer.call(event.getGuild());
event.replySuccess("Reset successful");
}
catch (Exception e)
{
event.replyError("An error occurred while resetting");
}
}
})
.build()
.display(event.getTextChannel());
}

private interface ThrowableConsumer<T>
{
void call(T t) throws Exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ public void setDehoistChar(Guild guild, char dehoistChar)
}
});
}

public void reset(Guild guild)
{
invalidateCache(guild);
readWrite(selectAll(GUILD_ID.is(guild.getIdLong())), rs ->
{
if(rs.next())
rs.deleteRow();
});
}

private void invalidateCache(Guild guild)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,15 @@ public void setTimezone(Guild guild, ZoneId zone)
}
});
}

public void reset(Guild guild)
MichailiK marked this conversation as resolved.
Show resolved Hide resolved
{
readWrite(selectAll(GUILD_ID.is(guild.getIdLong())), rs ->
{
if(rs.next())
rs.deleteRow();
});
}

public void enableRaidMode(Guild guild)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ public boolean unignore(Role role)
return false;
});
}

public void unignoreAll(Guild guild)
{
MichailiK marked this conversation as resolved.
Show resolved Hide resolved
readWrite(selectAll(GUILD_ID.is(guild.getIdLong())), rs ->
{
while(rs.next())
rs.deleteRow();
});
}

private void invalidateCache(Guild guild)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ public void removeAction(Guild guild, int numStrikes)
rs.deleteRow();
});
}

public void removeAllActions(Guild guild)
{
readWrite(selectAll(GUILD_ID.is(guild.getIdLong())), rs ->
{
while(rs.next())
rs.deleteRow();
});
}

public void setAction(Guild guild, int numStrikes, Action action)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,13 @@ public JSONObject getAllStrikesJson(Guild guild)
getAllStrikes(guild).entrySet().forEach(e -> obj.put(Long.toString(e.getKey()), e.getValue()));
return obj;
}

public void resetAllStrikes(Guild guild)
{
readWrite(selectAll(GUILD_ID.is(guild.getIdLong())), rs ->
{
while(rs.next())
rs.deleteRow();
});
}
}