Search in sources :

Example 1 with UnknownDiscordUser

use of com.gamebuster19901.excite.bot.user.UnknownDiscordUser in project ExciteBot by TheGameCommunity.

the class Player method ban.

@SuppressWarnings({ "rawtypes" })
public Ban ban(MessageContext context, Duration duration, String reason) {
    Ban ban = Ban.addBan(context, this, reason, duration);
    DiscordUser discord = DiscordUser.getDiscordUserIncludingUnknown(context, getDiscord());
    if (!(discord instanceof UnknownDiscordUser)) {
        discord.sendMessage(context, toString() + " " + reason);
    }
    return ban;
}
Also used : DiscordUser(com.gamebuster19901.excite.bot.user.DiscordUser) UnknownDiscordUser(com.gamebuster19901.excite.bot.user.UnknownDiscordUser) UnknownDiscordUser(com.gamebuster19901.excite.bot.user.UnknownDiscordUser) Ban(com.gamebuster19901.excite.bot.audit.ban.Ban)

Example 2 with UnknownDiscordUser

use of com.gamebuster19901.excite.bot.user.UnknownDiscordUser in project ExciteBot by TheGameCommunity.

the class Mailbox method analyzeMail.

private static LinkedHashSet<MailResponse> analyzeMail(Wii responder, MimeMessage prompt) throws MessagingException {
    LinkedHashSet<MailResponse> responses = new LinkedHashSet<MailResponse>();
    Session session = Session.getInstance(new Properties());
    Address[] from = prompt.getFrom();
    LOGGER.log(Level.FINEST, "Analyzing mail from: " + (from != null ? from[0] : from));
    if (from == null) {
        responses.add(new NoResponse(prompt));
        return responses;
    }
    ElectronicAddress senderEmail = new EmailAddress(prompt.getFrom()[0]);
    Wii sender = Wii.getWii(senderEmail);
    if (sender instanceof InvalidWii) {
        LOGGER.log(Level.FINEST, "Ignoring non-wii mail");
        responses.add(new NoResponse(prompt));
        return responses;
    } else {
        boolean wasKnown;
        if (!(wasKnown = sender.isKnown())) {
            try {
                Insertion.insertInto(WIIS).setColumns(WII_ID).to(sender.getWiiCode().toString()).prepare(ConsoleContext.INSTANCE).execute();
            } catch (SQLException e) {
                throw new MessagingException("Database error", e);
            }
        }
        String[] appheaders = prompt.getHeader(APP_ID_HEADER);
        String app = "";
        if (appheaders.length > 0) {
            app = appheaders[0];
        }
        Rewardable attachment = InvalidChallenge.INSTANCE;
        if (app.equals(EXCITEBOTS)) {
            attachment = analyzeIngameMail(prompt, sender);
        }
        if (sender.getOwner() instanceof UnknownDiscordUser) {
            // if wii is not registered
            if (app.equals(FRIEND_REQUEST) && !wasKnown) {
                MailResponse friendResponse = new AddFriendResponse(responder, sender, prompt);
                LOGGER.finest("Sending friend request to " + sender.getEmail());
                MailResponse codeResponse = new DiscordCodeResponse(responder, sender, prompt);
                LOGGER.finest("Sending verification discord code to " + sender.getEmail());
                responses.add(friendResponse);
                responses.add(codeResponse);
            }
            if (attachment.getReward() > 0) {
            // responses.add(new RefundResponse(responder, prompt, attachment));
            }
        } else {
            // excitebot is not currently accepting mail from anything other than Excitebots
            LOGGER.log(Level.FINEST, "Excitebot is not currently accepting mail from anything other than Excitebots");
            responses.add(new NoResponse(prompt));
        }
    }
    return responses;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InvalidWii(com.gamebuster19901.excite.bot.user.Wii.InvalidWii) Address(javax.mail.Address) SQLException(java.sql.SQLException) MessagingException(javax.mail.MessagingException) UnknownDiscordUser(com.gamebuster19901.excite.bot.user.UnknownDiscordUser) Properties(java.util.Properties) Rewardable(com.gamebuster19901.excite.game.challenge.Rewardable) InvalidWii(com.gamebuster19901.excite.bot.user.Wii.InvalidWii) Wii(com.gamebuster19901.excite.bot.user.Wii) Session(javax.mail.Session)

Example 3 with UnknownDiscordUser

use of com.gamebuster19901.excite.bot.user.UnknownDiscordUser in project ExciteBot by TheGameCommunity.

the class BanCommand method banDiscordUserForever.

@SuppressWarnings("rawtypes")
private static int banDiscordUserForever(MessageContext context, DiscordUser user, String reason) {
    if (context.isAdmin()) {
        if (user instanceof UnknownDiscordUser && !((UnknownDiscordUser) user).hasID()) {
            context.sendMessage("There is no discord user known as " + user);
            return 0;
        }
        Duration duration = ChronoUnit.FOREVER.getDuration();
        user.ban(context, duration, parseReason(duration, reason));
    } else {
        context.sendMessage("You do not have permission to execute this command");
    }
    return 0;
}
Also used : UnknownDiscordUser(com.gamebuster19901.excite.bot.user.UnknownDiscordUser) Duration(java.time.Duration)

Example 4 with UnknownDiscordUser

use of com.gamebuster19901.excite.bot.user.UnknownDiscordUser in project ExciteBot by TheGameCommunity.

the class Main method main.

@SuppressWarnings("rawtypes")
public static void main(String[] args) throws InterruptedException, ClassNotFoundException, IOException, SQLException {
    if (args.length % 2 != 0) {
        throw new IllegalArgumentException("Must be started with an even number of arguments!");
    }
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-owner")) {
            botOwner = Long.parseLong(args[++i]);
        }
    }
    wiimmfi = startWiimmfi(args);
    discordBot = null;
    int bootAttempts = 0;
    while (discordBot == null) {
        try {
            bootAttempts++;
            discordBot = startDiscordBot(args, wiimmfi);
            discordBot.setLoading();
            discordBot.setWiimmfi(wiimmfi);
        } catch (LoginException | IOException | ErrorResponseException e) {
            LOGGER.log(Level.SEVERE, e, () -> e.getMessage());
            if (bootAttempts >= 3) {
                System.exit(-2);
            }
            Thread.sleep(5000);
        }
    }
    while (CONSOLE == null) {
        try {
            CONSOLE = new ConsoleUser();
        } catch (IOError e) {
            System.out.println(e);
            discordBot.setNoDB();
            Thread.sleep(5000);
        }
    }
    do {
        try {
            DatabaseConnection.INSTANCE = new DatabaseConnection();
        } catch (Throwable t) {
            System.out.println(t);
            discordBot.setNoDB();
            Thread.sleep(5000);
        }
    } while (DatabaseConnection.INSTANCE == null);
    Throwable prevError = null;
    Instant nextDiscordPing = Instant.now().minusMillis(1);
    Instant sendDiscordNotifications = Instant.now().minusMillis(1);
    startConsole();
    Thread mailThread = startMailThread();
    try {
        while (true) {
            try {
                System.gc();
                Throwable error = wiimmfi.getError();
                wiimmfi.update();
                if (error == null) {
                    if (prevError != null) {
                    // LOGGER.log(Level.SEVERE, "Error resolved.");
                    }
                } else {
                    if (prevError == null || !prevError.getClass().equals(error.getClass())) {
                        System.out.println("Error!");
                        LOGGER.log(Level.SEVERE, error, () -> error.getMessage());
                    }
                    prevError = error;
                }
                if (discordBot != null) {
                    if (nextDiscordPing.isBefore(Instant.now())) {
                        nextDiscordPing = Instant.now().plus(Duration.ofSeconds(5));
                        updateLists(true, true);
                    }
                    if (sendDiscordNotifications.isBefore(Instant.now())) {
                        sendDiscordNotifications = Instant.now().plus(Duration.ofSeconds(4));
                        DiscordUser.notifyDiscordUsers();
                    }
                }
                while (!consoleCommandsAwaitingProcessing.isEmpty()) {
                    Commands.DISPATCHER.handleCommand(consoleCommandsAwaitingProcessing.pollFirst());
                }
            } catch (ErrorResponseException e) {
                CONSOLE.sendMessage(StacktraceUtil.getStackTrace(e));
                CONSOLE.sendMessage("An ErrorResponseException occurred... waiting 10 seconds");
            } catch (Throwable t) {
                if (t != null && (t instanceof ConnectionIsClosedException || t instanceof CommunicationsException || t instanceof CJCommunicationsException || t instanceof SQLNonTransientConnectionException || (t.getCause() != null && (t.getCause() instanceof IOException || t.getCause() instanceof SQLException || t.getCause() instanceof IOError)))) {
                    System.err.println("Attempting to recover from database connection failure...");
                    DatabaseConnection.INSTANCE.close();
                    DatabaseConnection.INSTANCE = null;
                    while (DatabaseConnection.INSTANCE == null) {
                        discordBot.setNoDB();
                        try {
                            DatabaseConnection.INSTANCE = new DatabaseConnection();
                        } catch (Throwable t2) {
                            System.err.println("Attempting to recover from database connection failure...");
                            t2.printStackTrace(System.err);
                            DatabaseConnection.INSTANCE = null;
                        }
                        Thread.sleep(5000);
                    }
                    continue;
                }
                throw t;
            }
            if (!mailThread.isAlive() && !stopping) {
                throw new Error("Mail thread has died");
            }
            Thread.sleep(1000);
        }
    } catch (Throwable t) {
        t.printStackTrace();
        if (discordBot != null) {
            discordBot.jda.getPresence().setPresence(OnlineStatus.DO_NOT_DISTURB, Activity.of(ActivityType.DEFAULT, "Bot Crashed"));
            if (botOwner != -1) {
                try {
                    DiscordUser user = DiscordUser.getDiscordUserIncludingUnknown(ConsoleContext.INSTANCE, botOwner);
                    if (!(user instanceof UnknownDiscordUser)) {
                        user.sendMessage(StacktraceUtil.getStackTrace(t));
                    } else {
                        CONSOLE.sendMessage(StacktraceUtil.getStackTrace(t));
                    }
                } catch (Throwable t2) {
                    CONSOLE.sendMessage(StacktraceUtil.getStackTrace(t));
                }
            }
            while (true) {
                Thread.sleep(1000);
            }
        } else {
            CONSOLE.sendMessage(StacktraceUtil.getStackTrace(t));
        }
    }
    System.exit(-1);
}
Also used : DiscordUser(com.gamebuster19901.excite.bot.user.DiscordUser) UnknownDiscordUser(com.gamebuster19901.excite.bot.user.UnknownDiscordUser) SQLException(java.sql.SQLException) UnknownDiscordUser(com.gamebuster19901.excite.bot.user.UnknownDiscordUser) Instant(java.time.Instant) IOError(java.io.IOError) IOException(java.io.IOException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) IOError(java.io.IOError) LoginException(javax.security.auth.login.LoginException) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) ConsoleUser(com.gamebuster19901.excite.bot.user.ConsoleUser) DatabaseConnection(com.gamebuster19901.excite.bot.database.sql.DatabaseConnection) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) ConnectionIsClosedException(com.mysql.cj.exceptions.ConnectionIsClosedException)

Aggregations

UnknownDiscordUser (com.gamebuster19901.excite.bot.user.UnknownDiscordUser)4 DiscordUser (com.gamebuster19901.excite.bot.user.DiscordUser)2 SQLException (java.sql.SQLException)2 Ban (com.gamebuster19901.excite.bot.audit.ban.Ban)1 DatabaseConnection (com.gamebuster19901.excite.bot.database.sql.DatabaseConnection)1 ConsoleUser (com.gamebuster19901.excite.bot.user.ConsoleUser)1 Wii (com.gamebuster19901.excite.bot.user.Wii)1 InvalidWii (com.gamebuster19901.excite.bot.user.Wii.InvalidWii)1 Rewardable (com.gamebuster19901.excite.game.challenge.Rewardable)1 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)1 ConnectionIsClosedException (com.mysql.cj.exceptions.ConnectionIsClosedException)1 CommunicationsException (com.mysql.cj.jdbc.exceptions.CommunicationsException)1 IOError (java.io.IOError)1 IOException (java.io.IOException)1 SQLNonTransientConnectionException (java.sql.SQLNonTransientConnectionException)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 Address (javax.mail.Address)1