Search in sources :

Example 1 with Guild

use of net.dv8tion.jda.api.entities.Guild in project c0debaseBot by Biospheere.

the class RoleStatsCommand method execute.

@Override
public void execute(final String[] args, final Message message) {
    final Guild guild = message.getGuild();
    final EmbedBuilder embedBuilder = getEmbed(guild, message.getAuthor()).setTitle("Rollen Statistiken");
    final Map<Role, Long> roles = guild.getMembers().stream().filter(member -> PermissionUtil.canInteract(guild.getSelfMember(), member)).map(member -> member.getRoles()).flatMap(stream -> stream.stream().filter(role -> (!role.isManaged() && !FORBIDDEN.contains(role.getName())))).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    embedBuilder.appendDescription("\n__**Rollen:**__\n\n");
    appendStats(roles, embedBuilder, false);
    embedBuilder.appendDescription("\n__**Farb-Rollen:**__\n\n");
    appendStats(roles, embedBuilder, true);
    message.getTextChannel().sendMessage(embedBuilder.build()).queue();
}
Also used : Role(net.dv8tion.jda.api.entities.Role) Message(net.dv8tion.jda.api.entities.Message) java.util(java.util) Guild(net.dv8tion.jda.api.entities.Guild) Role(net.dv8tion.jda.api.entities.Role) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Command(de.c0debase.bot.commands.Command) PermissionUtil(net.dv8tion.jda.internal.utils.PermissionUtil) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Guild(net.dv8tion.jda.api.entities.Guild)

Example 2 with Guild

use of net.dv8tion.jda.api.entities.Guild in project c0debaseBot by Biospheere.

the class Tempchannel method onTempchannelJoin.

@Override
public void onTempchannelJoin(final VoiceChannel voiceChannel, final Member member) {
    if (voiceChannel.equals(voiceChannel.getGuild().getAfkChannel())) {
        return;
    }
    if (textChannel == null) {
        final Guild guild = voiceChannel.getGuild();
        voiceChannel.getParent().createTextChannel("temp-" + voiceChannel.getName().toLowerCase()).addPermissionOverride(guild.getSelfMember(), MEMBER_PERMISSIONS, null).addPermissionOverride(member, MEMBER_PERMISSIONS, null).addPermissionOverride(guild.getPublicRole(), null, MEMBER_PERMISSIONS).queue(channel -> setTextChannel((TextChannel) channel));
    } else {
        if (textChannel.getPermissionOverride(member) != null) {
            textChannel.getPermissionOverride(member).getManager().grant(MEMBER_PERMISSIONS).queue();
        } else {
            textChannel.createPermissionOverride(member).setAllow(MEMBER_PERMISSIONS).queue();
        }
        if (member.getUser().isBot()) {
            return;
        }
        final EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setColor(member.getColor());
        embedBuilder.setDescription(":arrow_right: " + member.getAsMention() + " ist beigetreten");
        textChannel.sendMessage(embedBuilder.build()).queue();
    }
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Guild(net.dv8tion.jda.api.entities.Guild)

Example 3 with Guild

use of net.dv8tion.jda.api.entities.Guild in project Saber-Bot by notem.

the class Pruner method run.

@Override
public void run() {
    Logging.info(this.getClass(), "Running database pruner. . .");
    // purge guild setting entries for any guild not connected to the bot
    Bson query = new Document();
    Main.getDBDriver().getGuildCollection().find(query).projection(fields(include("_id"))).forEach((Consumer<? super Document>) document -> {
        try {
            String guildId = document.getString("_id");
            JDA jda = Main.getShardManager().getShard(guildId);
            if (jda == null)
                return;
            if (JDA.Status.valueOf("CONNECTED") != jda.getStatus())
                return;
            Guild guild = jda.getGuildById(guildId);
            if (guild == null) {
                Main.getDBDriver().getGuildCollection().deleteOne(eq("_id", guildId));
                Main.getDBDriver().getEventCollection().deleteMany(eq("guildId", guildId));
                Main.getDBDriver().getScheduleCollection().deleteMany(eq("guildId", guildId));
                Logging.info(this.getClass(), "Pruned guild with ID: " + guildId);
            }
        } catch (Exception e) {
            Logging.exception(this.getClass(), e);
        }
    });
    // purge schedules that the bot cannot connect to
    query = new Document();
    Main.getDBDriver().getScheduleCollection().find(query).projection(fields(include("_id", "guildId"))).forEach((Consumer<? super Document>) document -> {
        try {
            String guildId = document.getString("guildId");
            JDA jda = Main.getShardManager().getShard(guildId);
            if (jda == null)
                return;
            if (JDA.Status.valueOf("CONNECTED") != jda.getStatus())
                return;
            String chanId = document.getString("_id");
            MessageChannel channel = jda.getTextChannelById(chanId);
            if (channel == null) {
                Main.getDBDriver().getEventCollection().deleteMany(eq("channeldId", chanId));
                Main.getDBDriver().getScheduleCollection().deleteMany(eq("_id", chanId));
                Logging.info(this.getClass(), "Pruned schedule with channel ID: " + chanId);
            }
        } catch (Exception e) {
            Logging.exception(this.getClass(), e);
        }
    });
    // purge events for which the bot cannot access the message
    query = new Document();
    Main.getDBDriver().getEventCollection().find(query).projection(fields(include("_id", "messageId", "channelId", "guildId"))).forEach((Consumer<? super Document>) document -> {
        try {
            String guildId = document.getString("guildId");
            JDA jda = Main.getShardManager().getShard(guildId);
            if (jda == null)
                return;
            if (JDA.Status.valueOf("CONNECTED") != jda.getStatus())
                return;
            Integer eventId = document.getInteger("_id");
            String messageId = document.getString("messageId");
            if (messageId == null) {
                Main.getDBDriver().getEventCollection().deleteOne(eq("_id", eventId));
                Logging.info(this.getClass(), "Pruned event with ID: " + eventId);
                return;
            }
            String channelId = document.getString("channelId");
            TextChannel channel = jda.getTextChannelById(channelId);
            if (channel == null) {
                return;
            }
            channel.retrieveMessageById(messageId).queue(message -> {
                if (message == null) {
                    Main.getDBDriver().getEventCollection().deleteOne(eq("_id", eventId));
                    Logging.info(this.getClass(), "Pruned event with ID: " + eventId + " on channel with ID: " + channelId);
                }
            }, throwable -> {
                Main.getDBDriver().getEventCollection().deleteOne(eq("_id", eventId));
                Logging.info(this.getClass(), "Pruned event with ID: " + eventId + " on channel with ID: " + channelId);
            });
        } catch (Exception e) {
            Logging.exception(this.getClass(), e);
        }
    });
}
Also used : Document(org.bson.Document) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) JDA(net.dv8tion.jda.api.JDA) Projections.fields(com.mongodb.client.model.Projections.fields) Permission(net.dv8tion.jda.api.Permission) Logging(ws.nmathe.saber.utils.Logging) Collection(java.util.Collection) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Collectors(java.util.stream.Collectors) Projections.include(com.mongodb.client.model.Projections.include) Bson(org.bson.conversions.Bson) Consumer(java.util.function.Consumer) Guild(net.dv8tion.jda.api.entities.Guild) Stream(java.util.stream.Stream) Filters.eq(com.mongodb.client.model.Filters.eq) Main(ws.nmathe.saber.Main) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) JDA(net.dv8tion.jda.api.JDA) Document(org.bson.Document) Guild(net.dv8tion.jda.api.entities.Guild) Bson(org.bson.conversions.Bson)

Example 4 with Guild

use of net.dv8tion.jda.api.entities.Guild in project Saber-Bot by notem.

the class SchedulesCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    Guild guild = event.getGuild();
    List<String> scheduleIds = Main.getScheduleManager().getSchedulesForGuild(guild.getId());
    // build output main body
    StringBuilder content = new StringBuilder();
    for (String sId : scheduleIds) {
        content.append("<#").append(sId).append("> - has ").append(Main.getEntryManager().getEntriesFromChannel(sId).size()).append(" events\n");
    }
    // title for embed
    String title = "Schedules on " + guild.getName();
    // footer for embed
    String footer = scheduleIds.size() + " schedule(s)";
    // build embed
    MessageEmbed embed = new EmbedBuilder().setDescription(content.toString()).setTitle(title).setFooter(footer, null).build();
    // build message
    Message message = new MessageBuilder().setEmbed(embed).build();
    // send message
    MessageUtilities.sendMsg(message, event.getTextChannel(), null);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Message(net.dv8tion.jda.api.entities.Message) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Guild(net.dv8tion.jda.api.entities.Guild)

Example 5 with Guild

use of net.dv8tion.jda.api.entities.Guild in project Saber-Bot by notem.

the class ScheduleManager method createSchedule.

/**
 * Create a new schedule and it's associated schedule channel, if the bot cannot create the
 * new channel no schedule will be created
 * @param gId (String) guild ID
 * @param optional (String) optional name of schedule channel
 */
public void createSchedule(String gId, String optional) {
    JDA jda = Main.getShardManager().getJDA(gId);
    // bot self permissions
    Collection<Permission> channelPerms = Stream.of(Permission.MESSAGE_ADD_REACTION, Permission.MESSAGE_EMBED_LINKS, Permission.MESSAGE_MANAGE, Permission.MESSAGE_HISTORY, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE, Permission.MESSAGE_ATTACH_FILES).collect(Collectors.toList());
    // create channel and get ID
    String cId;
    try {
        Guild guild = jda.getGuildById(gId);
        cId = guild.createTextChannel(optional != null ? optional : "new_schedule").addPermissionOverride(// allow self permissions
        guild.getMember(jda.getSelfUser()), channelPerms, new ArrayList<>()).addPermissionOverride(// disable @everyone message write
        guild.getPublicRole(), // disable @everyone message write
        new ArrayList<>(), Collections.singletonList(Permission.MESSAGE_WRITE)).complete().getId();
    } catch (PermissionException e) {
        String m = e.getMessage() + ": Guild ID " + gId;
        Logging.warn(this.getClass(), m);
        return;
    } catch (Exception e) {
        Logging.exception(this.getClass(), e);
        return;
    }
    // create the schedule database entry
    createNewSchedule(cId, gId);
}
Also used : PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) JDA(net.dv8tion.jda.api.JDA) Permission(net.dv8tion.jda.api.Permission) Guild(net.dv8tion.jda.api.entities.Guild) PermissionException(net.dv8tion.jda.api.exceptions.PermissionException)

Aggregations

Guild (net.dv8tion.jda.api.entities.Guild)6 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 TextChannel (net.dv8tion.jda.api.entities.TextChannel)3 Collectors (java.util.stream.Collectors)2 JDA (net.dv8tion.jda.api.JDA)2 Permission (net.dv8tion.jda.api.Permission)2 Message (net.dv8tion.jda.api.entities.Message)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 Filters.eq (com.mongodb.client.model.Filters.eq)1 Projections.fields (com.mongodb.client.model.Projections.fields)1 Projections.include (com.mongodb.client.model.Projections.include)1 Command (de.c0debase.bot.commands.Command)1 java.util (java.util)1 Collection (java.util.Collection)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Stream (java.util.stream.Stream)1 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)1 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)1 Role (net.dv8tion.jda.api.entities.Role)1