public static void manageException(Exception e, IMessage message, Command command, Language lg){ Reporter.report(e, message.getGuild(), message.getChannel(), message.getAuthor(), message); LOG.error("manageException", e); BasicDiscordException.UNKNOWN_ERROR.throwException(message, command, lg); }
@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()); } } }
/** * Creates a copy of the collection made up of copies of the individual messages in the collection. * * @return A copy of the collection made up of copies of the individual messages in the collection. */ public MessageHistory deepCopy() { IMessage[] copied = new IMessage[backing.length]; for (int i = 0; i < backing.length; i++) copied[i] = backing[i].copy(); return new MessageHistory(copied); }
public MessageEvent(IMessage message) { super(message.getChannel()); this.message = message; this.messageID = message.getLongID(); }
public MessageTokenizer(IMessage message) { this(message.getClient(), message.getContent()); }
protected String getPrefixMdEscaped(IMessage message){ if (! message.getChannel().isPrivate()) return Guild.getGuild(message.getGuild()).getPrefix() .replaceAll("\\*", "\\\\*") // Italic & Bold .replaceAll("_", "\\_") // Underline .replaceAll("~", "\\~") // Strike .replaceAll("\\`", "\\\\`"); // Code return ""; }
/** * Gets the channel the messages were sent in, or null if no messages are present. * * @return The channel the messages were sent in, or null if no messages are present. */ public IChannel getChannel() { return (backing.length == 0) ? null : backing[0].getChannel(); }
IUser user = message.getAuthor(); ServerDofus server = Guild.getGuild(message.getGuild()).getServerDofus(); List<ServerDofus> servers; if (message.getMentions().isEmpty()){ BasicDiscordException.USER_NEEDED.throwException(message, this, lg); return; user = message.getMentions().get(0); List<EmbedObject> embeds = JobUser.getJobsFromUser(user, server, message.getGuild(), lg); for(EmbedObject embed : embeds) Message.sendEmbed(message.getChannel(), embed); if (user == message.getAuthor()) { Message.sendText(message.getChannel(), Translator.getLabel(lg, "job.noone")); return; Message.sendText(message.getChannel(), st.toString()); } else badUse.throwException(message, this, lg); Message.sendText(message.getChannel(), Translator.getLabel(lg, "job.noone")); return; List<EmbedObject> embeds = JobUser.getJobsFromFilters(message.getGuild().getUsers(), server, jobs, level, message.getGuild(), lg);
@Override public final void request(IMessage message) { try { Language lg = Translator.getLanguageFrom(message.getChannel()); Matcher m = getMatcher(message); boolean isFound = m.find(); if ((!isPublic() || isAdmin()) && message.getAuthor().getLongID() != Constants.authorId) return; if (!isUsableInMP() && message.getChannel().isPrivate()) { BasicDiscordException.NOT_USABLE_IN_MP.throwException(message, this, lg); return; else if (!message.getChannel().isPrivate() && message.getAuthor().getLongID() != Constants.authorId && isForbidden(Guild.getGuild(message.getGuild()))) { BasicDiscordException.COMMAND_FORBIDDEN.throwException(message, this, lg); return; else if (message.getContent().startsWith(getPrefix(message) + getName())) { badUse.throwException(message, this, lg); return; Reporter.report(e, message.getGuild(), message.getChannel(), message.getAuthor(), message); LOG.error("request", e);
String[] splitMessage = event.getMessage().getContent().split("[\\s&&[^\\n]]++"); String commandString = splitMessage[0]; SimpleCommand command = commands.get(commandString.toLowerCase()); if (event.getMessage().getChannel().isPrivate() && !commandAnnotation.privateMessages()) { return; if (!event.getMessage().getChannel().isPrivate() && !commandAnnotation.channelMessages()) { return; if (!hasPermission(event.getMessage().getAuthor(), commandAnnotation.requiredPermissions())) { if (Sdcf4jMessage.MISSING_PERMISSIONS.getMessage() != null) { try { event.getMessage().getChannel().sendMessage(Sdcf4jMessage.MISSING_PERMISSIONS.getMessage()); } catch (MissingPermissionsException | RateLimitException | DiscordException ignored) { }
@Override public void throwException(IMessage message, Command command, Language lg, Object... arguments) { Message.sendText(message.getChannel(), Translator.getLabel(lg, "exception.bad_use_command") .replace("{author}", Matcher.quoteReplacement(message.getAuthor().toString())) .replaceAll("\\{prefix}", Matcher.quoteReplacement(AbstractCommand.getPrefix(message))) .replaceAll("\\{cmd.name}", command.getName()) .replace("{HelpCmd.name}", HelpCommand.NAME)); } }
Discord4J.LOGGER.debug(LogMarkers.MESSAGES, "Message from: {} ({}) in channel ID {}: {}", message.getAuthor().getName(), json.author.id, json.channel_id, json.content); if (message.getAuthor().equals(client.getOurUser())) { client.dispatcher.dispatch(new MessageSendEvent(message)); message.getChannel().setTypingStatus(false); //Messages being sent should stop the bot from typing } else { client.dispatcher.dispatch(new MessageReceivedEvent(message)); if (!message.getEmbeds().isEmpty()) { client.dispatcher.dispatch(new MessageEmbedEvent(null, message, new ArrayList<>()));
@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); } }
@Override public void pin(IMessage message) { PermissionUtils.requirePermissions(this, client.getOurUser(), Permissions.MANAGE_MESSAGES); if (!message.getChannel().equals(this)) throw new DiscordException("Message channel doesn't match current channel!"); if (message.isPinned()) throw new DiscordException("Message already pinned!"); ((DiscordClientImpl) client).REQUESTS.PUT.makeRequest(DiscordEndpoints.CHANNELS + id + "/pins/" + message.getStringID()); }
@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); } } } } }
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)); } } }
@Override public List<IUser> getUsers() { List<IUser> users = new ArrayList<>(); String emoji = getEmoji().isUnicode() ? getEmoji().getName() : getEmoji().getName() + ":" + getEmoji().getStringID(); String endpoint = String.format(DiscordEndpoints.REACTIONS_USER_LIST, getMessage().getChannel().getStringID(), getMessage().getStringID(), emoji); String after = "0"; while (users.size() < count) { UserObject[] json = ((DiscordClientImpl) getClient()).REQUESTS.GET.makeRequest( endpoint + "?after=" + after + "&limit=100", UserObject[].class); for (UserObject obj : json) { users.add(getMessage().getShard().getUserByID(Long.parseUnsignedLong(obj.id))); } // Temporary measure so a refactor can be applied later. if (json.length == 0) break; after = json[json.length - 1].id; } return users; }
public static long getRole(String toLookFor, IMessage m) { return getRole(toLookFor, m.getGuild()); }
@Override public Matcher getMatcher(IMessage message){ String prefixe = getPrefix(message); return Pattern.compile("^" + Pattern.quote(prefixe) + name + pattern + "$").matcher(message.getContent()); }