use of me.duncte123.durationparser.ParsedDuration in project SkyBot by duncte123.
the class RemindmeCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
final List<String> args = ctx.getArgs();
if (args.size() < 2) {
this.sendUsageInstructions(ctx);
return;
}
final var flags = ctx.getParsedFlags(this);
if (flags.get("undefined").isEmpty()) {
sendMsg(ctx, "What do you want me to remind you of?\nUsage: " + this.getUsageInstructions(ctx));
return;
}
if (!flags.containsKey("t")) {
sendMsg(ctx, "It looks like you forgot to add the time, you can do this with the `--time` flag");
return;
}
final Optional<ParsedDuration> optionalDuration = getDuration(flags);
if (optionalDuration.isEmpty()) {
sendMsg(ctx, "Incorrect duration format");
return;
}
final ParsedDuration duration = optionalDuration.get();
if (duration.getMilis() == 0) {
sendMsg(ctx, "Your specified time is too short or the time syntax is invalid.");
return;
}
if (duration.getSeconds() < 30) {
sendMsg(ctx, "Minimum duration is 30 seconds");
return;
}
if (duration.getDays() > 1460) {
// 4 years in days
sendMsg(ctx, "Just keep it below 4 years ok ;)");
return;
}
final String reminder = String.join(" ", flags.get("undefined"));
if (reminder.length() > 255) {
sendMsg(ctx, "The maximum message length is 255 characters");
return;
}
final OffsetDateTime expireDate = AirUtils.getDatabaseDate(duration);
createReminder(ctx, expireDate, reminder, flags, duration);
}
use of me.duncte123.durationparser.ParsedDuration in project SkyBot by duncte123.
the class TempBanCommand method getDuration.
@Nullable
public static ParsedDuration getDuration(String arg, String name, CommandContext ctx, String prefix) {
Optional<ParsedDuration> optionalDuration;
try {
optionalDuration = DurationParser.parse(arg);
} catch (IllegalArgumentException ignored) {
optionalDuration = Optional.empty();
}
if (optionalDuration.isEmpty()) {
sendMsg(ctx, "Usage is `" + prefix + name + " <@user> <time><w/d/h/m/s> [Reason]`");
return null;
}
final ParsedDuration duration = optionalDuration.get();
if (duration.getMilis() == 0) {
sendMsg(ctx, "Your specified time is too short or the time syntax is invalid.");
return null;
}
if (duration.getSeconds() < 30) {
sendMsg(ctx, "Minimum duration is 30 seconds");
return null;
}
return duration;
}
use of me.duncte123.durationparser.ParsedDuration in project SkyBot by duncte123.
the class TempBanCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
final List<String> args = ctx.getArgs();
final List<Member> mentioned = ctx.getMentionedArg(0);
if (mentioned.isEmpty()) {
sendMsg(ctx, "I could not find any members with name " + args.get(0));
return;
}
final Member toBanMember = mentioned.get(0);
if (toBanMember.equals(ctx.getMember())) {
sendMsg(ctx, "You cannot ban yourself");
return;
}
if (!canInteract(ctx.getMember(), toBanMember, "ban", ctx.getChannel())) {
return;
}
final ParsedDuration duration = getDuration(args.get(1), getName(), ctx, ctx.getPrefix());
if (duration == null) {
return;
}
String reason = "No reason given";
final var flags = ctx.getParsedFlags(this);
if (flags.containsKey("r")) {
reason = String.join(" ", flags.get("r"));
}
final String finalUnbanDate = AirUtils.getDatabaseDateFormat(duration);
final String fReason = reason;
final User toBan = toBanMember.getUser();
ctx.getGuild().ban(toBan.getId(), 1, String.format("%#s: %s", ctx.getAuthor(), fReason)).queue((ignored) -> {
if (duration.getSeconds() > 0) {
ctx.getDatabaseAdapter().createBan(ctx.getAuthor().getIdLong(), toBan.getName(), toBan.getDiscriminator(), toBan.getIdLong(), finalUnbanDate, ctx.getGuild().getIdLong());
modLog(ctx.getAuthor(), toBan, "banned", fReason, duration.toString(), ctx.getGuild());
} else {
LOGGER.error("Perm ban code in temp ban ran {}", args);
modLog(ctx.getAuthor(), toBan, "banned", fReason, null, ctx.getGuild());
}
});
sendSuccess(ctx.getMessage());
}
use of me.duncte123.durationparser.ParsedDuration in project SkyBot by DuncteBot.
the class TempBanCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
final List<String> args = ctx.getArgs();
final List<Member> mentioned = ctx.getMentionedArg(0);
if (mentioned.isEmpty()) {
sendMsg(ctx, "I could not find any members with name " + args.get(0));
return;
}
final Member toBanMember = mentioned.get(0);
if (toBanMember.equals(ctx.getMember())) {
sendMsg(ctx, "You cannot ban yourself");
return;
}
if (!canInteract(ctx.getMember(), toBanMember, "ban", ctx.getChannel())) {
return;
}
final ParsedDuration duration = getDuration(args.get(1), getName(), ctx, ctx.getPrefix());
if (duration == null) {
return;
}
String reason = "No reason given";
final var flags = ctx.getParsedFlags(this);
if (flags.containsKey("r")) {
reason = String.join(" ", flags.get("r"));
} else if (args.size() > 2) {
final var example = "\nExample: `%stempban %s %s -r %s`".formatted(ctx.getPrefix(), args.get(0), args.get(1), String.join(" ", args.subList(2, args.size())));
sendMsg(ctx, "Hint: if you want to set a reason, use the `-r` flag" + example);
}
final String finalUnbanDate = AirUtils.getDatabaseDateFormat(duration);
final String fReason = reason;
final User toBan = toBanMember.getUser();
ctx.getGuild().ban(toBan.getId(), 1, String.format("%#s: %s", ctx.getAuthor(), fReason)).queue((ignored) -> {
if (duration.getSeconds() > 0) {
ctx.getDatabaseAdapter().createBan(ctx.getAuthor().getIdLong(), toBan.getName(), toBan.getDiscriminator(), toBan.getIdLong(), finalUnbanDate, ctx.getGuild().getIdLong());
modLog(ctx.getAuthor(), toBan, "banned", fReason, duration.toString(), ctx.getGuild());
} else {
LOGGER.error("Perm ban code in temp ban ran {}", args);
modLog(ctx.getAuthor(), toBan, "banned", fReason, null, ctx.getGuild());
}
});
sendSuccess(ctx.getMessage());
}
use of me.duncte123.durationparser.ParsedDuration in project SkyBot by DuncteBot.
the class TempMuteCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
final List<Member> mentioned = ctx.getMentionedArg(0);
final List<String> args = ctx.getArgs();
if (mentioned.isEmpty()) {
sendMsg(ctx, "I could not find any members with name " + args.get(0));
return;
}
final GuildSetting settings = ctx.getGuildSettings();
if (settings.getMuteRoleId() <= 0) {
sendMsg(ctx, "No mute/spamrole is set, use `" + ctx.getPrefix() + "spamrole <Role>` to set it");
return;
}
final Member mod = ctx.getMember();
final Member toMute = mentioned.get(0);
final Guild guild = ctx.getGuild();
final Role role = guild.getRoleById(settings.getMuteRoleId());
final Member self = ctx.getSelfMember();
if (canNotProceed(ctx, mod, toMute, role, self)) {
return;
}
String reason = "No reason given";
final var flags = ctx.getParsedFlags(this);
if (flags.containsKey("r")) {
reason = String.join(" ", flags.get("r"));
} else if (args.size() > 2) {
final var example = "\nExample: `%stempmute %s %s -r %s`".formatted(ctx.getPrefix(), args.get(0), args.get(1), String.join(" ", args.subList(2, args.size())));
sendMsg(ctx, "Hint: if you want to set a reason, use the `-r` flag" + example);
}
final ParsedDuration duration = getDuration(args.get(1), getName(), ctx, ctx.getPrefix());
if (duration == null) {
return;
}
final String fReason = reason;
final String finalDate = AirUtils.getDatabaseDateFormat(duration);
final User mutee = toMute.getUser();
final User author = ctx.getAuthor();
ctx.getDatabaseAdapter().createMute(author.getIdLong(), mutee.getIdLong(), mutee.getAsTag(), finalDate, guild.getIdLong(), (mute) -> {
if (mute != null) {
final String modId = mute.getModId();
final User oldMuteMod = guild.getJDA().getUserById(modId);
String modName = "Unknown";
if (oldMuteMod != null) {
modName = oldMuteMod.getAsTag();
}
sendMsg(ctx, String.format("Previously created muted for %#s removed, mute was created by %s", mutee, modName));
}
return null;
});
guild.addRoleToMember(toMute, role).reason("Muted by " + String.format("%#s: %s", ctx.getAuthor(), fReason)).queue(success -> {
ModerationUtils.modLog(author, mutee, "muted", fReason, duration.toString(), ctx.getGuild());
sendSuccess(ctx.getMessage());
});
}
Aggregations