use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.
the class MiscCmds method iamnotFunction.
protected static void iamnotFunction(String autoroleName, GuildMessageReceivedEvent event) {
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
Map<String, String> autoroles = dbGuild.getData().getAutoroles();
if (autoroles.containsKey(autoroleName)) {
Role role = event.getGuild().getRoleById(autoroles.get(autoroleName));
if (role == null) {
event.getChannel().sendMessage(EmoteReference.ERROR + "The role that this autorole corresponded " + "to has been deleted").queue();
// delete the non-existent autorole.
dbGuild.getData().getAutoroles().remove(autoroleName);
dbGuild.saveAsync();
} else {
if (!(event.getMember().getRoles().stream().filter(r1 -> r1.getId().equals(role.getId())).collect(Collectors.toList()).size() > 0)) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You don't have this role, silly!").queue();
return;
}
try {
event.getGuild().getController().removeRolesFromMember(event.getMember(), role).queue(aVoid -> event.getChannel().sendMessage(EmoteReference.OK + event.getMember().getEffectiveName() + ", you've " + "lost the **" + role.getName() + "** role").queue());
} catch (PermissionException pex) {
event.getChannel().sendMessage(String.format("%sI couldn't give you **%s. Make sure that I have permission to add roles and that my role is above **%s**", EmoteReference.ERROR, role.getName(), role.getName())).queue();
}
}
} else {
event.getChannel().sendMessage(EmoteReference.ERROR + "There isn't an autorole with the name ``" + autoroleName + "``!").queue();
}
}
use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.
the class ModerationCmds method tempban.
@Subscribe
public void tempban(CommandRegistry cr) {
cr.register("tempban", new SimpleCommand(Category.MODERATION) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
String reason = content;
Guild guild = event.getGuild();
User author = event.getAuthor();
TextChannel channel = event.getChannel();
Message receivedMessage = event.getMessage();
if (!guild.getMember(author).hasPermission(net.dv8tion.jda.core.Permission.BAN_MEMBERS)) {
channel.sendMessage(EmoteReference.ERROR + "Cannot ban: You have no Ban Members permission.").queue();
return;
}
if (event.getMessage().getMentionedUsers().isEmpty()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You need to mention an user!").queue();
return;
}
for (User user : event.getMessage().getMentionedUsers()) {
reason = reason.replaceAll("(\\s+)?<@!?" + user.getId() + ">(\\s+)?", "");
}
int index = reason.indexOf("time:");
if (index < 0) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot temp ban an user without giving me the time!").queue();
return;
}
String time = reason.substring(index);
reason = reason.replace(time, "").trim();
time = time.replaceAll("time:(\\s+)?", "");
if (reason.isEmpty()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot temp ban someone without a reason.!").queue();
return;
}
if (time.isEmpty()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot temp ban someone without giving me the time!").queue();
return;
}
final DBGuild db = MantaroData.db().getGuild(event.getGuild());
long l = Utils.parseTime(time);
String finalReason = String.format("Temporally banned by %#s: %s", event.getAuthor(), reason);
String sTime = StringUtils.parseTime(l);
receivedMessage.getMentionedUsers().forEach(user -> guild.getController().ban(user, 7).queue(success -> user.openPrivateChannel().queue(privateChannel -> {
if (!user.isBot()) {
privateChannel.sendMessage(String.format("%sYou were **temporarily banned** by %s#%s with reason: %s on server **%s**.", EmoteReference.MEGA, event.getAuthor().getName(), event.getAuthor().getDiscriminator(), finalReason, event.getGuild().getName())).queue();
}
db.getData().setCases(db.getData().getCases() + 1);
db.saveAsync();
channel.sendMessage(String.format("%s%s (%s got temporarly banned)", EmoteReference.ZAP, modActionQuotes[r.nextInt(modActionQuotes.length)], user.getName())).queue();
ModLog.log(event.getMember(), user, finalReason, ModLog.ModAction.TEMP_BAN, db.getData().getCases(), sTime);
MantaroBot.getTempBanManager().addTempban(guild.getId() + ":" + user.getId(), l + System.currentTimeMillis());
TextChannelGround.of(event).dropItemWithChance(1, 2);
}), error -> {
if (error instanceof PermissionException) {
channel.sendMessage(String.format("%sError banning %s: (I need the permission %s)", EmoteReference.ERROR, user.getName(), ((PermissionException) error).getPermission())).queue();
} else {
channel.sendMessage(String.format("%sI encountered an unknown error while banning %s", EmoteReference.ERROR, user.getName())).queue();
log.warn("Encountered an unexpected error while trying to ban someone.", error);
}
}));
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Tempban Command").setDescription("**Temporarily bans an user**").addField("Usage", "`~>tempban <user> <reason> time:<time>`", false).addField("Example", "`~>tempban @Kodehawa example time:1d`", false).addField("Extended usage", "`time` - can be used with the following parameters: " + "d (days), s (second), m (minutes), h (hour). **For example time:1d1h will give a day and an hour.**", false).build();
}
});
}
use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.
the class ModerationCmds method softban.
@Subscribe
public void softban(CommandRegistry cr) {
cr.register("softban", new SimpleCommand(Category.MODERATION) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
Guild guild = event.getGuild();
User author = event.getAuthor();
TextChannel channel = event.getChannel();
Message receivedMessage = event.getMessage();
String reason = content;
if (!guild.getMember(author).hasPermission(Permission.BAN_MEMBERS)) {
channel.sendMessage(EmoteReference.ERROR2 + "Cannot soft ban: You don't have the Ban Members permission.").queue();
return;
}
if (receivedMessage.getMentionedUsers().isEmpty()) {
channel.sendMessage(EmoteReference.ERROR + "You must mention 1 or more users to be soft-banned!").queue();
return;
}
Member selfMember = guild.getSelfMember();
if (!selfMember.hasPermission(Permission.BAN_MEMBERS)) {
channel.sendMessage(EmoteReference.ERROR2 + "Sorry! I don't have permission to ban members in this server!").queue();
return;
}
for (User user : event.getMessage().getMentionedUsers()) {
reason = reason.replaceAll("(\\s+)?<@!?" + user.getId() + ">(\\s+)?", "");
}
if (reason.isEmpty()) {
reason = "Reason not specified";
}
final String finalReason = String.format("Softbanned by %#s: %s", event.getAuthor(), reason);
receivedMessage.getMentionedUsers().forEach(user -> {
if (!event.getGuild().getMember(event.getAuthor()).canInteract(event.getGuild().getMember(user))) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot softban an user in a higher hierarchy than you").queue();
return;
}
if (event.getAuthor().getId().equals(user.getId())) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Why are you trying to soft-ban yourself?").queue();
return;
}
Member member = guild.getMember(user);
if (member == null)
return;
// If one of them is in a higher hierarchy than the bot, cannot ban.
if (!selfMember.canInteract(member)) {
channel.sendMessage(EmoteReference.ERROR2 + "Cannot softban member: " + member.getEffectiveName() + ", they are " + "higher or the same " + "hierarchy than I am!").queue();
return;
}
final DBGuild db = MantaroData.db().getGuild(event.getGuild());
// Proceed to ban them. Again, using queue so I don't get rate limited.
guild.getController().ban(member, 7).reason(finalReason).queue(success -> user.openPrivateChannel().queue(privateChannel -> {
if (!user.isBot()) {
privateChannel.sendMessage(String.format("%sYou were **softbanned** by %s#%s for reason %s on server **%s**.", EmoteReference.MEGA, event.getAuthor().getName(), event.getAuthor().getDiscriminator(), finalReason, event.getGuild().getName())).queue();
}
db.getData().setCases(db.getData().getCases() + 1);
db.saveAsync();
channel.sendMessage(String.format("%s%s. **(%s got softbanned)**", EmoteReference.ZAP, modActionQuotes[r.nextInt(modActionQuotes.length)], member.getEffectiveName())).queue();
guild.getController().unban(member.getUser()).reason(finalReason).queue(aVoid -> {
}, error -> {
if (error instanceof PermissionException) {
channel.sendMessage(String.format(EmoteReference.ERROR + "Error unbanning [%s]: (No permission provided: %s)", member.getEffectiveName(), ((PermissionException) error).getPermission())).queue();
} else {
channel.sendMessage(String.format(EmoteReference.ERROR + "Unknown error while unbanning [%s]: <%s>: %s", member.getEffectiveName(), error.getClass().getSimpleName(), error.getMessage())).queue();
log.warn("Unexpected error while unbanning someone.", error);
}
});
ModLog.log(event.getMember(), user, finalReason, ModLog.ModAction.KICK, db.getData().getCases());
TextChannelGround.of(event).dropItemWithChance(2, 2);
}), error -> {
if (error instanceof PermissionException) {
channel.sendMessage(String.format(EmoteReference.ERROR + "Error softbanning %s: (No permission provided: %s)", member.getEffectiveName(), ((PermissionException) error).getPermission())).queue();
} else {
channel.sendMessage(String.format(EmoteReference.ERROR + "Unknown error while softbanning %s", member.getEffectiveName())).queue();
log.warn("Unexpected error while soft banning someone.", error);
}
});
});
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Softban").setDescription("**Softban the mentioned user and clears their messages from the past week. (You need Ban " + "Members)**").addField("Summarizing", "A soft ban is a ban & instant unban, normally used to clear " + "the user's messages but **without banning the person permanently**.", false).build();
}
});
}
use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.
the class ModerationCmds method ban.
@Subscribe
public void ban(CommandRegistry cr) {
cr.register("ban", new SimpleCommand(Category.MODERATION) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
Guild guild = event.getGuild();
User author = event.getAuthor();
TextChannel channel = event.getChannel();
Message receivedMessage = event.getMessage();
String reason = content;
if (!guild.getMember(author).hasPermission(net.dv8tion.jda.core.Permission.BAN_MEMBERS)) {
channel.sendMessage(EmoteReference.ERROR + "You can't ban: You need the `Ban Users` permission.").queue();
return;
}
if (receivedMessage.getMentionedUsers().isEmpty()) {
channel.sendMessage(EmoteReference.ERROR + "You need to mention at least one user!").queue();
return;
}
for (User user : event.getMessage().getMentionedUsers()) {
reason = reason.replaceAll("(\\s+)?<@!?" + user.getId() + ">(\\s+)?", "");
}
if (reason.isEmpty()) {
reason = "Reason not specified";
}
final String finalReason = String.format("Banned by %#s: %s", event.getAuthor(), reason);
receivedMessage.getMentionedUsers().forEach(user -> {
if (!event.getGuild().getMember(event.getAuthor()).canInteract(event.getGuild().getMember(user))) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You cannot ban an user who's higher than you in the " + "server hierarchy! Nice try " + EmoteReference.SMILE).queue();
return;
}
if (event.getAuthor().getId().equals(user.getId())) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Why are you trying to ban yourself, silly?").queue();
return;
}
Member member = guild.getMember(user);
if (member == null)
return;
if (!guild.getSelfMember().canInteract(member)) {
channel.sendMessage(String.format("%sI can't ban %s; they're higher in the server hierarchy than me!", EmoteReference.ERROR, member.getEffectiveName())).queue();
return;
}
if (!guild.getSelfMember().hasPermission(net.dv8tion.jda.core.Permission.BAN_MEMBERS)) {
channel.sendMessage(EmoteReference.ERROR + "Sorry! I don't have permission to ban members in this server!").queue();
return;
}
final DBGuild db = MantaroData.db().getGuild(event.getGuild());
guild.getController().ban(member, 7).reason(finalReason).queue(success -> user.openPrivateChannel().queue(privateChannel -> {
if (!user.isBot()) {
privateChannel.sendMessage(String.format("%sYou were **banned** by %s#%s on server **%s**. Reason: %s.", EmoteReference.MEGA, event.getAuthor().getName(), event.getAuthor().getDiscriminator(), event.getGuild().getName(), finalReason)).queue();
}
db.getData().setCases(db.getData().getCases() + 1);
db.saveAsync();
channel.sendMessage(String.format("%s%s (%s got banned)", EmoteReference.ZAP, modActionQuotes[r.nextInt(modActionQuotes.length)], user.getName())).queue();
ModLog.log(event.getMember(), user, finalReason, ModLog.ModAction.BAN, db.getData().getCases());
TextChannelGround.of(event).dropItemWithChance(1, 2);
}), error -> {
if (error instanceof PermissionException) {
channel.sendMessage(String.format("%sError banning %s: (I need the permission %s)", EmoteReference.ERROR, user.getName(), ((PermissionException) error).getPermission())).queue();
} else {
channel.sendMessage(String.format("%sI encountered an unknown error while banning %s", EmoteReference.ERROR, member.getEffectiveName())).queue();
log.warn("Encountered an unexpected error while trying to ban someone.", error);
}
});
});
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Ban").setDescription("**Bans the mentioned users. (You need Ban Members)**").addField("Usage", "`~>ban <@user> <reason>` - **Bans the specified user**", false).build();
}
});
}
use of net.kodehawa.mantarobot.db.entities.DBGuild in project MantaroBot by Mantaro.
the class MuteCmds method mute.
@Subscribe
public void mute(CommandRegistry registry) {
Command mute = registry.register("mute", new SimpleCommand(Category.MODERATION) {
@Override
protected void call(GuildMessageReceivedEvent event, String content, String[] args) {
if (!event.getMember().hasPermission(Permission.KICK_MEMBERS) || !event.getMember().hasPermission(Permission.BAN_MEMBERS)) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You need to have either ban or kick members permission to mute!").queue();
return;
}
ManagedDatabase db = MantaroData.db();
DBGuild dbGuild = db.getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
String reason = "Not specified";
Map<String, Optional<String>> opts = br.com.brjdevs.java.utils.texts.StringUtils.parse(args);
if (guildData.getMutedRole() == null) {
event.getChannel().sendMessage(EmoteReference.ERROR + "The mute role is not set in this server, you can set it by doing `~>opts muterole set <role>`").queue();
return;
}
Role mutedRole = event.getGuild().getRoleById(guildData.getMutedRole());
if (mutedRole == null) {
event.getChannel().sendMessage(EmoteReference.ERROR + "The previously configured mute role on this server is now non-existent!").queue();
return;
}
if (args.length > 1) {
reason = StringUtils.splitArgs(content, 2)[1];
}
if (event.getMessage().getMentionedUsers().isEmpty()) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You need to mention at least one user to mute.").queue();
return;
}
if (!event.getGuild().getSelfMember().hasPermission(event.getChannel(), Permission.MANAGE_ROLES)) {
event.getChannel().sendMessage(EmoteReference.ERROR + "I don't have permissions to administrate roles on this server!").queue();
return;
}
// Regex from: Fabricio20
final String finalReason = timePattern.matcher(reason).replaceAll("");
MantaroObj data = db.getMantaroData();
event.getMessage().getMentionedUsers().forEach(user -> {
Member m = event.getGuild().getMember(user);
long time = guildData.getSetModTimeout() > 0 ? System.currentTimeMillis() + guildData.getSetModTimeout() : 0L;
if (opts.containsKey("time")) {
if (!opts.get("time").isPresent() || opts.get("time").get().isEmpty()) {
event.getChannel().sendMessage(EmoteReference.WARNING + "You wanted time but didn't specify for how long!").queue();
return;
}
time = System.currentTimeMillis() + Utils.parseTime(opts.get("time").get());
if (time > System.currentTimeMillis() + TimeUnit.DAYS.toMillis(10)) {
// smh smh smfh god fuck rethinkdb just
// dont
event.getChannel().sendMessage(EmoteReference.ERROR + "Too long...").queue();
// smh
return;
}
if (time < 0) {
event.getChannel().sendMessage("You cannot mute someone for negative time!").queue();
return;
}
data.getMutes().put(user.getIdLong(), Pair.of(event.getGuild().getId(), time));
data.save();
dbGuild.save();
} else {
if (time > 0) {
if (time > System.currentTimeMillis() + TimeUnit.DAYS.toMillis(10)) {
event.getChannel().sendMessage(EmoteReference.ERROR + "The default mute timeout length is too long (Maximum: 10 days)...").queue();
return;
}
data.getMutes().put(user.getIdLong(), Pair.of(event.getGuild().getId(), time));
data.save();
dbGuild.save();
} else {
event.getChannel().sendMessage(EmoteReference.ERROR + "You didn't specify any time!").queue();
return;
}
}
if (m.getRoles().contains(mutedRole)) {
event.getChannel().sendMessage(EmoteReference.WARNING + "This user already has a mute role assigned. Please do `~>unmute` to unmute them.").queue();
return;
}
if (!event.getGuild().getSelfMember().canInteract(m)) {
event.getChannel().sendMessage(EmoteReference.ERROR + "I cannot assign the mute role to this user because they're in a higher hierarchy than me, or the role is in a higher hierarchy!").queue();
return;
}
if (!event.getMember().canInteract(m)) {
event.getChannel().sendMessage(EmoteReference.ERROR + "I cannot assign the mute role to this user because they're in a higher hierarchy than me, or the role is in a higher hierarchy than you!").queue();
return;
}
final DBGuild dbg = db.getGuild(event.getGuild());
event.getGuild().getController().addSingleRoleToMember(m, mutedRole).reason(String.format("Muted by %#s for %s: %s", event.getAuthor(), Utils.formatDuration(time - System.currentTimeMillis()), finalReason)).queue();
event.getChannel().sendMessage(EmoteReference.CORRECT + "Added mute role to **" + m.getEffectiveName() + (time > 0 ? "** for around " + Utils.getHumanizedTime(time - System.currentTimeMillis()) : "**")).queue();
dbg.getData().setCases(dbg.getData().getCases() + 1);
dbg.saveAsync();
ModLog.log(event.getMember(), user, finalReason, ModLog.ModAction.MUTE, dbg.getData().getCases());
});
}
@Override
public MessageEmbed help(GuildMessageReceivedEvent event) {
return helpEmbed(event, "Mute").setDescription("**Mutes the specified users**").addField("Usage", "`~>mute <user> <reason> [-time <time>]` - Mutes the specified users.", false).addField("Parameters", "`users` - The users to mute. Needs to be mentions.\n" + "`[-time <time>]` - The time to mute an user for. For example `~>mute @Natan#1289 wew, nice -time 1m20s` will mute Natan for 1 minute and 20 seconds.", false).addField("Considerations", "To unmute an user, do `~>unmute`.", false).addField("Extended usage", "`time` - can be used with the following parameters: " + "d (days), s (second), m (minutes), h (hour). **For example -time 1d1h will mute for one day and one hour.**", false).build();
}
});
mute.addOption("defaultmutetimeout:set", new Option("Default mute timeout", "Sets the default mute timeout for ~>mute.\n" + "This command will set the timeout of ~>mute to a fixed value **unless you specify another time in the command**\n" + "**Example:** `~>opts defaultmutetimeout set 1m20s`\n" + "**Considerations:** Time is in 1m20s or 1h10m3s format, for example.", OptionType.GUILD).setAction(((event, args) -> {
if (args.length == 0) {
event.getChannel().sendMessage(EmoteReference.ERROR + "You have to specify a timeout in the format of 1m20s, for example.").queue();
return;
}
if (!(args[0]).matches("(?:(\\d+)h)?(?:(\\d+)m)?(?:(\\d+)s)?")) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Wrong time format. You have to specify a timeout in the format of 1m20s, for example.").queue();
return;
}
long timeoutToSet = Utils.parseTime(args[0]);
long time = System.currentTimeMillis() + timeoutToSet;
if (time > System.currentTimeMillis() + TimeUnit.DAYS.toMillis(10)) {
event.getChannel().sendMessage(EmoteReference.ERROR + "Too long...").queue();
return;
}
if (time < 0) {
event.getChannel().sendMessage("You cannot mute someone for negative time!").queue();
return;
}
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
guildData.setSetModTimeout(timeoutToSet);
dbGuild.save();
event.getChannel().sendMessage(EmoteReference.CORRECT + "Successfully set mod action timeout to `" + args[0] + "` (" + timeoutToSet + "ms)").queue();
})).setShortDescription("Sets the default timeout for the ~>mute command"));
mute.addOption("defaultmutetimeout:reset", new Option("Default mute timeout reset", "Resets the default mute timeout which was set previously with `defaultmusictimeout set`", OptionType.GUILD).setAction((event -> {
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
guildData.setSetModTimeout(0L);
dbGuild.save();
event.getChannel().sendMessage(EmoteReference.CORRECT + "Successfully reset timeout.").queue();
})).setShortDescription("Resets the default mute timeout."));
mute.addOption("muterole:set", new Option("Mute role set", "Sets this guilds mute role to apply on the ~>mute command.\n" + "To use this command you need to specify a role name. *In case the name contains spaces, the name should" + " be wrapped in quotation marks", OptionType.COMMAND).setAction((event, args) -> {
if (args.length < 1) {
OptsCmd.onHelp(event);
return;
}
String roleName = String.join(" ", args);
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
List<Role> roleList = event.getGuild().getRolesByName(roleName, true);
if (roleList.size() == 0) {
event.getChannel().sendMessage(EmoteReference.ERROR + "I didn't find a role with that name!").queue();
} else if (roleList.size() == 1) {
Role role = roleList.get(0);
guildData.setMutedRole(role.getId());
dbGuild.saveAsync();
event.getChannel().sendMessage(EmoteReference.OK + "Set mute role to **" + roleName + "**").queue();
} else {
DiscordUtils.selectList(event, roleList, role -> String.format("%s (ID: %s) | Position: %s", role.getName(), role.getId(), role.getPosition()), s -> OptsCmd.getOpts().baseEmbed(event, "Select the Mute Role:").setDescription(s).build(), role -> {
guildData.setMutedRole(role.getId());
dbGuild.saveAsync();
event.getChannel().sendMessage(EmoteReference.OK + "Set mute role to **" + roleName + "**").queue();
});
}
}).setShortDescription("Sets this guilds mute role to apply on the ~>mute command"));
mute.addOption("muterole:unbind", new Option("Mute Role unbind", "Resets the current value set for the mute role", OptionType.GENERAL).setAction(event -> {
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
guildData.setMutedRole(null);
dbGuild.saveAsync();
event.getChannel().sendMessage(EmoteReference.OK + "Correctly reset the mute role.").queue();
}).setShortDescription("Resets the current value set for the mute role."));
}
Aggregations