Gafarrell
Gafarrell

Reputation: 29

Discord bot unusually repeats

So I'm trying to create a discord bot that has simple access to a database for printing out values, my code currently will print the values to the discord server but it repeats them 5 times.

Bot functionality class:

private MySQLAccess sql = new MySQLAccess();

public static void main(String[] args) throws Exception {
    JDABuilder ark = new JDABuilder(AccountType.BOT);
    ark.setToken("insert_discord_token_here");
    ark.addEventListener(new MessageListener());
    ark.buildAsync();
}



@Override
public void onMessageReceived(MessageReceivedEvent e) {
    if (e.getAuthor().isBot()) return;
    Message msg = e.getMessage();
    String str = msg.getContentRaw();

    //Ping pong
    if (str.equalsIgnoreCase("!ping")) {
        e.getChannel().sendMessage("Pong!").queue();
    }

    //Bal check
    if (str.contains("!bal")) {
        String user = str.substring(5);
        System.out.println(user);
        try {
            sql.readDataBase(e.getChannel(), user);
        } catch (Exception e1) {
        }

    }
}

Database Access Class:

private Connection connect = null;
private Statement statement = null;
private ResultSet resultSet = null;
private final String user = "pass";
private final String pass = "user";


public void readDataBase(MessageChannel msg, String username) throws Exception {
    //Retrieve data and search for username
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");

        connect = DriverManager.getConnection("jdbc:mysql://localhost/serverusers?allowPublicKeyRetrieval=true&useSSL=false", user, pass);

        statement = connect.createStatement();
        resultSet = statement
                .executeQuery("select * from serverusers.userinfo where user=\"" + username + "\"");
        writeResultSet(resultSet, msg);

    } catch (Exception e) {
        throw e;
    } finally {
        close();
    }

}

private void writeResultSet(ResultSet resultSet, MessageChannel msg) throws SQLException {
    // Check resultSet and print its contents
    if (resultSet.next()) {
        String user = resultSet.getString(2);
        Double website = resultSet.getDouble(3);
        msg.sendMessage("User: " + user).queue();
        msg.sendMessage("Bank Amount: " + website).queue();
    }
}

private void close() {
    try {
        if (resultSet != null) {
            resultSet.close();
        }

        if (statement != null) {
            statement.close();
        }

        if (resultSet != null) {
            resultSet.close();
        }
        if (connect != null) {
            connect.close();
        }
    } catch (Exception e) {

    }
}

When the program is run it finds the correct data that I'm looking for and the search function is fine, but for some odd reason the program will spit the same username and balance out 5 times. Screenshot of Discord Bot

Upvotes: 1

Views: 945

Answers (1)

Minn
Minn

Reputation: 6131

The common mistake here is that you run the program multiple times, each instance then responds accordingly with the same thing. You can check if that is the case by opening the task manager and looking for java processes. This often occurs with developers using the Eclipse IDE because of the console hiding other processes behind a drop-down menu on the console.

Upvotes: 1

Related Questions