Haiku no. 3

Lonely midnight heart: behind the silent mirror, blush of fire and wine.. “Haiku no. 3” is published by Sarah Hashemi in Poets Unlimited.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to make a Discord Moderation bot in Java

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

Our build.gradle now looks like this:

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.

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.

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:

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:

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:

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(commandClientBuilder.build()) to .addEventListener(commandClientBuilder.build(), new CurseWorldFilter()) so our listener is registered.

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

Add a comment

Related posts:

Five cryptocurrency myths debunked

There is much talk about cryptocurrencies these days, more notably Bitcoin. Bitcoin was the major disrupt-er, and many have seen the light of day since then. While many still fail to fully understand…

Is Fifa 19 really as broken as the fans are saying?

Currently holding an abysmal one out of five star rating on all the major product review sites, the 7 month old Fifa 19 has proved to be a considerable disappointment. While most of the issues being…