use of net.dv8tion.jda.core.JDA in project Saber-Bot by notem.
the class ScheduleEntry method getMessageObject.
/**
* Attempts to retrieve the discord Message, if the message does not exist
* (or the bot can for any other reason cannot retrieve it) the method returns null
* @return (Message) if exists, otherwise null
*/
public Message getMessageObject() {
Message msg;
try {
JDA jda = Main.getShardManager().isSharding() ? Main.getShardManager().getShard(guildId) : Main.getShardManager().getJDA();
msg = jda.getTextChannelById(this.chanId).getMessageById(this.msgId).complete();
} catch (Exception e) {
msg = null;
}
return msg;
}
use of net.dv8tion.jda.core.JDA in project Saber-Bot by notem.
the class EventListener method onGuildJoin.
@Override
public void onGuildJoin(GuildJoinEvent event) {
// leave guild if guild is blacklisted
if (Main.getBotSettingsManager().getBlackList().contains(event.getGuild().getId())) {
event.getGuild().leave().queue();
return;
}
// identify which shard is responsible for the guild
String guildId = event.getGuild().getId();
JDA jda = Main.getShardManager().isSharding() ? Main.getShardManager().getShard(guildId) : Main.getShardManager().getJDA();
// send welcome message to the server owner
String welcomeMessage = "```diff\n- Joined```\n" + "**" + jda.getSelfUser().getName() + "**, a calendar bot, has been added to the guild you own, '" + event.getGuild().getName() + "'." + "\n\n" + "If this is your first time using the bot, you will need to create a new channel in your guild named" + " **" + Main.getBotSettingsManager().getControlChan() + "** to control the bot.\n" + "The bot will not listen to commands in any other channel!" + "\n\n" + "If you have not yet reviewed the **Quickstart** guide (as seen on the bots.discord.pw listing), " + "it may be found here: https://bots.discord.pw/bots/250801603630596100";
MessageUtilities.sendPrivateMsg(welcomeMessage, event.getGuild().getOwner().getUser(), null);
// update web stats
JDA.ShardInfo info = event.getJDA().getShardInfo();
HttpUtilities.updateStats(info == null ? null : info.getShardId());
}
use of net.dv8tion.jda.core.JDA 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 schedule entries 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");
MessageChannel channel = jda.getTextChannelById(channelId);
if (channel == null) {
Main.getDBDriver().getEventCollection().deleteOne(eq("_id", eventId));
Logging.info(this.getClass(), "Pruned event with ID: " + eventId);
return;
}
channel.getMessageById(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.core.JDA in project Saber-Bot by notem.
the class ParsingUtilities method compileUserList.
/**
* generates a list of user IDs for a given RSVP category of an event
* @param entry ScheduleEntry object
* @param name name of RSVP category
* @return List of Stings or null if category is invalid
*/
private static List<String> compileUserList(ScheduleEntry entry, String name) {
List<String> users = null;
if (name.toLowerCase().equals("no-input")) {
List<String> rsvped = new ArrayList<>();
Set<String> keys = entry.getRsvpMembers().keySet();
for (String key : keys) {
rsvped.addAll(entry.getRsvpMembersOfType(key));
}
JDA shard = Main.getShardManager().getJDA(entry.getGuildId());
Guild guild = shard.getGuildById(entry.getGuildId());
Channel channel = shard.getTextChannelById(entry.getChannelId());
users = guild.getMembers().stream().filter(member -> member.getPermissions(channel).contains(Permission.MESSAGE_READ)).map(member -> member.getUser().getId()).filter(memberId -> !rsvped.contains(memberId)).collect(Collectors.toList());
} else {
List<String> members = entry.getRsvpMembers().get(name);
if (members != null)
users = entry.getRsvpMembersOfType(name);
}
return users;
}
use of net.dv8tion.jda.core.JDA in project Saber-Bot by notem.
the class VerifyUtilities method verifyEmoji.
/**
* verify that a supplied emoji string is a valid discord emoji
* @param emoji emoji string (either raw emoji char or discord emoji ID)
* @return true if valid
*/
public static boolean verifyEmoji(String emoji) {
if (!EmojiManager.isEmoji(emoji)) {
String emoteId = emoji.replaceAll("[^\\d]", "");
Emote emote = null;
try {
for (JDA jda : Main.getShardManager().getShards()) {
emote = jda.getEmoteById(emoteId);
if (emote != null)
break;
}
} catch (Exception e) {
return false;
}
if (emote == null) {
return false;
}
}
return true;
}
Aggregations