use of net.dv8tion.jda.api.entities.Message in project Saber-Bot by notem.
the class TestCommand method action.
@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
int index = 0;
// get entry object
Integer entryId = ParsingUtilities.encodeIDToInt(args[index]);
ScheduleEntry entry = Main.getEntryManager().getEntry(entryId);
// verify the entry's message exists
Message msg = entry.getMessageObject();
if (msg == null)
return;
index++;
String format = Main.getScheduleManager().getStartAnnounceFormat(entry.getChannelId());
if (args.length == 2) {
switch(args[index].toLowerCase()) {
case "e":
case "end":
format = Main.getScheduleManager().getEndAnnounceFormat(entry.getChannelId());
break;
case "r":
case "remind":
format = Main.getScheduleManager().getReminderFormat(entry.getChannelId());
break;
}
}
String remindMsg = ParsingUtilities.processText(format, entry, true);
MessageUtilities.sendMsg(remindMsg, event.getChannel(), null);
}
use of net.dv8tion.jda.api.entities.Message in project Saber-Bot by notem.
the class ScheduleManager method sortSchedule.
/**
* Reorders the schedule so that entries are displayed by start datetime ascending order in
* the discord schedule channel
* @param cId schedule ID
* @param reverseOrder (boolean) whether or not to reverse the sort order
*/
public void sortSchedule(String cId, boolean reverseOrder) {
if (this.getScheduleSize(cId) > MAX_SIZE_TO_SYNC)
return;
if (this.isLocked(cId))
return;
// lock the channel
this.lock(cId);
// always unlock the schedule at finish regardless of success or failure
try {
// identify which shard is responsible for the schedule
Document doc = Main.getDBDriver().getScheduleCollection().find(eq("_id", cId)).projection(fields(include("guildId"))).first();
JDA jda = Main.getShardManager().getJDA(doc.getString("guildId"));
// find the message channel and send the 'is typing' while processing
MessageChannel chan = jda.getTextChannelById(cId);
chan.sendTyping().queue();
int sortOrder = 1;
if (reverseOrder)
sortOrder = -1;
LinkedList<ScheduleEntry> unsortedEntries = new LinkedList<>();
Main.getDBDriver().getEventCollection().find(eq("channelId", cId)).sort(new Document("start", sortOrder)).forEach((Consumer<? super Document>) document -> unsortedEntries.add(new ScheduleEntry(document)));
// selection sort the entries by timestamp
while (!unsortedEntries.isEmpty()) {
// continue to send 'is typing'
chan.sendTyping().queue();
ScheduleEntry top = unsortedEntries.pop();
ScheduleEntry min = top;
for (ScheduleEntry cur : unsortedEntries) {
Message minMsg = min.getMessageObject();
Message topMsg = cur.getMessageObject();
if (minMsg != null && topMsg != null) {
OffsetDateTime a = minMsg.getTimeCreated();
OffsetDateTime b = topMsg.getTimeCreated();
if (a.isAfter(b)) {
min = cur;
}
}
}
// swap messages and update db
if (!(min == top)) {
Message tmp = top.getMessageObject();
top.setMessageObject(min.getMessageObject());
Main.getDBDriver().getEventCollection().updateOne(eq("_id", top.getId()), new Document("$set", new Document("messageId", min.getMessageObject().getId())));
min.setMessageObject(tmp);
Main.getDBDriver().getEventCollection().updateOne(eq("_id", min.getId()), new Document("$set", new Document("messageId", tmp.getId())));
}
// reload display
top.reloadDisplay();
}
} catch (PermissionException e) {
String m = e.getMessage() + ": Channel ID " + cId;
Logging.warn(this.getClass(), m);
} catch (Exception e) {
Logging.exception(this.getClass(), e);
} finally {
// always unlock
this.unlock(cId);
}
}
use of net.dv8tion.jda.api.entities.Message in project MantaroBot by Mantaro.
the class Poll method createPoll.
private void createPoll(Context ctx, Message message, I18nContext languageContext) {
runningPoll = ReactionOperations.create(message, TimeUnit.MILLISECONDS.toSeconds(timeout), new ReactionOperation() {
@Override
public int add(MessageReactionAddEvent e) {
// always return false anyway lul
return Operation.IGNORED;
}
@Override
public void onExpire() {
if (getChannel() == null)
return;
var user = ctx.getAuthor();
var embedBuilder = new EmbedBuilder().setTitle(languageContext.get("commands.poll.result_header")).setDescription(String.format(languageContext.get("commands.poll.result_screen"), user.getName(), name)).setFooter(languageContext.get("commands.poll.thank_note"), null);
var react = new AtomicInteger(0);
var counter = new AtomicInteger(0);
getChannel().retrieveMessageById(message.getIdLong()).queue(message -> {
var votes = message.getReactions().stream().filter(r -> react.getAndIncrement() <= options.length).map(r -> String.format(languageContext.get("commands.poll.vote_results"), r.getCount() - 1, options[counter.getAndIncrement()])).collect(Collectors.joining("\n"));
embedBuilder.addField(languageContext.get("commands.poll.results"), "```diff\n" + votes + "```", false);
getChannel().sendMessageEmbeds(embedBuilder.build()).queue();
});
getRunningPolls().remove(getChannel().getId());
}
@Override
public void onCancel() {
getChannel().sendMessageFormat(languageContext.get("commands.poll.cancelled"), EmoteReference.CORRECT).queue();
onExpire();
}
}, reactions(options.length));
}
use of net.dv8tion.jda.api.entities.Message in project c0debaseBot by Biospheere.
the class ClearCommand method execute.
@Override
public void execute(final String[] args, final Message message) {
if (args.length == 0) {
final EmbedBuilder embedBuilder = getEmbed(message.getMember());
embedBuilder.appendDescription("!clear <Amount>");
message.getTextChannel().sendMessage(embedBuilder.build()).queue();
} else {
int i = 0;
try {
i = Integer.valueOf(args[0]);
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
final MessageHistory history = new MessageHistory(message.getTextChannel());
final List<Message> messages = history.retrievePast(i + 1).complete();
message.getTextChannel().deleteMessages(messages).queue();
final EmbedBuilder embedBuilder = DiscordUtils.getDefaultEmbed(message.getMember());
embedBuilder.setColor(message.getGuild().getSelfMember().getColor());
embedBuilder.appendDescription("Es wurden **" + (i) + "** Nachrichten gelöscht");
message.getTextChannel().sendMessage(embedBuilder.build()).queue();
}
}
use of net.dv8tion.jda.api.entities.Message in project lavaplayer by sedmelluq.
the class MusicScheduler method updateTrackBox.
private void updateTrackBox(boolean newMessage) {
AudioTrack track = player.getPlayingTrack();
if (track == null || newMessage) {
Message message = boxMessage.getAndSet(null);
if (message != null) {
message.delete();
}
}
if (track != null) {
Message message = boxMessage.get();
String box = TrackBoxBuilder.buildTrackBox(80, track, player.isPaused(), player.getVolume());
if (message != null) {
message.editMessage(box).queue();
} else {
if (creatingBoxMessage.compareAndSet(false, true)) {
messageDispatcher.sendMessage(box, created -> {
boxMessage.set(created);
creatingBoxMessage.set(false);
}, error -> {
creatingBoxMessage.set(false);
});
}
}
}
}
Aggregations