use of toby.command.CommandContext in project toby-bot by ml404.
the class SetVolumeCommand method setNewVolume.
private void setNewVolume(CommandContext ctx, String prefix, TextChannel channel, Member member, Integer deleteDelay) {
Guild guild = ctx.getGuild();
boolean validVolumeArg = ctx.getArgs().get(0).matches("\\d+");
if (validVolumeArg) {
int volume = Integer.parseInt(ctx.getArgs().get(0));
if (PlayerManager.getInstance().isCurrentlyStoppable() || member.hasPermission(Permission.KICK_MEMBERS)) {
AudioPlayer audioPlayer = PlayerManager.getInstance().getMusicManager(guild).getAudioPlayer();
if (volume < 1 || volume > 100) {
channel.sendMessage(getHelp(prefix)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
return;
}
int oldVolume = audioPlayer.getVolume();
if (volume == oldVolume) {
channel.sendMessageFormat("New volume and old volume are the same value, somebody shoot %s", member.getEffectiveName()).queue(message -> ICommand.deleteAfter(message, deleteDelay));
return;
}
audioPlayer.setVolume(volume);
channel.sendMessageFormat("Changing volume from '%s' to '%s' \uD83D\uDD0A", oldVolume, volume).queue(message -> ICommand.deleteAfter(message, deleteDelay));
} else {
channel.sendMessageFormat("You aren't allowed to change the volume kid %s", Emotes.TOBY).queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
} else
channel.sendMessage(getHelp(prefix)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
use of toby.command.CommandContext in project toby-bot by ml404.
the class IntroSongCommand method setIntroViaDiscordAttachment.
private void setIntroViaDiscordAttachment(CommandContext ctx, UserDto requestingUserDto, Integer deleteDelay, TextChannel channel, List<Message.Attachment> attachments, int introVolume) {
Message.Attachment attachment = attachments.stream().findFirst().get();
if (!Objects.equals(attachment.getFileExtension(), "mp3")) {
channel.sendMessage("Please use mp3 files only").queue(message -> ICommand.deleteAfter(message, deleteDelay));
return;
} else if (attachment.getSize() > 300000) {
channel.sendMessage("Please keep the file size under 300kb").queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
List<Member> mentionedMembers = ctx.getMessage().getMentionedMembers();
InputStream inputStream = null;
try {
inputStream = attachment.retrieveInputStream().get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
if (mentionedMembers.size() > 0 && requestingUserDto.isSuperUser()) {
InputStream finalInputStream = inputStream;
mentionedMembers.forEach(member -> {
UserDto userDto = calculateUserDto(member.getGuild().getIdLong(), member.getIdLong(), member.isOwner(), userService, introVolume);
persistMusicFile(userDto, deleteDelay, channel, attachment.getFileName(), introVolume, finalInputStream, member.getEffectiveName());
});
} else
persistMusicFile(requestingUserDto, deleteDelay, channel, attachment.getFileName(), introVolume, inputStream, ctx.getAuthor().getName());
}
use of toby.command.CommandContext in project toby-bot by ml404.
the class KickCommand method handle.
@Override
public void handle(CommandContext ctx, String prefix, UserDto requestingUserDto, Integer deleteDelay) {
ICommand.deleteAfter(ctx.getMessage(), deleteDelay);
final TextChannel channel = ctx.getChannel();
final Message message = ctx.getMessage();
final Member member = ctx.getMember();
final List<String> args = ctx.getArgs();
if (message.getMentionedMembers().isEmpty()) {
channel.sendMessage("You must mention 1 or more Users to shoot").queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
return;
}
message.getMentionedMembers().forEach(target -> {
if (!member.canInteract(target) || !member.hasPermission(Permission.KICK_MEMBERS)) {
channel.sendMessage(String.format("You can't shoot %s", target)).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
return;
}
final Member botMember = ctx.getSelfMember();
if (!botMember.canInteract(target) || !botMember.hasPermission(Permission.KICK_MEMBERS)) {
channel.sendMessage(String.format("I'm not allowed to shoot %s", target)).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
return;
}
ctx.getGuild().kick(target).reason("because you told me to.").queue((__) -> channel.sendMessage("Shot hit the mark... something about fortnite?").queue(message1 -> ICommand.deleteAfter(message1, deleteDelay)), (error) -> channel.sendMessageFormat("Could not shoot %s", error.getMessage()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay)));
});
}
use of toby.command.CommandContext in project toby-bot by ml404.
the class SocialCreditCommand method handle.
@Override
public void handle(CommandContext ctx, String prefix, UserDto requestingUserDto, Integer deleteDelay) {
ICommand.deleteAfter(ctx.getMessage(), deleteDelay);
List<String> args = ctx.getArgs();
TextChannel channel = ctx.getChannel();
Message message = ctx.getMessage();
final Member member = ctx.getMember();
if (!ctx.getGuild().isLoaded())
ctx.getGuild().loadMembers();
if (args.contains("leaderboard")) {
Map<Long, Long> discordSocialCreditMap = new HashMap<>();
userService.listGuildUsers(ctx.getGuild().getIdLong()).forEach(userDto -> {
Long socialCredit = userDto.getSocialCredit() == null ? 0L : userDto.getSocialCredit();
discordSocialCreditMap.put(userDto.getDiscordId(), socialCredit);
});
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("**Social Credit Leaderboard**\n");
stringBuilder.append("**-----------------------------**\n");
LinkedHashMap<Long, Long> sortedMap = discordSocialCreditMap.entrySet().stream().sorted(comparingByValue(Comparator.reverseOrder())).collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
AtomicInteger position = new AtomicInteger();
sortedMap.forEach((k, v) -> {
position.getAndIncrement();
ctx.getGuild().getMembers().stream().filter(member1 -> member1.getIdLong() == k).findFirst().ifPresent(memberById -> stringBuilder.append(String.format("#%s: %s - score: %d\n", position, memberById.getEffectiveName(), v)));
});
channel.sendMessageFormat(stringBuilder.toString()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
} else {
List<Member> mentionedMembers = message.getMentionedMembers();
if (mentionedMembers.isEmpty()) {
listSocialCreditScore(requestingUserDto, member.getEffectiveName(), deleteDelay, channel);
} else
mentionedMembers.forEach(targetMember -> {
UserDto targetUserDto = userService.getUserById(targetMember.getIdLong(), targetMember.getGuild().getIdLong());
if (args.subList(0, args.size()).stream().allMatch(s -> s.matches(Message.MentionType.USER.getPattern().pattern()))) {
listSocialCreditScore(targetUserDto, targetMember.getEffectiveName(), deleteDelay, channel);
} else {
// Check to see if the database contained an entry for the user we have made a request against
if (targetUserDto != null) {
boolean isSameGuild = requestingUserDto.getGuildId().equals(targetUserDto.getGuildId());
boolean requesterCanAdjustPermissions = member.isOwner();
if (requesterCanAdjustPermissions && isSameGuild) {
String socialCreditString = args.subList(0, args.size()).stream().filter(s -> !s.matches(Message.MentionType.USER.getPattern().pattern())).findFirst().get();
UserDto updatedUser = validateArgumentsAndAdjustSocialCredit(ctx, targetUserDto, channel, Long.valueOf(socialCreditString), ctx.getMember().isOwner(), deleteDelay);
channel.sendMessageFormat("Updated user %s's social credit by %s. New score is: %d", targetMember.getEffectiveName(), socialCreditString, updatedUser.getSocialCredit()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
} else
channel.sendMessageFormat("User '%s' is not allowed to adjust the social credit of user '%s'.", member.getNickname(), targetMember.getNickname()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
}
}
});
}
}
use of toby.command.CommandContext in project toby-bot by ml404.
the class DbdRandomKillerCommand method handle.
@Override
@SuppressWarnings("unchecked")
public void handle(CommandContext ctx, String prefix, UserDto requestingUserDto, Integer deleteDelay) {
ICommand.deleteAfter(ctx.getMessage(), deleteDelay);
final TextChannel channel = ctx.getChannel();
try {
WikiFetcher wikiFetcher = new WikiFetcher(cache);
List<String> dbdKillers = wikiFetcher.fetchFromWiki(cacheName, dbdWebUrl, className, cssQuery);
channel.sendMessage(RandomCommand.getRandomElement(dbdKillers)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
} catch (IOException ignored) {
channel.sendMessage("Huh, the website I pull data from must have returned something unexpected.").queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
}
Aggregations