@Override public Matcher getMatcher(IMessage message){ String prefixe = getPrefix(message); return Pattern.compile("^" + Pattern.quote(prefixe) + name + pattern + "$").matcher(message.getContent()); }
public MessageTokenizer(IMessage message) { this(message.getClient(), message.getContent()); }
@Override public void flush() throws IOException { if (isClosed.get()) throw new IOException("This stream is closed."); final AtomicReference<IOException> exceptionReference = new AtomicReference<>(); if (buf.get().size() > 0) RequestBuffer.request(() -> { try { IMessage currentMessage = getCurrentMessage(); if (currentMessage != null) { String toAdd = getStringFromCharBuffer(MAX_MESSAGE_LENGTH - currentMessage.getContent().length()); messages.set(messages.indexOf(currentMessage), currentMessage.edit(currentMessage.getContent() + toAdd)); } else { messages.add(channel.sendMessage(getStringFromCharBuffer(MAX_MESSAGE_LENGTH))); } } catch (Exception e) { if (e instanceof RateLimitException) throw (RateLimitException) e; else exceptionReference.set(new IOException(e)); } }); if (exceptionReference.get() != null) throw exceptionReference.get(); }
/** * Génère une liste de source formatée à partir d'un salon textuel * @param channel Salon d'origine * @return Liste de message éligibles à une reconnaissance de langue */ private static List<String> getReformatedMessages(IChannel channel){ List<String> result = new ArrayList<>(); if (channel != null) { try { IMessage[] messages = channel.getMessageHistory(MAX_MESSAGES_READ).asArray(); for (IMessage message : messages) { String content = message.getContent().replaceAll(":\\w+:", "").trim(); if (content.length() > MAX_CHARACTER_ACCEPTANCE) result.add(content); } } catch (Exception e){ LOG.warn("Impossible to gather data from " + channel.getStringID() + "/" + channel.getName()); } } return result; }
@EventSubscriber public void onMessageReceived(MessageReceivedEvent event) { IMessage message = event.getMessage(); String[] command = message.getContent().split(" ", 2); IGuild guild = message.getGuild(); if (guild != null) { if ("~play".equals(command[0]) && command.length == 2) { loadAndPlay(message.getChannel(), command[1]); } else if ("~skip".equals(command[0])) { skipTrack(message.getChannel()); } } }
@Override public void throwException(IMessage message, Command command, Language lg, Object... arguments) { AnkamaBug bug = null; if (command instanceof WhoisCommand) bug = AnkamaBug.CHARACTER_NOT_FOUND; else if (command instanceof GuildCommand) bug = AnkamaBug.GUILD_NOT_FOUND; else if (command instanceof AllianceCommand) bug = AnkamaBug.ALLY_NOT_FOUND; else if (command instanceof ItemCommand) if (message.getContent().contains("'")) bug = AnkamaBug.ITEM_NOT_FOUND_APOSTROPHE; else if (message.getContent().toLowerCase().contains(Translator.getLabel(lg, "equip.muldo").toLowerCase()) || message.getContent().toLowerCase().contains(Translator.getLabel(lg, "equip.volkorne").toLowerCase())) bug = AnkamaBug.ITEM_PAGE_MULDO_VOLKORNE_NOT_FOUND; String gender = Translator.getLabel(lg, "exception.object." + objectKey + ".gender"); String text = Translator.getLabel(lg, "exception.notfound.not." + gender) + " " + Translator.getLabel(lg, "exception.object." + objectKey + ".singular") + " " + Translator.getLabel(lg, "exception.notfound.found." + gender) + "."; if (bug != null) Message.sendEmbed(message.getChannel(), bug.getEmbed(text, lg)); else Message.sendText(message.getChannel(), text); } }
@EventSubscriber public void onReady(MessageReceivedEvent event) { Language lg = Translator.getLanguageFrom(event.getChannel()); String prefixe = AbstractCommand.getPrefix(event.getMessage()); // If the authorId is a bot, message get ignored if (! event.getMessage().getAuthor().isBot()) { List<Command> commandsAvailable = new ArrayList<>(); for (Command command : CommandManager.getCommands()) if (event.getMessage().getContent().startsWith(prefixe + command.getName())) commandsAvailable.add(command); if (!commandsAvailable.isEmpty()){ commandsAvailable.sort((cmd1, cmd2) -> cmd2.getName().length() - cmd1.getName().length()); Command command = commandsAvailable.get(0); try { command.request(event.getMessage()); } catch (Exception e) { BasicDiscordException.UNKNOWN_ERROR.throwException(event.getMessage(), command, lg); Reporter.report(e, event.getGuild(), event.getChannel(), event.getAuthor(), event.getMessage()); LOG.error("onReady", e); } } } } }
String[] splitMessage = event.getMessage().getContent().split("[\\s&&[^\\n]]++"); String commandString = splitMessage[0]; SimpleCommand command = commands.get(commandString.toLowerCase());
/** * Rejette une exception dans un salon discord. Politique du "posté uniquement une fois" * @param exception Exception à rejeter * @param guild Guilde d'origine de l'erreur * @param channel Salon d'origine de l'erreur * @param user Auteur du message d'origine de l'erreur * @param message Contenu du message à l'origine de l'erreur */ public void send(Exception exception, IGuild guild, IChannel channel, IUser user, IMessage message){ try { Sentry.getContext().clearTags(); if (guild != null) Sentry.getContext().addTag(GUILD, guild.getStringID() + " - " + guild.getName()); if (channel != null) Sentry.getContext().addTag(CHANNEL, channel.getStringID() + " - " + channel.getName()); if (user != null) Sentry.getContext().addTag(USER, user.getStringID() + " - " + user.getName()); if (message != null) Sentry.getContext().addTag(MESSAGE, message.getContent()); Sentry.capture(exception); } catch(Exception e){ Sentry.capture(exception); LOG.error("report", exception); Sentry.capture(e); LOG.error("report", e); } } }
private void messageUpdate(MessageObject json) { Channel channel = (Channel) client.getChannelByID(Long.parseUnsignedLong(json.channel_id)); if (channel == null) return; IMessage toUpdate = channel.messages.get(json.id); if (toUpdate == null) { // Cannot resolve update type. MessageObject is incomplete, so we'll have to request for the full message. if (channel.isPrivate() || PermissionUtils.hasHierarchicalPermissions(channel, client.ourUser, channel.getGuild().getRolesForUser(client.ourUser), Permissions.READ_MESSAGE_HISTORY)) client.dispatcher.dispatch(new MessageUpdateEvent(null, channel.fetchMessage(Long.parseUnsignedLong(json.id)))); // else //FIXME: unable to fire message update events when the user doesn't have the read message history permission } else { IMessage oldMessage = toUpdate.copy(); IMessage updatedMessage = DiscordUtils.getUpdatedMessageFromJSON(client, toUpdate, json); if (json.pinned != null && oldMessage.isPinned() && !json.pinned) { client.dispatcher.dispatch(new MessageUnpinEvent(oldMessage, updatedMessage)); } else if (json.pinned != null && !oldMessage.isPinned() && json.pinned) { client.dispatcher.dispatch(new MessagePinEvent(oldMessage, updatedMessage)); } else if (oldMessage.getEmbeds().size() < updatedMessage.getEmbeds().size()) { client.dispatcher.dispatch(new MessageEmbedEvent(oldMessage, updatedMessage, oldMessage.getEmbeds())); } else if (json.content != null && !oldMessage.getContent().equals(json.content)) { client.dispatcher.dispatch(new MessageEditEvent(oldMessage, updatedMessage)); } else { client.dispatcher.dispatch(new MessageUpdateEvent(oldMessage, updatedMessage)); } } }
else if (message.getContent().startsWith(getPrefix(message) + getName())) { badUse.throwException(message, this, lg); return;