'Discord bot does not send any messages out
I have made discord bot integration in java.
But I'm getting an indexoutofboundexception and I'm not sure where to go from there.
WARN: Error executing task: MessageEvent [pendingQueue: 0 executionQueue: 5],
[m java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_181]
at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_181]
at com.osroyale.util.Utility.randomElement(Utility.java:248) ~[bin/:?]
And the randomElement class
public static <T> T randomElement(Collection<T> collection) {
return new ArrayList<T>(collection).get((int) (RANDOM.nextDouble() * collection.size()));
}
Any ideas?
Edit: Added two classes related to the discord messages.
DiscordDispatcher class
@EventSubscriber
public void onMessageReceivedEvent(MessageReceivedEvent event) throws DiscordException, MissingPermissionsException {
IMessage message = event.getMessage();
long channel = event.getChannel().getLongID();
String name = event.getAuthor().getName();
String command = message.getContent();
if (Discord.community == null && channel == Discord.COMMUNITY_CHANNEL) {
Discord.community = event.getChannel();
}
if (command.equals("::players")) {
Discord.communityMessage("There are currently " + World.getPlayerCount() + " players online!");
} else if (command.equals("::staffonline")) {
Discord.communityMessage("There are currently " + World.getStaffCount() + " staff online!");
}
}
}
And my Discord class
public class Discord {
/** The community channel identification. */
public static final long COMMUNITY_CHANNEL = 232389411260596224L;
/** The client builder. */
public static ClientBuilder builder;
/** The discord client. */
public static IDiscordClient client;
/** The community channel instance. */
static IChannel community;
/** Handles starting the discord bot. */
public static void start() {
if (!Config.LIVE_SERVER) {
return;
}
builder = new ClientBuilder().withToken(Config.DISCORD_TOKEN);
client = builder.login();
client.getDispatcher().registerListener(new DiscordDispatcher());
}
public static void communityMessage(String message) {
if (community != null)
community.sendMessage(message);
}
Solution 1:[1]
I'm not sure about the library you use for discord, I'm using JDA so I'm giving you the solution, which is you have to add GatewayIntent.GUILD_MESSAGES when you create your bot and add the token it will be someone like that
Example :
JDABuilder jda = JDABuilder.create(token, GatewayIntent.GUILD_MESSAGES);
hopefully, this is the case
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | omar ibrahim |
