__2020 / 5/19 Addendum __ Here introduces how to make the latest bot.
__ 2019/10/27 Addendum __ It seems that the Discord 4J library handled on this page has been discontinued and replaced with another library. Therefore, it is very difficult or impossible to develop a bot by this method at present. Thank you very much for reading this as a reference when developing bots. If I have time, I would like to write an article about how to develop with the new Discord 4J.
This time, I will develop a simple DiscordBot using the Discord4j library published on GitHub.
To use Bot, you need to register the app on Discord and get a token, which is introduced in the extra edition at the bottom of the page.
...
<repositories>
...
<repository> <!-- This repo fixes issues with transitive dependencies -->
<id>jcenter</id>
<url>http://jcenter.bintray.com</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
...
<dependencies>
...
<dependency>
<groupId>com.github.austinv11</groupId>
<artifactId>Discord4J</artifactId>
<version>@VERSION@</version>
</dependency>
</dependencies>
...
...
repositories {
...
jcenter() //This prevents issues with transitive dependencies
maven {
url "https://jitpack.io"
}
}
...
dependencies {
...
compile "com.github.austinv11:Discord4J:@VERSION@"
}
...
https://austinv11.github.io/Discord4J/downloads.html
public static IDiscordClient login(String token) {
ClientBuilder clientBuilder = new ClientBuilder();
clientBuilder.withToken(token);
return clientBuilder.login();
}
To process an event, you must first register a listener. Please add the code below after logging in. Please put the instance of the class used for the listener in the argument.
In addition, this time we will introduce the registration of listeners using annotations.
client.getDispatcher().registerListener(Object listener);
@EventSubscriber
public void onReady(ReadyEvent event) {
System.out.println("The bot is ready!");
}
@EventSubscriber
public void onMessage(MessageReceivedEvent event) throws RateLimitException, DiscordException, MissingPermissionsException {
System.out.println(event.getAuthor().getName() + """ + event.getMessage().getContent() + "I posted.");
}
The onReady method will be called once the login is complete and the onMessage method will be called when the chat is posted to the channel.
You can pick up an event by adding the EventSubscriber annotation.
message.getAuthor() //IUser object of the user who posted
message.getMessage() //Posted chat IMessage object
message.getChannel() //IChannel object of the channel to which the posted bit belongs
//Such.
It is provided by the IChannel class.
IChannel#sendMessage(String);
IUser#getName()
IMessage#getMessage()
As this sample, I would like to make a Bot to answer if it is posted "Hello".
import sx.blah.discord.api.ClientBuilder;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.api.events.EventSubscriber;
import sx.blah.discord.handle.impl.events.ReadyEvent;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IUser;
import sx.blah.discord.util.DiscordException;
import sx.blah.discord.util.MissingPermissionsException;
import sx.blah.discord.util.RateLimitException;
public class Example {
private static String TOKEN = "TOKEN"; //Discord Bot token
private static IDiscordClient client;
public static void main(String args[]) {
Example main = new Example();
System.out.println("Starting Bot...");
client = new ClientBuilder().withToken(TOKEN).build();
client.getDispatcher().registerListener(main);
client.login();
}
@EventSubscriber
public void onReady(ReadyEvent event) {
System.out.println("The bot is ready!");
}
@EventSubscriber
public void onMessage(MessageReceivedEvent event) throws RateLimitException, DiscordException, MissingPermissionsException {
IMessage message = event.getMessage();
IUser user = message.getAuthor();
if (user.isBot()) return;
IChannel channel = message.getChannel();
String mes = message.getContent();
if (mes.contains("Hello")) {
channel.sendMessage("Hello," + user.getName() + "San!");
}
}
}
Go to https: //discordapp.com/developers/applications/me
Click New App
Enter an appropriate name in APP NAME and click Create App
Make a note of the tokens in the red frame (will be used later)
Scroll a little to find this and click Create a Bot User
After Yes, do it !, click click to reveal and make a note of the token that appears. (Check also for Public Bot)
Replace [CLIENT_ID] in the URL below with the Client Id you wrote down in step 4 to access.
https://discordapp.com/api/oauth2/authorize?client_id=[CLIENT_ID]&permissions=0&scope=bot
Recommended Posts