use of sx.blah.discord.handle.obj.IChannel in project DisCal-Discord-Bot by NovaFox161.
the class AnnouncementMessageFormatter method sendAnnouncementMessage.
/**
* Sends an embed with the announcement info in a proper format.
*
* @param announcement The announcement to send info about.
* @param event the calendar event the announcement is for.
* @param data The BotData belonging to the guild.
*/
static void sendAnnouncementMessage(Announcement announcement, Event event, CalendarData data, GuildSettings settings) {
EmbedBuilder em = new EmbedBuilder();
em.withAuthorIcon(Main.client.getGuildByID(266063520112574464L).getIconURL());
IGuild guild = Main.client.getGuildByID(announcement.getGuildId());
assert guild != null;
// Set all of the stuff for embeds regardless of announcement settings
if (settings.isBranded()) {
em.withAuthorName(guild.getName());
} else {
em.withAuthorName("DisCal");
}
em.withTitle(MessageManager.getMessage("Embed.Announcement.Announce.Title", settings));
EventData ed = DatabaseManager.getManager().getEventData(announcement.getGuildId(), event.getId());
if (ed.getImageLink() != null && ImageUtils.validate(ed.getImageLink())) {
em.withImage(ed.getImageLink());
}
em.withUrl(event.getHtmlLink());
try {
EventColor ec = EventColor.fromNameOrHexOrID(event.getColorId());
em.withColor(ec.getR(), ec.getG(), ec.getB());
} catch (Exception e) {
// I dunno, color probably null.
em.withColor(56, 138, 237);
}
if (!settings.usingSimpleAnnouncements()) {
em.withFooterText(MessageManager.getMessage("Embed.Announcement.Announce.ID", "%id%", announcement.getAnnouncementId().toString(), settings));
}
if (announcement.isInfoOnly()) {
// Only send info...
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Info", settings), announcement.getInfo(), false);
} else {
// Requires all announcement data
if (event.getSummary() != null) {
String summary = event.getSummary();
if (summary.length() > 250) {
summary = summary.substring(0, 250);
summary = summary + " (continues on Google Calendar View)";
}
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Summary", settings), summary, true);
}
if (event.getDescription() != null) {
String description = event.getDescription();
if (description.length() > 250) {
description = description.substring(0, 250);
description = description + " (continues on Google Calendar View)";
}
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Description", settings), description, true);
}
if (!settings.usingSimpleAnnouncements()) {
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Date", settings), EventMessageFormatter.getHumanReadableDate(event.getStart(), settings, false), true);
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Time", settings), EventMessageFormatter.getHumanReadableTime(event.getStart(), settings, false), true);
try {
Calendar service = CalendarAuth.getCalendarService();
String tz = service.calendars().get(data.getCalendarAddress()).execute().getTimeZone();
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.TimeZone", settings), tz, true);
} catch (Exception e1) {
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.TimeZone", settings), "Unknown *Error Occurred", true);
}
} else {
String start = EventMessageFormatter.getHumanReadableDate(event.getStart(), settings, false) + " at " + EventMessageFormatter.getHumanReadableTime(event.getStart(), settings, false);
try {
Calendar service = CalendarAuth.getCalendarService();
String tz = service.calendars().get(data.getCalendarAddress()).execute().getTimeZone();
start = start + " " + tz;
} catch (Exception e1) {
start = start + " (TZ UNKNOWN/ERROR)";
}
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Start", settings), start, false);
}
if (event.getLocation() != null && !event.getLocation().equalsIgnoreCase("")) {
if (event.getLocation().length() > 300) {
String location = event.getLocation().substring(0, 300).trim() + "... (cont. on Google Cal)";
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), location, true);
} else {
em.appendField(MessageManager.getMessage("Embed.Event.Confirm.Location", settings), event.getLocation(), true);
}
}
if (!settings.usingSimpleAnnouncements()) {
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.EventID", settings), event.getId(), false);
}
if (!announcement.getInfo().equalsIgnoreCase("None")) {
em.appendField(MessageManager.getMessage("Embed.Announcement.Announce.Info", settings), announcement.getInfo(), false);
}
}
IChannel channel = null;
try {
channel = guild.getChannelByID(Long.valueOf(announcement.getAnnouncementChannelId()));
} catch (Exception e) {
Logger.getLogger().exception(null, "Failed to find announcement channel! Will auto delete announcement to prevent issue! | Announcement: " + announcement.getAnnouncementId() + " | Guild: " + announcement.getGuildId(), e, AnnouncementMessageFormatter.class, true);
}
if (channel == null) {
// Channel does not exist or could not be found, automatically delete announcement to prevent issues.
DatabaseManager.getManager().deleteAnnouncement(announcement.getAnnouncementId().toString());
return;
}
Message.sendMessageAsync(em.build(), getSubscriberMentions(announcement, guild), channel);
}
use of sx.blah.discord.handle.obj.IChannel in project DisCal-Discord-Bot by NovaFox161.
the class Logger method exception.
public void exception(@Nullable IUser author, @Nullable String message, Exception e, Class clazz, boolean post) {
String timeStamp = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
// stack trace as a string
String error = sw.toString();
pw.close();
try {
sw.close();
} catch (IOException e1) {
// Can ignore silently...
}
if (Main.getSelfUser() != null) {
IUser bot = Main.getSelfUser();
if (post) {
String shortError = error;
if (error.length() > 1250) {
shortError = error.substring(0, 1250);
}
EmbedBuilder em = new EmbedBuilder();
if (bot != null) {
em.withAuthorIcon(bot.getAvatarURL());
}
if (author != null) {
em.withAuthorName(author.getName());
em.withThumbnail(author.getAvatarURL());
}
em.withColor(239, 15, 0);
em.withFooterText(clazz.getName());
// Send to discord!
em.appendField("Time", timeStamp, true);
if (e.getMessage() != null) {
if (e.getMessage().length() > 1024) {
em.appendField("Exception", e.getMessage().substring(0, 1024), true);
} else {
em.appendField("Exception", e.getMessage(), true);
}
}
if (message != null) {
em.appendField("Message", message, true);
}
// Get DisCal guild and channel..
IGuild guild = Main.client.getGuildByID(266063520112574464L);
IChannel channel = guild.getChannelByID(302249332244217856L);
Message.sendMessage(em.build(), "```" + shortError + "```", channel);
}
}
// ALWAYS LOG TO FILE!
try {
FileWriter exceptions = new FileWriter(exceptionsFile, true);
exceptions.write("ERROR --- " + timeStamp + " ---" + MessageUtils.lineBreak);
if (author != null) {
exceptions.write("user: " + author.getName() + "#" + author.getDiscriminator() + MessageUtils.lineBreak);
}
if (message != null) {
exceptions.write("message: " + message + MessageUtils.lineBreak);
}
exceptions.write(error + MessageUtils.lineBreak);
exceptions.close();
} catch (IOException io) {
io.printStackTrace();
}
}
use of sx.blah.discord.handle.obj.IChannel in project de-DiscordBot by DACH-Discord.
the class Roll method command_Roll.
@CommandSubscriber(command = "roll", help = "Würfeln. Syntax: `roll AnzahlWuerfel;[AugenJeWuerfel=6]`")
public void command_Roll(final IMessage commandMessage, final String diceArgsInput) throws InterruptedException {
final IChannel channel = commandMessage.getChannel();
final EmbedBuilder outputBuilder = new EmbedBuilder();
if (diceArgsInput.matches("^[0-9]+;?[0-9]*")) {
try {
final String[] args = diceArgsInput.split(";");
final int diceCount = Integer.parseInt(args[0]);
final int dotCount = args.length > 1 ? Integer.parseInt(args[1]) : DEFAULT_DOT_COUNT;
if (diceCount < 1 || dotCount < 1) {
throw new NumberFormatException("Würfelanzahl und maximale Augenzahl muss größer als 0 sein!");
}
final StringBuilder resultBuilder = new StringBuilder();
final int sum = IntStream.generate(() -> (rng.nextInt(dotCount) + 1)).limit(diceCount).reduce(0, (int acc, int number) -> {
resultBuilder.append(number);
resultBuilder.append("\n");
return acc + number;
});
resultBuilder.append(MessageFormat.format("Sum: {0}", sum));
outputBuilder.appendField(MessageFormat.format("Würfelt {0} Würfel mit einer maximalen Augenzahl von {1}!", diceCount, dotCount), resultBuilder.toString(), false);
EmbedObject rollObject = outputBuilder.build();
Util.sendEmbed(channel, rollObject);
} catch (NumberFormatException ex) {
Util.sendMessage(channel, MessageFormat.format("Konnte Eingabe '{0}' nicht verarbeiten." + "Bitte sicherstellen, dass sowohl die Würfelanzahl als auch die maximale Augenzahl Integer-Zahlen > 0 sind!", diceArgsInput));
}
} else {
Util.sendMessage(channel, "Syntax: `roll AnzahlWürfel;[AugenJeWürfel=6]`");
}
}
use of sx.blah.discord.handle.obj.IChannel in project Ublisk by Derkades.
the class DiscordBot method onChat.
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChat(AsyncPlayerChatEvent event) {
if (!enabled) {
super.log(this, LogLevel.DEBUG, "Skipping discord bot for this message - module has not been initialized successfully.");
return;
}
Player player = event.getPlayer();
String message = "[" + player.getName() + "] " + event.getMessage();
IChannel channel = client.getChannelByID("310155421719724052");
try {
channel.sendMessage(message);
} catch (MissingPermissionsException e) {
e.printStackTrace();
} catch (RateLimitException e) {
e.printStackTrace();
} catch (DiscordException e) {
e.printStackTrace();
}
}
use of sx.blah.discord.handle.obj.IChannel in project Discord4J by Discord4J.
the class ParrotBot method handle.
/**
* Called when the client receives a message.
*/
@Override
public void handle(MessageReceivedEvent event) {
// Gets the message from the event object NOTE: This is not the content of the message, but the object itself
IMessage message = event.getMessage();
// Gets the channel in which this message was sent.
IChannel channel = message.getChannel();
try {
// Builds (sends) and new message in the channel that the original message was sent with the content of the original message.
new MessageBuilder(this.client).withChannel(channel).withContent(message.getContent()).build();
} catch (RateLimitException e) {
// RateLimitException thrown. The bot is sending messages too quickly!
System.err.print("Sending messages too quickly!");
e.printStackTrace();
} catch (DiscordException e) {
// DiscordException thrown. Many possibilities. Use getErrorMessage() to see what went wrong.
// Print the error message sent by Discord
System.err.print(e.getErrorMessage());
e.printStackTrace();
} catch (MissingPermissionsException e) {
// MissingPermissionsException thrown. The bot doesn't have permission to send the message!
System.err.print("Missing permissions for channel!");
e.printStackTrace();
}
}
Aggregations