use of net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege in project triumph-cmds by TriumphTeam.
the class SlashCommandManager method updateAllCommands.
/**
* Updates all the commands in one go.
* This should be used if the default trigger for the updating of the commands isn't working.
* Or if commands are added after the initial setup.
*/
public void updateAllCommands() {
jda.updateCommands().addCommands(globalCommands.values().stream().map(SlashCommand::asCommandData).collect(Collectors.toList())).queue();
guildCommands.entrySet().stream().map(entry -> {
final Guild guild = jda.getGuildById(entry.getKey());
return guild != null ? Maps.immutableEntry(guild, entry.getValue()) : null;
}).filter(Objects::nonNull).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)).forEach((guild, commands) -> guild.updateCommands().addCommands(commands.values().stream().map(SlashCommand::asCommandData).collect(Collectors.toList())).queue(cmds -> guild.updateCommandPrivileges(cmds.stream().map(cmd -> {
final SlashCommand<S> command = commands.get(cmd.getName());
if (command == null)
return null;
final List<CommandPrivilege> privileges = Stream.concat(command.getEnabledRoles().stream().map(id -> new CommandPrivilege(CommandPrivilege.Type.ROLE, true, id)), command.getDisabledRoles().stream().map(id -> new CommandPrivilege(CommandPrivilege.Type.ROLE, false, id))).collect(Collectors.toList());
if (privileges.isEmpty())
return null;
return Maps.immutableEntry(cmd.getId(), privileges);
}).filter(Objects::nonNull).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))).queue()));
}
use of net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege in project reputation-bot by RainbowDashLabs.
the class Permissions method buildGuildPriviledges.
public static void buildGuildPriviledges(GuildData guildData, Guild guild) {
var settings = guildData.getGuildSettings(guild);
var optRole = settings.generalSettings().managerRole().map(guild::getRoleById);
List<Role> roles;
log.debug("Refreshing command priviledges for guild {}", Guilds.prettyName(guild));
if (optRole.isEmpty()) {
log.debug("No manager role defined on guild {}. Using admin roles.", guild.getIdLong());
roles = guild.getRoles().stream().filter(r -> r.hasPermission(Permission.ADMINISTRATOR)).limit(5).collect(Collectors.toList());
} else {
log.debug("Using manager role on {}", Guilds.prettyName(guild));
roles = Collections.singletonList(optRole.get());
}
List<CommandPrivilege> privileges = new ArrayList<>();
for (Role role : roles) {
privileges.add(CommandPrivilege.enable(role));
}
privileges.add(CommandPrivilege.enable(guild.retrieveOwner().complete().getUser()));
var adminCommands = guild.retrieveCommands().complete().stream().filter(c -> !c.isDefaultEnabled()).collect(Collectors.toList());
Map<String, Collection<CommandPrivilege>> commandPrivileges = new HashMap<>();
for (var adminCommand : adminCommands) {
commandPrivileges.put(adminCommand.getId(), privileges);
}
log.debug("Update done. Set restricted commands to {} priviledges", privileges.size());
guild.updateCommandPrivileges(commandPrivileges).complete();
}
use of net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege in project DIH4JDA by DynxstyGIT.
the class InteractionHandler method registerCommandPrivileges.
/**
* Registers all Command Privileges.
*
* @param jda The {@link JDA} instance.
*/
private void registerCommandPrivileges(JDA jda) {
for (Guild guild : jda.getGuilds()) {
Map<String, Set<CommandPrivilege>> privileges = new HashMap<>();
guild.retrieveCommands().queue(commands -> {
for (Command command : commands) {
if (privileges.containsKey(command.getId()))
continue;
Optional<SlashCommandInteraction> interactionOptional = this.slashCommandIndex.keySet().stream().filter(p -> p.equals(command.getName()) || p.split("/")[0].equals(command.getName())).map(slashCommandIndex::get).filter(p -> p.getPrivileges() != null && p.getPrivileges().length > 0).findFirst();
if (interactionOptional.isPresent()) {
SlashCommandInteraction interaction = interactionOptional.get();
if (interaction.getBaseClass().getSuperclass().equals(GlobalSlashCommand.class)) {
DIH4JDALogger.error(String.format("Can not register command privileges for global command %s (%s).", command.getName(), interaction.getBaseClass().getSimpleName()));
continue;
}
privileges.put(command.getId(), new HashSet<>(Arrays.asList(interaction.getPrivileges())));
DIH4JDALogger.info(String.format("[%s] Registered privileges for command %s: %s", guild.getName(), command.getName(), Arrays.stream(interaction.getPrivileges()).map(CommandPrivilege::toData).collect(Collectors.toList())), DIH4JDALogger.Type.COMMAND_PRIVILEGE_REGISTERED);
}
if (privileges.isEmpty())
continue;
guild.updateCommandPrivileges(privileges).queue();
}
});
}
}
use of net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege in project JDA by DV8FromTheWorld.
the class GuildImpl method updateCommandPrivileges.
@Nonnull
@Override
public RestAction<Map<String, List<CommandPrivilege>>> updateCommandPrivileges(@Nonnull Map<String, ? extends Collection<CommandPrivilege>> privileges) {
Checks.notNull(privileges, "Privileges");
privileges.forEach((key, value) -> {
Checks.isSnowflake(key, "Map Key");
Checks.noneNull(value, "Privilege List for Command");
Checks.check(value.size() <= 10, "Cannot have more than 10 privileges for a command!");
});
DataArray array = DataArray.empty();
privileges.forEach((commandId, list) -> {
DataObject entry = DataObject.empty();
entry.put("id", commandId);
entry.put("permissions", DataArray.fromCollection(list));
array.add(entry);
});
Route.CompiledRoute route = Route.Interactions.EDIT_ALL_COMMAND_PERMISSIONS.compile(getJDA().getSelfUser().getApplicationId(), getId());
return new RestActionImpl<>(getJDA(), route, RequestBody.create(Requester.MEDIA_TYPE_JSON, array.toJson()), (response, request) -> {
Map<String, List<CommandPrivilege>> map = new HashMap<>();
response.getArray().stream(DataArray::getObject).forEach(obj -> {
String id = obj.getString("id");
List<CommandPrivilege> list = parsePrivilegesList(obj);
map.put(id, list);
});
return map;
});
}
use of net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege in project TechDiscordBot by TechsCode-Team.
the class ModulesManager method load.
public void load() {
TechDiscordBot.getJDA().updateCommands().queue();
CommandListUpdateAction commands = TechDiscordBot.getGuild().updateCommands();
for (Class<?> each : ProjectUtil.getClasses("me.TechsCode.TechDiscordBot.module")) {
if (CommandModule.class.isAssignableFrom(each) && !Modifier.isAbstract(each.getModifiers())) {
try {
CommandModule module = (CommandModule) each.getConstructor(TechDiscordBot.class).newInstance(TechDiscordBot.getBot());
if (module.getName() == null)
continue;
cmdModules.add(module);
CommandData cmdData = new CommandData(module.getName(), module.getDescription() == null ? "No description set." : module.getDescription()).addOptions(module.getOptions()).setDefaultEnabled(module.getCommandPrivileges().length == 0);
commands.addCommands(cmdData);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
} else if (Module.class.isAssignableFrom(each) && !Modifier.isAbstract(each.getModifiers())) {
try {
Module module = (Module) each.getConstructor(TechDiscordBot.class).newInstance(TechDiscordBot.getBot());
module.enable();
modules.add(module);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
}
}
commands.addCommands(new CommandData("ticket", "Manage tickets.").addSubcommands(new SubcommandData("add", "Add a member to a ticket.").addOptions(new OptionData(OptionType.USER, "member", "Member to add.", true)), new SubcommandData("remove", "Remove a member from a ticket.").addOptions(new OptionData(OptionType.USER, "member", "Member to remove.", true)), new SubcommandData("transcript", "Force make a ticket transcript."), new SubcommandData("close", "Close a ticket.").addOptions(new OptionData(OptionType.STRING, "reason", "Reason to close the ticket. (Optional)")))).queue(cmds -> {
cmds.forEach(command -> {
CommandPrivilege[] privilege = cmdModules.stream().filter(c -> c.getName().equals(command.getName())).map(CommandModule::getCommandPrivileges).findFirst().orElse(new CommandPrivilege[] {});
if (privilege.length > 0)
TechDiscordBot.getGuild().updateCommandPrivilegesById(command.getId(), Arrays.asList(privilege)).queue();
});
});
TechDiscordBot.getJDA().addEventListener(modules.toArray());
TechDiscordBot.getJDA().addEventListener(cmdModules.toArray());
Runtime.getRuntime().addShutdownHook(new Thread(() -> modules.forEach(Module::onDisable)));
}
Aggregations