LApi is a Discord API written in Java
Achived: No further development.
In order to install it, you can either build it yourself or use gradle and implement it into your Project:
In your build.gradle
add mavenCentral()
to the repositories if you have not done so already and add the following two dependencies. Replace [version]
with the version you want to install.
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'io.github.lni-dev:lapi-annotation-processor:1.0.1'
implementation 'io.github.lni-dev:lapi:[version]'
}
An example build.gradle
could look like this:
plugins {
id 'java'
}
group 'com.example'
version 'your.version'
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'io.github.lni-dev:lapi-annotation-processor:1.0.1'
implementation 'io.github.lni-dev:lapi:1.0.4'
}
(Tested on gradle 7.5.1)
If you want to make an executable .jar file at some point in time, I recommend
the gradle plugins application
and shadow
. An example build.gradle
with these plugins could
look like this:
plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
group 'com.example'
version '1.0.0'
mainClassName = 'com.example.exampleProjectName.Main'
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'io.github.lni-dev:lapi-annotation-processor:1.0.1'
implementation 'io.github.lni-dev:lapi:1.0.4'
}
This will then add a gradle task called shadowJar, which will build an executable jar for you.
First you will need to create a Discord bot and copy it's TOKEN
.
Then you can create a Config:
Config config = ConfigBuilder.getDefault("TOKEN", true).build();
And create a LApi instance:
Config config = ConfigBuilder.getDefault("TOKEN", true).build();
LApi lApi = LApi.newInstance(config);
Or a lot simpler:
LApi lApi = ConfigBuilder.getDefault("TOKEN", true).buildLApi();
TOKEN
must be replaced with your bot token. The second boolean parameter specifies, whether
you want the privileged intents enabled. Read more here.
If you pass true
, you have to enable them for your application's bot here.
Now you can register an EventListener:
lApi.getEventTransmitter().addListener(new EventListener() {
@Override
public void onMessageCreate(@NotNull LApi lApi, @NotNull MessageCreateEvent event) {
//code
}
@Override
public void onMessageUpdate(@NotNull LApi lApi, @NotNull MessageUpdateEvent event) {
//code
}
@Override
public void onMessageDelete(@NotNull LApi lApi, @NotNull MessageDeleteEvent event) {
//code
}
});
Inside your listener, you can overwrite all events, you want to listen to.
Here a small example on how to respond to "Hi":
lApi.getEventTransmitter().addListener(new EventListener() {
@Override
public void onMessageCreate(@NotNull LApi lApi, @NotNull MessageCreateEvent event) {
System.out.println("Message: " + event.getMessage().getContent());
if(!event.getMessage().getAuthor().isBot() && event.getMessage().getContent().equals("Hi")){
lApi.getRequestFactory().createMessage(event.getChannelId(), "Hi").queue();
}
}
});
Note the check, if the message was sent by a bot. This check is important, so that your bot does not respond to itself.