How to make a Discord Moderation bot in Java

Oliy Barrett
Jun 19, 2018 · 5 min read
Image for post
Image for post

Creating a simple moderation bot

This tutorial assumes you have basic knowledge on JDA. If you don’t know how to setup a project, please read this tutorial

Step 1

Create a new project and add JDA as a dependency (again, if you don’t know how refer to the tutorial above)

In this tutorial, we’ll be using JDA-Utilities to handle our commands, so be sure to add it as a depencency as well. Check the latest version here.

Our build.gradle now looks like this:

Step 2

Now, we’ll use JDA-Utilities’ CommandClient to handle our command processing.

This is what a basic command looks like:


All mod actions in JDA (such as banning, kicking, adding or removing roles, server muting, server deafening, etc) are done through the GuildController class.

To get the GuildController for a Guild, call it’s getController() method.


All JDA methods that interact with discord’s REST API return an instance of the RestAction class.

A RestAction represents a request that will be done. However, the request will be executed only when you call queue(), complete(), or submit() on the RestAction object. If you don't call one of those methods, the action will never be executed and your bot will not work.

Whenever possible, try using the queue() method and it's overloads, as they do not block) the current thread), so other operations aren't slowed down.


For now, let’s make a kick command. It’ll be executed as !!kick @Person, where Person is who you want to kick. For this command, we'll use the Message#getMentionedMembers() and GuildController#kick methods.

This is what our kick command looks like:

You might be wondering what the x->{} syntax is. If you don't know, they're lambda expressions, which are similar to javascript's closures or python's lambdas.


The way to ban is almost equal to the way you kick, but there’s one additional argument you must give to the GuildController#ban() method, which is how far the banned person's messages will be deleted.

The implementation of this command is left as an exercise for you.


A softban is basically a ban and unban, to kick and delete an user’s messages. Let’s implement one now by modifying the ban command:


To mute, we’ll need a Mute role. For now, we’ll just give the person the first role named Muted we find:

This example uses the java stream and optional APIs

Again, the actual command implementation is left as an exercise for the reader.


We’ll mute someone for 1 hour, then unmute. Let’s change our mute command:

This uses the RestAction#queueAfter method, which means our pending unmutes will be lost when the bot is restarted. To keep track of them even across restarts, we'd need to save them in a database (do not save data in json files, that always backfires), but that's out of the scope of this tutorial.

Curse Word Filter

To create our filter, we’ll need to add a new listener to our JDA object. Let’s create a new class and extend ListenerAdapter:

Now, let’s override the onGuildMessageReceived(GuildMessageReceivedEvent) method:

Let’s also add a curse word list:

Now, let’s see if there’s a curse word and delete the message if it has one:

If we run our bot now, we’ll notice it completely ignores curse words. That’s because we didn’t register the listener. Now let’s go back to our main class and change .addEventListener( to .addEventListener(, new CurseWorldFilter()) so our listener is registered.

Run the bot again and you’ll notice it’s now detecting curse words and deleting them.

Discord Bots

A blog for all information surrounding discord bots and our…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store