@Override public Message sendMessage(Message msg) { checkVerification(); SelfInfo self = getJDA().getSelfInfo(); if (!checkPermission(self, Permission.MESSAGE_WRITE)) throw new PermissionException(Permission.MESSAGE_WRITE); JDAImpl api = (JDAImpl) getJDA(); if (api.getMessageLimit(guild.getId()) != null) { throw new RateLimitedException( api.getMessageLimit(guild.getId()) - System.currentTimeMillis()); } try { Requester.Response response = api.getRequester() .post( Requester.DISCORD_API_PREFIX + "channels/" + getId() + "/messages", new JSONObject().put("content", msg.getRawContent()).put("tts", msg.isTTS())); if (response.isRateLimit()) { long retry_after = response.getObject().getLong("retry_after"); api.setMessageTimeout(guild.getId(), retry_after); throw new RateLimitedException(retry_after); } if (!response.isOk()) // sending failed (Verification-level?) return null; return new EntityBuilder(api).createMessage(response.getObject()); } catch (JSONException ex) { JDAImpl.LOG.log(ex); // sending failed return null; } }
@Override public Message sendFile(File file, Message message) { checkVerification(); if (!checkPermission(getJDA().getSelfInfo(), Permission.MESSAGE_WRITE)) throw new PermissionException(Permission.MESSAGE_WRITE); if (!checkPermission(getJDA().getSelfInfo(), Permission.MESSAGE_ATTACH_FILES)) throw new PermissionException(Permission.MESSAGE_ATTACH_FILES); if (file == null || !file.exists() || !file.canRead()) throw new IllegalArgumentException( "Provided file is either null, doesn't exist or is not readable!"); if (file.length() > 8 << 20) // 8MB throw new IllegalArgumentException("File is to big! Max file-size is 8MB"); JDAImpl api = (JDAImpl) getJDA(); try { MultipartBody body = Unirest.post(Requester.DISCORD_API_PREFIX + "channels/" + getId() + "/messages") .header("authorization", getJDA().getAuthToken()) .header("user-agent", Requester.USER_AGENT) .field("file", file); if (message != null) body.field("content", message.getRawContent()).field("tts", message.isTTS()); String dbg = String.format( "Requesting %s -> %s\n\tPayload: file: %s, message: %s, tts: %s\n\tResponse: ", body.getHttpRequest().getHttpMethod().name(), body.getHttpRequest().getUrl(), file.getAbsolutePath(), message == null ? "null" : message.getRawContent(), message == null ? "N/A" : message.isTTS()); HttpResponse<JsonNode> response = body.asJson(); Requester.LOG.trace(dbg + body); try { int status = response.getStatus(); if (status >= 200 && status < 300) { return new EntityBuilder(api).createMessage(response.getBody().getObject()); } else if (response.getStatus() == 429) { long retryAfter = response.getBody().getObject().getLong("retry_after"); api.setMessageTimeout(guild.getId(), retryAfter); throw new RateLimitedException(retryAfter); } else { throw new RuntimeException( "An unknown status code was returned when attempting to upload file. Status: " + status + " JSON: " + response.getBody().toString()); } } catch (JSONException e) { Requester.LOG.fatal("Following json caused an exception: " + response.getBody().toString()); Requester.LOG.log(e); } } catch (UnirestException e) { Requester.LOG.log(e); } return null; }
@Override public int getPosition() { List<TextChannel> channels = guild.getTextChannels(); for (int i = 0; i < channels.size(); i++) { if (channels.get(i) == this) return i; } throw new RuntimeException( "Somehow when determining position we never found the TextChannel in the Guild's channels? wtf?"); }
@Override public void sendMessageAsync(Message msg, Consumer<Message> callback) { checkVerification(); SelfInfo self = getJDA().getSelfInfo(); if (!checkPermission(self, Permission.MESSAGE_WRITE)) throw new PermissionException(Permission.MESSAGE_WRITE); ((MessageImpl) msg).setChannelId(id); AsyncMessageSender.getInstance(getJDA(), guild.getId()).enqueue(msg, false, callback); }
public void chat(Player p, String[] args) { if (p.hasPermission("guilds.chat")) { String msg = null; if (args.length > 0) { msg = ""; for (String a : args) { msg = msg + "" + a; } } else { plugin.util.msg(p, "missing parameters..."); } Guild g = plugin.util.getGuild(p); msg = g.getChatPrefix() + msg; plugin.log("Chat.java guild chat prefix... " + g.getChatPrefix()); msg = msg.replaceAll("&([0-9a-fk-or])", "\u00A7$1"); // ADD COLOR if (!plugin.onlinePlayers.isEmpty()) { for (Map.Entry<Player, Guild> players : plugin.onlinePlayers.entrySet()) { if (players.getValue().equals(g)) { if (!players.getKey().equals(p)) { players.getKey().sendMessage(msg); } } } } } else { plugin.util.msg(p, "you do not have permissions..."); } }
public static String parseMPtoGuild(int GuildID) { Guild G = World.getGuild(GuildID); byte enclosMax = (byte) Math.floor(G.get_lvl() / 10); StringBuilder packet = new StringBuilder(); packet.append(enclosMax); for (Entry<Short, Carte.MountPark> mp : MountPark.entrySet()) { if (mp.getValue().get_guild() != null && mp.getValue().get_guild().get_id() == GuildID) { packet .append("|") .append(mp.getValue().get_map().get_id()) .append(";") .append(mp.getValue().get_size()) .append(";") .append(mp.getValue().getObjectNumb()); // Nombre d'objets pour le dernier } else { continue; } } return packet.toString(); }
public static boolean guildEmblemIsUsed(String emb) { for (Guild g : Guildes.values()) { if (g.get_emblem().equals(emb)) return true; } return false; }
public static boolean guildNameIsUsed(String name) { for (Guild g : Guildes.values()) if (g.get_name().equalsIgnoreCase(name)) return true; return false; }
public static void addGuild(Guild g, boolean save) { Guildes.put(g.get_id(), g); if (save) SQLManager.SAVE_NEWGUILD(g); }
@Override public JDA getJDA() { return guild.getJDA(); }
private void checkVerification() { if (!guild.checkVerification()) { throw new VerificationLevelException(guild.getVerificationLevel()); } }