简体   繁体   中英

java - ArrayList issue

package me.mizoo.bot;

import javax.security.auth.login.LoginException;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import net.dv8tion.jda.core.hooks.ListenerAdapter;

import java.util.ArrayList;

public class Main extends ListenerAdapter
{
public static void main( String[] args ) throws LoginException, IllegalArgumentException, InterruptedException, RateLimitedException
{

    //Initializes the bot
    JDA jdaBot = new JDABuilder(AccountType.BOT).setToken("**********************************").buildBlocking();
    jdaBot.addEventListener(new Main());

}

@Override
public void onMessageReceived(MessageReceivedEvent e)
{
    ArrayList<String> adminsOn = new ArrayList<String>();

    Message msg = e.getMessage();
    MessageChannel channel = e.getChannel();
    User user = e.getAuthor();

    if(msg.getContent().equalsIgnoreCase("$.readytoban"))
    {
        String name = msg.getAuthor().getName();
        adminsOn.add(name);
        channel.sendMessage("You're added to the list of admins ready to BAN!").queue();

    }

    else if(msg.getContent().equalsIgnoreCase("$.readytoleave"))
    {
        String name = msg.getAuthor().getName();
        adminsOn.remove(name);
        channel.sendMessage("You're off it, pal. Bye!").queue();
    }


    if (msg.getContent().equalsIgnoreCase(".$adminson"))
    {
        channel.sendMessage("Here are the admins on: " + adminsOn).queue();
    }

    }
}

This is my Java code, using the JDA library. I want to make it so that whenever they type the readytoban command, they're added to this list of admins on, and readytoleave removes them. This runs fine, but it doesn't work. I type the readytoban command and then adminson, and it doesn't show any names inside the array. Fixes?

Your problem is that you are initializing a new list every time the event is fired. You need to set the list as a field instead:

private ArrayList<String> adminsOn = new ArrayList<String>();
@Override
public void onMessageReceived(MessageReceivedEvent e)
{
    Message msg = e.getMessage();
    MessageChannel channel = e.getChannel();
    User user = e.getAuthor();

    if(msg.getContent().equalsIgnoreCase("$.readytoban"))
    {
        String name = msg.getAuthor().getName();
        adminsOn.add(name);
        channel.sendMessage("You're added to the list of admins ready to BAN!").queue();

    }

    else if(msg.getContent().equalsIgnoreCase("$.readytoleave"))
    {
        String name = msg.getAuthor().getName();
        adminsOn.remove(name);
        channel.sendMessage("You're off it, pal. Bye!").queue();
    }


    if (msg.getContent().equalsIgnoreCase(".$adminson"))
    {
        channel.sendMessage("Here are the admins on: " + adminsOn).queue();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM