use of toby.jpa.dto.UserDto in project toby-bot by ml404.
the class Handler method onGuildVoiceJoin.
// Auto joining voice channel when it becomes occupied and an audio connection doesn't already exist on the server, then play the associated user's intro song
@Override
public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {
Guild guild = event.getGuild();
AudioManager audioManager = guild.getAudioManager();
String volumePropertyName = ConfigDto.Configurations.VOLUME.getConfigValue();
ConfigDto databaseVolumeConfig = configService.getConfigByName(volumePropertyName, event.getGuild().getId());
ConfigDto deleteDelayConfig = configService.getConfigByName(ConfigDto.Configurations.DELETE_DELAY.getConfigValue(), event.getGuild().getId());
int defaultVolume = databaseVolumeConfig != null ? Integer.parseInt(databaseVolumeConfig.getValue()) : 100;
List<Member> nonBotConnectedMembers = event.getChannelJoined().getMembers().stream().filter(member -> !member.getUser().isBot()).collect(Collectors.toList());
AudioPlayer audioPlayer = PlayerManager.getInstance().getMusicManager(guild).getAudioPlayer();
if (!nonBotConnectedMembers.isEmpty() && !audioManager.isConnected()) {
audioPlayer.setVolume(defaultVolume);
audioManager.openAudioConnection(event.getChannelJoined());
}
Member member = event.getMember();
long discordId = member.getUser().getIdLong();
long guildId = member.getGuild().getIdLong();
UserDto userDto = userService.getUserById(discordId, guildId);
if (Objects.equals(audioManager.getConnectedChannel(), event.getChannelJoined())) {
playUserIntro(userDto, guild, guild.getDefaultChannel(), Integer.parseInt(deleteDelayConfig.getValue()));
}
}
use of toby.jpa.dto.UserDto 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.jpa.dto.UserDto in project toby-bot by ml404.
the class IntroSongCommand method persistMusicFile.
private void persistMusicFile(UserDto targetDto, Integer deleteDelay, TextChannel channel, String filename, int introVolume, InputStream inputStream, String memberName) {
byte[] fileContents;
try {
fileContents = readInputStreamToByteArray(inputStream);
} catch (ExecutionException | InterruptedException | IOException e) {
channel.sendMessageFormat("Unable to read file '%s'", filename).queue(message -> ICommand.deleteAfter(message, deleteDelay));
return;
}
MusicDto musicFileDto = targetDto.getMusicDto();
if (musicFileDto == null) {
MusicDto musicDto = new MusicDto(targetDto.getDiscordId(), targetDto.getGuildId(), filename, introVolume, fileContents);
musicFileService.createNewMusicFile(musicDto);
targetDto.setMusicDto(musicDto);
userService.updateUser(targetDto);
channel.sendMessageFormat("Successfully set %s's intro song to '%s' with volume '%d'", memberName, musicDto.getFileName(), musicDto.getIntroVolume()).queue(message -> ICommand.deleteAfter(message, deleteDelay));
return;
}
musicFileDto.setFileName(filename);
musicFileDto.setIntroVolume(introVolume);
musicFileDto.setMusicBlob(fileContents);
musicFileService.updateMusicFile(musicFileDto);
channel.sendMessageFormat("Successfully updated %s's intro song to '%s' with volume '%d'", memberName, filename, introVolume).queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
use of toby.jpa.dto.UserDto 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.jpa.dto.UserDto 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));
}
}
});
}
}
Aggregations