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();
}
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();
}
}
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);
}
});
}
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);
}
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);
}
Aggregations