Search in sources :

Example 1 with ErrorResponseException

use of net.dv8tion.jda.core.exceptions.ErrorResponseException in project FlareBot by FlareBot.

the class FlareBot method init.

public void init() throws InterruptedException {
    LOGGER.info("Starting init!");
    manager = new FlareBotManager();
    Runtime.getRuntime().addShutdownHook(new Thread(this::stop));
    Metrics.setup();
    RestAction.DEFAULT_FAILURE = t -> {
        if (t instanceof ErrorResponseException) {
            ErrorResponseException e = (ErrorResponseException) t;
            Metrics.failedRestActions.labels(String.valueOf(e.getErrorCode())).inc();
            if (// Socket timeout
            e.getErrorCode() == -1)
                return;
        }
        LOGGER.warn("Failed RestAction", t);
    };
    events = new Events(this);
    LOGGER.info("Starting builders");
    try {
        shardManager = new DefaultShardManagerBuilder().addEventListeners(events).addEventListeners(new ModlogEvents()).addEventListeners(Metrics.instance().jdaEventMetricsListener).addEventListeners(new NINOListener()).setToken(config.getString("bot.token").get()).setAudioSendFactory(new NativeAudioSendFactory()).setWebsocketFactory(new WebSocketFactory(new WebSocketListener())).setShardsTotal(-1).setHttpClientBuilder(client.newBuilder()).setBulkDeleteSplittingEnabled(false).build();
        commandManager = new CommandManager();
    } catch (Exception e) {
        LOGGER.error("Could not log in!", e);
        Thread.sleep(500);
        System.exit(1);
        return;
    }
    System.setErr(new PrintStream(new OutputStream() {

        // Nothing really so all good.
        @Override
        public void write(int b) {
        }
    }));
    // No operation STDERR. Will not do much of anything, except to filter out some Jsoup spam
    manager = new FlareBotManager();
    manager.executeCreations();
}
Also used : WebSocketListener(stream.flarebot.flarebot.ws.WebSocketListener) PrintStream(java.io.PrintStream) NativeAudioSendFactory(com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) ErrorResponseException(net.dv8tion.jda.core.exceptions.ErrorResponseException) IOException(java.io.IOException) UnknownBindingException(com.arsenarsen.lavaplayerbridge.libraries.UnknownBindingException) DefaultShardManagerBuilder(net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder) WebSocketFactory(stream.flarebot.flarebot.ws.WebSocketFactory) ErrorResponseException(net.dv8tion.jda.core.exceptions.ErrorResponseException) NINOListener(stream.flarebot.flarebot.mod.nino.NINOListener)

Example 2 with ErrorResponseException

use of net.dv8tion.jda.core.exceptions.ErrorResponseException in project FlareBot by FlareBot.

the class GeneralUtils method sendImage.

/**
 * This will download and cache the image if not found already!
 *
 * @param fileUrl  Url to download the image from.
 * @param fileName Name of the image file.
 * @param user     User to send the image to.
 */
public static void sendImage(String fileUrl, String fileName, User user) {
    try {
        File dir = new File("imgs");
        if (!dir.exists() && !dir.mkdir())
            throw new IllegalStateException("Cannot create 'imgs' folder!");
        File image = new File("imgs" + File.separator + fileName);
        if (!image.exists() && image.createNewFile()) {
            URL url = new URL(fileUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 FlareBot");
            InputStream is = conn.getInputStream();
            OutputStream os = new FileOutputStream(image);
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
            is.close();
            os.close();
        }
        user.openPrivateChannel().complete().sendFile(image, fileName, null).queue();
    } catch (IOException | ErrorResponseException e) {
        FlareBot.LOGGER.error("Unable to send image '" + fileName + "'", e);
    }
}
Also used : ErrorResponseException(net.dv8tion.jda.core.exceptions.ErrorResponseException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

ErrorResponseException (net.dv8tion.jda.core.exceptions.ErrorResponseException)2 UnknownBindingException (com.arsenarsen.lavaplayerbridge.libraries.UnknownBindingException)1 NativeAudioSendFactory (com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 URL (java.net.URL)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 DefaultShardManagerBuilder (net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder)1 NINOListener (stream.flarebot.flarebot.mod.nino.NINOListener)1 WebSocketFactory (stream.flarebot.flarebot.ws.WebSocketFactory)1 WebSocketListener (stream.flarebot.flarebot.ws.WebSocketListener)1