use of net.dv8tion.jda.api.entities.GuildVoiceState in project toby-bot by ml404.
the class TalkCommand 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 Member member = ctx.getMember();
final GuildVoiceState memberVoiceState = member.getVoiceState();
final VoiceChannel memberChannel = memberVoiceState.getChannel();
memberChannel.getMembers().forEach(target -> {
if (!member.canInteract(target) || !member.hasPermission(Permission.VOICE_MUTE_OTHERS) || !requestingUserDto.isSuperUser()) {
channel.sendMessage(String.format("You aren't allowed to unmute %s", target)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
return;
}
final Member bot = ctx.getSelfMember();
if (!bot.hasPermission(Permission.VOICE_MUTE_OTHERS)) {
channel.sendMessage(String.format("I'm not allowed to unmute %s", target)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
return;
}
ctx.getGuild().mute(target, false).reason("Unmuted for Among Us.").queue();
});
}
use of net.dv8tion.jda.api.entities.GuildVoiceState in project Bean by Xirado.
the class InteractionCommandHandler method handleUserContextCommand.
public void handleUserContextCommand(@NotNull UserContextInteractionEvent event) {
if (!event.isFromGuild())
return;
Guild guild = event.getGuild();
Member member = event.getMember();
UserContextCommand command = null;
if (registeredGuildCommands.containsKey(guild.getIdLong())) {
List<UserContextCommand> guildCommands = registeredGuildCommands.get(guild.getIdLong()).stream().filter(cmd -> cmd instanceof UserContextCommand).map(cmd -> (UserContextCommand) cmd).toList();
UserContextCommand guildCommand = guildCommands.stream().filter(cmd -> cmd.getData().getName().equalsIgnoreCase(event.getName())).findFirst().orElse(null);
if (guildCommand != null)
command = guildCommand;
}
if (command == null) {
UserContextCommand globalCommand = getRegisteredUserContextCommands().stream().filter(cmd -> cmd.getData().getName().equalsIgnoreCase(event.getName())).findFirst().orElse(null);
if (globalCommand != null)
command = globalCommand;
}
if (command == null)
return;
List<Permission> neededPermissions = command.getRequiredUserPermissions();
List<Permission> neededBotPermissions = command.getRequiredBotPermissions();
if (neededPermissions != null && !member.hasPermission((GuildChannel) event.getChannel(), neededPermissions)) {
event.reply(LocaleLoader.ofGuild(guild).get("general.no_perms", String.class)).queue();
return;
}
if (neededBotPermissions != null && !event.getGuild().getSelfMember().hasPermission((GuildChannel) event.getChannel(), neededBotPermissions)) {
event.reply(LocaleLoader.ofGuild(guild).get("general.no_bot_perms1", String.class)).queue();
return;
}
if (command.getCommandFlags().contains(CommandFlag.MUST_BE_IN_VC)) {
GuildVoiceState guildVoiceState = member.getVoiceState();
if (guildVoiceState == null || !guildVoiceState.inAudioChannel()) {
event.replyEmbeds(EmbedUtil.errorEmbed("You are not connected to a voice-channel!")).queue();
return;
}
}
if (command.getCommandFlags().contains(CommandFlag.MUST_BE_IN_SAME_VC)) {
GuildVoiceState voiceState = member.getVoiceState();
AudioManager manager = event.getGuild().getAudioManager();
if (manager.isConnected()) {
if (!manager.getConnectedChannel().equals(voiceState.getChannel())) {
event.replyEmbeds(EmbedUtil.errorEmbed("You must be listening in " + manager.getConnectedChannel().getAsMention() + "to do this!")).setEphemeral(true).queue();
return;
}
}
}
UserContextCommand finalCommand = command;
Runnable r = () -> {
try {
finalCommand.executeCommand(event);
Metrics.COMMANDS.labels("success").inc();
} catch (Exception e) {
Metrics.COMMANDS.labels("failed").inc();
LinkedDataObject translation = event.getGuild() == null ? LocaleLoader.getForLanguage("en_US") : LocaleLoader.ofGuild(event.getGuild());
if (event.isAcknowledged())
event.getHook().sendMessageEmbeds(EmbedUtil.errorEmbed(translation.getString("general.unknown_error_occured"))).setEphemeral(true).queue(s -> {
}, ex -> {
});
else
event.replyEmbeds(EmbedUtil.errorEmbed(translation.getString("general.unknown_error_occured"))).setEphemeral(true).queue(s -> {
}, ex -> {
});
LOGGER.error("Could not execute user-context-menu-command", e);
EmbedBuilder builder = new EmbedBuilder().setTitle("An error occurred while executing a user-context-command!").addField("Guild", event.getGuild() == null ? "None (Direct message)" : event.getGuild().getIdLong() + " (" + event.getGuild().getName() + ")", true).addField("Channel", event.getGuild() == null ? "None (Direct message)" : event.getChannel().getName(), true).addField("User", event.getUser().getAsMention() + " (" + event.getUser().getAsTag() + ")", true).addField("Command", event.getName(), false).setColor(EmbedUtil.ERROR_COLOR);
event.getJDA().openPrivateChannelById(Bean.OWNER_ID).flatMap(c -> c.sendMessageEmbeds(builder.build()).content("```fix\n" + ExceptionUtils.getStackTrace(e) + "\n```")).queue();
}
};
Bean.getInstance().getCommandExecutor().submit(r);
}
use of net.dv8tion.jda.api.entities.GuildVoiceState in project Bean by Xirado.
the class InteractionCommandHandler method handleMessageContextCommand.
public void handleMessageContextCommand(@NotNull MessageContextInteractionEvent event) {
if (!event.isFromGuild())
return;
Guild guild = event.getGuild();
Member member = event.getMember();
MessageContextCommand command = null;
if (registeredGuildCommands.containsKey(guild.getIdLong())) {
List<MessageContextCommand> guildCommands = registeredGuildCommands.get(guild.getIdLong()).stream().filter(cmd -> cmd instanceof MessageContextCommand).map(cmd -> (MessageContextCommand) cmd).toList();
MessageContextCommand guildCommand = guildCommands.stream().filter(cmd -> cmd.getData().getName().equalsIgnoreCase(event.getName())).findFirst().orElse(null);
if (guildCommand != null)
command = guildCommand;
}
if (command == null) {
MessageContextCommand globalCommand = getRegisteredMessageContextCommands().stream().filter(cmd -> cmd.getData().getName().equalsIgnoreCase(event.getName())).findFirst().orElse(null);
if (globalCommand != null)
command = globalCommand;
}
if (command == null)
return;
List<Permission> neededPermissions = command.getRequiredUserPermissions();
List<Permission> neededBotPermissions = command.getRequiredBotPermissions();
if (neededPermissions != null && !member.hasPermission((GuildChannel) event.getChannel(), neededPermissions)) {
event.reply(LocaleLoader.ofGuild(guild).get("general.no_perms", String.class)).queue();
return;
}
if (neededBotPermissions != null && !event.getGuild().getSelfMember().hasPermission((GuildChannel) event.getChannel(), neededBotPermissions)) {
event.reply(LocaleLoader.ofGuild(guild).get("general.no_bot_perms1", String.class)).queue();
return;
}
if (command.getCommandFlags().contains(CommandFlag.MUST_BE_IN_VC)) {
GuildVoiceState guildVoiceState = member.getVoiceState();
if (guildVoiceState == null || !guildVoiceState.inAudioChannel()) {
event.replyEmbeds(EmbedUtil.errorEmbed("You are not connected to a voice-channel!")).queue();
return;
}
}
if (command.getCommandFlags().contains(CommandFlag.MUST_BE_IN_SAME_VC)) {
GuildVoiceState voiceState = member.getVoiceState();
AudioManager manager = event.getGuild().getAudioManager();
if (manager.isConnected()) {
if (!manager.getConnectedChannel().equals(voiceState.getChannel())) {
event.replyEmbeds(EmbedUtil.errorEmbed("You must be listening in " + manager.getConnectedChannel().getAsMention() + "to do this!")).setEphemeral(true).queue();
return;
}
}
}
MessageContextCommand finalCommand = command;
Runnable r = () -> {
try {
finalCommand.executeCommand(event);
Metrics.COMMANDS.labels("success").inc();
} catch (Exception e) {
Metrics.COMMANDS.labels("failed").inc();
LinkedDataObject translation = event.getGuild() == null ? LocaleLoader.getForLanguage("en_US") : LocaleLoader.ofGuild(event.getGuild());
if (event.isAcknowledged())
event.getHook().sendMessageEmbeds(EmbedUtil.errorEmbed(translation.getString("general.unknown_error_occured"))).setEphemeral(true).queue(s -> {
}, ex -> {
});
else
event.replyEmbeds(EmbedUtil.errorEmbed(translation.getString("general.unknown_error_occured"))).setEphemeral(true).queue(s -> {
}, ex -> {
});
LOGGER.error("Could not execute message-context-menu-command", e);
EmbedBuilder builder = new EmbedBuilder().setTitle("An error occurred while executing a message-context-command!").addField("Guild", event.getGuild() == null ? "None (Direct message)" : event.getGuild().getIdLong() + " (" + event.getGuild().getName() + ")", true).addField("Channel", event.getGuild() == null ? "None (Direct message)" : event.getChannel().getName(), true).addField("User", event.getUser().getAsMention() + " (" + event.getUser().getAsTag() + ")", true).addField("Command", event.getName(), false).setColor(EmbedUtil.ERROR_COLOR);
event.getJDA().openPrivateChannelById(Bean.OWNER_ID).flatMap(c -> c.sendMessageEmbeds(builder.build()).content("```fix\n" + ExceptionUtils.getStackTrace(e) + "\n```")).queue();
}
};
Bean.getInstance().getCommandExecutor().submit(r);
}
use of net.dv8tion.jda.api.entities.GuildVoiceState in project Bean by Xirado.
the class JoinCommand method executeCommand.
@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
Member member = event.getMember();
GuildVoiceState voiceState = member.getVoiceState();
if (voiceState.getChannel() == null) {
event.replyEmbeds(EmbedUtil.errorEmbed("You must be listening in a voice channel to run this command!")).queue();
return;
}
GuildVoiceState state = event.getGuild().getSelfMember().getVoiceState();
if (state.getChannel() != null) {
AudioChannel channel = state.getChannel();
if (voiceState.getChannel().getIdLong() == channel.getIdLong()) {
event.replyEmbeds(EmbedUtil.errorEmbed("I already joined this channel!")).queue();
return;
}
if (Util.getListeningUsers(channel) > 0) {
event.replyEmbeds(EmbedUtil.errorEmbed("I am already playing music in **" + channel.getName() + "**!")).queue();
return;
}
}
GuildAudioPlayer audioPlayer = Bean.getInstance().getAudioManager().getAudioPlayer(event.getGuild().getIdLong());
try {
audioPlayer.getLink().connect(voiceState.getChannel());
} catch (PermissionException exception) {
event.replyEmbeds(EmbedUtil.errorEmbed("I do not have permission to join this channel!")).queue();
return;
}
event.replyEmbeds(EmbedUtil.successEmbed("Joined <#" + voiceState.getChannel().getIdLong() + ">!")).setEphemeral(true).queue();
audioPlayer.playerSetup((GuildMessageChannel) event.getChannel(), (s) -> {
}, e -> {
});
}
use of net.dv8tion.jda.api.entities.GuildVoiceState in project Bean by Xirado.
the class StopCommand method executeCommand.
@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
GuildVoiceState state = event.getGuild().getSelfMember().getVoiceState();
if (state.getChannel() == null) {
event.replyEmbeds(EmbedUtil.warningEmbed("I am not connected to a voice channel!")).queue();
return;
}
GuildAudioPlayer player = Bean.getInstance().getAudioManager().getAudioPlayer(event.getGuild().getIdLong());
if (player.getPlayer().getPlayingTrack() == null || Util.getListeningUsers(state.getChannel()) == 1) {
String name = state.getChannel().getName();
player.destroy();
event.replyEmbeds(EmbedUtil.defaultEmbed("Disconnected from **" + name + "**!")).queue();
player.setOpenPlayer(null);
return;
}
if (!ctx.getGuildData().isDJ(event.getMember())) {
boolean allowedToStop = true;
long userId = event.getUser().getIdLong();
List<AudioTrack> tracks = new ArrayList<>(player.getScheduler().getQueue());
tracks.add(player.getPlayer().getPlayingTrack());
for (AudioTrack track : tracks) {
TrackInfo trackInfo = track.getUserData(TrackInfo.class);
if (trackInfo.getRequesterIdLong() != userId) {
allowedToStop = false;
break;
}
}
if (!allowedToStop) {
event.replyEmbeds(EmbedUtil.errorEmbed("You need to be a DJ to do this!")).queue();
return;
}
}
player.setOpenPlayer(null);
String name = state.getChannel().getName();
player.destroy();
event.replyEmbeds(EmbedUtil.defaultEmbed("Disconnected from **" + name + "**!")).queue();
}
Aggregations