Search in sources :

Example 11 with DiscordUser

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

the class Banee method getBanee.

@Nullable
@SuppressWarnings({ "rawtypes", "deprecation" })
public static Banee getBanee(MessageContext context, String name) {
    DiscordUser[] discords = DiscordUser.getDiscordUsersWithUsername(context, name);
    Player[] players = Player.getPlayersByName(context, name);
    int amount = discords.length + players.length;
    if (amount == 1) {
        if (discords.length == 1) {
            return discords[0];
        }
        return players[0];
    } else if (amount < 1) {
        context.sendMessage("Could not find a discord user or player named " + name);
    } else if (amount > 1) {
        String ret = name + " is ambiguous, please supply an ID instead.\n\nAmbiguities\n\n:";
        if (discords.length > 0) {
            ret = ret + "Discord Users:\n";
            for (DiscordUser discord : discords) {
                ret = ret + discord.toDetailedString();
            }
        }
        if (players.length > 0) {
            ret = ret + "Profiles:\n";
            for (Player player : players) {
                ret = ret + player.toFullString();
            }
        }
        context.sendMessage(ret);
    }
    return null;
}
Also used : DiscordUser(com.gamebuster19901.excite.bot.user.DiscordUser) Player(com.gamebuster19901.excite.Player) Nullable(javax.annotation.Nullable)

Example 12 with DiscordUser

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

the class BanCommand method banDiscordUser.

@SuppressWarnings("rawtypes")
private static int banDiscordUser(MessageContext context, DiscordUser user, Duration duration, String reason) {
    if (context.isAdmin()) {
        Ban ban = user.ban(context, duration, parseReason(duration, reason));
        String message = "Banned discord user" + user + ": \n\n" + ban;
        if (context.isConsoleMessage()) {
            context.sendMessage(message);
        } else {
            PrivateChannel privateChannel;
            if (context.isPrivateMessage()) {
                privateChannel = (PrivateChannel) context.getChannel();
            } else {
                privateChannel = context.getDiscordAuthor().getJDAUser().openPrivateChannel().complete();
            }
            privateChannel.sendMessage(message);
        }
    } else {
        context.sendMessage("You do not have permission to execute this command");
    }
    return 1;
}
Also used : PrivateChannel(net.dv8tion.jda.api.entities.PrivateChannel) Ban(com.gamebuster19901.excite.bot.audit.ban.Ban)

Example 13 with DiscordUser

use of com.gamebuster19901.excite.bot.user.DiscordUser 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 14 with DiscordUser

use of com.gamebuster19901.excite.bot.user.DiscordUser 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)

Example 15 with DiscordUser

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

the class Commands method handleCommand.

public void handleCommand(GuildMessageReceivedEvent e) {
    MessageContext<GuildMessageReceivedEvent> context = new MessageContext<GuildMessageReceivedEvent>(e);
    try {
        String message = e.getMessage().getContentRaw();
        String prefix = context.getServer().getPrefix();
        if (message.startsWith(prefix)) {
            message = StringUtils.replaceOnce(message, prefix, "");
            DiscordUser sender = DiscordUser.getDiscordUser(ConsoleContext.INSTANCE, e.getAuthor().getIdLong());
            if (!sender.isBanned()) {
                if (!sender.isBanned()) {
                    CommandAudit.addCommandAudit(context, message);
                    this.dispatcher.execute(message, context);
                }
            }
        }
    } catch (CommandSyntaxException ex) {
        if (ex.getMessage() != null && !ex.getMessage().startsWith("Unknown command at position")) {
            context.sendMessage(ex.getClass() + " " + ex.getMessage());
        }
    } catch (Throwable t) {
        if (t instanceof StackOverflowError) {
            context.sendMessage(t.getClass().getCanonicalName());
            throw t;
        }
        context.sendMessage(StacktraceUtil.getStackTrace(t));
        if (!context.isConsoleMessage()) {
            t.printStackTrace();
        }
        if (t instanceof Error) {
            throw t;
        }
    }
}
Also used : DiscordUser(com.gamebuster19901.excite.bot.user.DiscordUser) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Aggregations

DiscordUser (com.gamebuster19901.excite.bot.user.DiscordUser)8 SQLException (java.sql.SQLException)7 Player (com.gamebuster19901.excite.Player)5 Result (com.gamebuster19901.excite.bot.database.Result)5 IOError (java.io.IOError)5 PreparedStatement (com.gamebuster19901.excite.bot.database.sql.PreparedStatement)4 UnknownDiscordUser (com.gamebuster19901.excite.bot.user.UnknownDiscordUser)3 Ban (com.gamebuster19901.excite.bot.audit.ban.Ban)2 Comparison (com.gamebuster19901.excite.bot.database.Comparison)2 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)2 Duration (java.time.Duration)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ErrorResponseException (net.dv8tion.jda.api.exceptions.ErrorResponseException)2 Wiimmfi (com.gamebuster19901.excite.Wiimmfi)1 MessageContext (com.gamebuster19901.excite.bot.command.MessageContext)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