use of net.kodehawa.mantarobot.db.entities.helpers.GuildData 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."));
}
use of net.kodehawa.mantarobot.db.entities.helpers.GuildData in project MantaroBot by Mantaro.
the class UtilsCmds method dateGMT.
protected static String dateGMT(Guild guild, String tz) {
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date date = new Date();
DBGuild dbGuild = MantaroData.db().getGuild(guild.getId());
GuildData guildData = dbGuild.getData();
if (guildData.getTimeDisplay() == 1) {
format = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");
}
format.setTimeZone(TimeZone.getTimeZone(tz));
return format.format(date);
}
use of net.kodehawa.mantarobot.db.entities.helpers.GuildData in project MantaroBot by Mantaro.
the class Poll method startPoll.
public void startPoll() {
try {
if (!isCompliant) {
getChannel().sendMessage(EmoteReference.WARNING + "This poll cannot build. " + "**Remember that the options must be a maximum of 9 and a minimum of 2 and the timeout must be a maximum of 45m and a minimum of 30s.**\n" + "Options are separated with a comma, for example `1,2,3`. For spaced stuff use quotation marks at the start and end of the sentence.").queue();
getRunningPolls().remove(getChannel().getId());
return;
}
if (isPollAlreadyRunning(getChannel())) {
getChannel().sendMessage(EmoteReference.WARNING + "There seems to be another poll running here...").queue();
return;
}
if (!getGuild().getSelfMember().hasPermission(getChannel(), Permission.MESSAGE_ADD_REACTION)) {
getChannel().sendMessage(EmoteReference.ERROR + "Seems like I cannot add reactions here...").queue();
getRunningPolls().remove(getChannel().getId());
return;
}
DBGuild dbGuild = MantaroData.db().getGuild(getGuild());
GuildData data = dbGuild.getData();
AtomicInteger at = new AtomicInteger();
data.setRanPolls(data.getRanPolls() + 1L);
dbGuild.saveAsync();
String toShow = Stream.of(options).map(opt -> String.format("#%01d.- %s", at.incrementAndGet(), opt)).collect(Collectors.joining("\n"));
if (toShow.length() > 1014) {
toShow = "This was too long to show, so I pasted it: " + Utils.paste(toShow);
}
User author = MantaroBot.getInstance().getUserById(owner);
EmbedBuilder builder = new EmbedBuilder().setAuthor(String.format("Poll #%1d created by %s", data.getRanPolls(), author.getName()), null, author.getAvatarUrl()).setDescription("**Poll started. React to the number to vote.**\n*" + name + "*\n" + "Type &cancelpoll to cancel a running poll.").addField("Options", "```md\n" + toShow + "```", false).setColor(Color.CYAN).setThumbnail("https://cdn.pixabay.com/photo/2012/04/14/16/26/question-34499_960_720.png").setFooter("You have " + Utils.getHumanizedTime(timeout) + " to vote.", author.getAvatarUrl());
getChannel().sendMessage(builder.build()).queue(this::createPoll);
InteractiveOperations.createOverriding(getChannel(), timeout, e -> {
if (e.getAuthor().getId().equals(owner)) {
if (e.getMessage().getContentRaw().equalsIgnoreCase("&cancelpoll")) {
runningPoll.cancel(true);
return Operation.COMPLETED;
}
}
return Operation.IGNORED;
});
runningPolls.put(getChannel().getId(), this);
} catch (Exception e) {
getChannel().sendMessage(EmoteReference.ERROR + "An unknown error has occurred while setting up a poll. Maybe try again?").queue();
}
}
use of net.kodehawa.mantarobot.db.entities.helpers.GuildData in project MantaroBot by Mantaro.
the class AudioLoader method playlistLoaded.
@Override
public void playlistLoaded(AudioPlaylist playlist) {
if (playlist.isSearchResult()) {
if (!skipSelection)
onSearch(playlist);
else
loadSingle(playlist.getTracks().get(0), false);
return;
}
try {
int i = 0;
for (AudioTrack track : playlist.getTracks()) {
DBGuild dbGuild = MantaroData.db().getGuild(event.getGuild());
GuildData guildData = dbGuild.getData();
if (guildData.getMusicQueueSizeLimit() != null) {
if (i < guildData.getMusicQueueSizeLimit()) {
loadSingle(track, true);
} else {
event.getChannel().sendMessage(String.format(":warning: The queue you added had more than %d songs, so we added songs until this limit and ignored the rest.", guildData.getMusicQueueSizeLimit())).queue();
break;
}
} else {
if (i > MAX_QUEUE_LENGTH && !dbGuild.isPremium()) {
event.getChannel().sendMessage(":warning: The queue you added had more than " + MAX_QUEUE_LENGTH + " songs, so we added songs until this limit and ignored the rest.").queue();
// stop adding songs
break;
} else {
loadSingle(track, true);
}
}
i++;
}
event.getChannel().sendMessage(String.format("%sAdded **%d songs** to queue on playlist: **%s** *(%s)*", EmoteReference.CORRECT, i, playlist.getName(), Utils.getDurationMinutes(playlist.getTracks().stream().mapToLong(temp -> temp.getInfo().length).sum()))).queue();
} catch (Exception e) {
SentryHelper.captureExceptionContext("Cannot load playlist. I guess something broke pretty hard. Please check", e, this.getClass(), "Music Loader");
}
}
use of net.kodehawa.mantarobot.db.entities.helpers.GuildData in project MantaroBot by Mantaro.
the class MantaroListener method onUserLeave.
private void onUserLeave(GuildMemberLeaveEvent event) {
DBGuild dbg = MantaroData.db().getGuild(event.getGuild());
GuildData data = dbg.getData();
try {
String hour = df.format(new Date(System.currentTimeMillis()));
if (event.getMember().getUser().isBot() && data.isIgnoreBotsWelcomeMessage()) {
return;
}
String logChannel = MantaroData.db().getGuild(event.getGuild()).getData().getGuildLogChannel();
if (logChannel != null) {
TextChannel tc = event.getGuild().getTextChannelById(logChannel);
if (tc != null && tc.canTalk()) {
tc.sendMessage(String.format("`[%s]` \uD83D\uDCE3 `%s#%s` just left `%s` `(User #%d)`", hour, event.getMember().getEffectiveName(), event.getMember().getUser().getDiscriminator(), event.getGuild().getName(), event.getGuild().getMembers().size())).queue();
}
logTotal++;
}
} catch (Exception e) {
SentryHelper.captureExceptionContext("Failed to process leave message!", e, MantaroListener.class, "Join Handler");
}
try {
String leaveChannel = data.getLogJoinLeaveChannel() == null ? data.getLogLeaveChannel() : data.getLogJoinLeaveChannel();
String leaveMessage = data.getLeaveMessage();
sendJoinLeaveMessage(event, leaveMessage, leaveChannel);
MantaroBot.getInstance().getStatsClient().increment("leave_messages");
} catch (Exception e) {
SentryHelper.captureExceptionContext("Failed to send leave message!", e, MantaroListener.class, "Join Handler");
}
}
Aggregations