#minecraft to discord report plugin

1 messages · Page 1 of 1 (latest)

light sleet
#

here is the code that i am using can someone help me get the results from the command and make it so that i can get the name of the person reporting the name of the person being reported and the reason why and make it go to a discord channel

#

package me.mraxeman6.atf_plugin;

import me.mraxeman6.atf_plugin.listeners.discordListener;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
import net.dv8tion.jda.api.sharding.ShardManager;

import org.bukkit.plugin.java.JavaPlugin;

import javax.security.auth.login.LoginException;

public class ATF_plugin extends JavaPlugin {

public static ATF_plugin plugin;

private final ShardManager shardManager;
public ATF_plugin() throws LoginException {
    String Token = "MTEzMzg4OTUwMTIzNjgzNDQyNA.GksLYk.D_8qrZkcHxDADE14C-92lmqUYDpH6XmG0CWvds";
    DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createDefault(Token);
    builder.setStatus(OnlineStatus.ONLINE);
    builder.setActivity(Activity.playing("testing"));
    shardManager = builder.build();

    //Registier events
    shardManager.addEventListener(new discordListener());
}
public ShardManager getShardManager(){
    return shardManager;
}
@Override
public void onEnable() {
    plugin = this;
    getLogger().info("ATF Plugin has been Enabled");
}
@Override
public void onDisable(){
    getLogger().severe("something went wrong");
}



public static ATF_plugin getInstance(){
    return plugin;
}

}

#

package me.mraxeman6.atf_plugin.listeners;

import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import org.jetbrains.annotations.NotNull;

public class discordListener extends ListenerAdapter {
@Override
public void onReady(@NotNull ReadyEvent event) {
TextChannel txtChannel = event.getJDA().getTextChannelById("1134805403625082980");

    if (txtChannel.canTalk()) {
        txtChannel.sendMessage("Bot is online").queue();
    }
}

}

#

package me.mraxeman6.atf_plugin.commands;

import me.mraxeman6.atf_plugin.utils.msgColour;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

public class report implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("report")) {
        // Check if the command sender is a player
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (args.length >= 2) {
                String targetPlayerName = args[0];
                String reportReason = String.join(" ", Arrays.copyOfRange(args, 1, args.length));

                // Handle your report logic here, e.g., store it in a database or notify admins
                // You can also use Bukkit's built-in report system if you prefer

            } else {
                player.sendMessage("Usage: /report <player> <reason>");
            }
        } else {
            sender.sendMessage("Only players can use this command.");
        }
        return true;
    }
    return false;
}

}

void crescent
#

You can write the code between "`" so it gets formatted

System.println("Hello World");
light sleet
#

ok

void crescent
#

I don't know much about jda (I prefer nodejs for discord bots), but as I guess you need it to be in java, doesn't jda have a method to obtain a channel by id and send a message to it?

light sleet
#
package me.mraxeman6.atf_plugin.commands;

import me.mraxeman6.atf_plugin.utils.msgColour;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

public class report implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("report")) {
            // Check if the command sender is a player
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (args.length >= 2) {
                    String targetPlayerName = args[0];
                    String reportReason = String.join(" ", Arrays.copyOfRange(args, 1, args.length));

                    // Handle your report logic here, e.g., store it in a database or notify admins
                    // You can also use Bukkit's built-in report system if you prefer

                } else {
                    player.sendMessage("Usage: /report <player> <reason>");
                }
            } else {
                sender.sendMessage("Only players can use this command.");
            }
            return true;
        }
        return false;
    }
}

#
package me.mraxeman6.atf_plugin;

import me.mraxeman6.atf_plugin.listeners.discordListener;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
import net.dv8tion.jda.api.sharding.ShardManager;

import org.bukkit.plugin.java.JavaPlugin;

import javax.security.auth.login.LoginException;

public class ATF_plugin extends JavaPlugin  {
    
    public static ATF_plugin plugin;

    private final ShardManager shardManager;
    public ATF_plugin() throws LoginException {
        String Token = "MTEzMzg4OTUwMTIzNjgzNDQyNA.GksLYk.D_8qrZkcHxDADE14C-92lmqUYDpH6XmG0CWvds";
        DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createDefault(Token);
        builder.setStatus(OnlineStatus.ONLINE);
        builder.setActivity(Activity.playing("testing"));
        shardManager = builder.build();

        //Registier events
        shardManager.addEventListener(new discordListener());
    }
    public ShardManager getShardManager(){
        return shardManager;
    }
    @Override
    public void onEnable() {
        plugin = this;
        getLogger().info("ATF Plugin has been Enabled");
    }
    @Override
    public void onDisable(){
        getLogger().severe("something went wrong");
    }



    public static ATF_plugin getInstance(){
        return plugin;
    }

}

#
package me.mraxeman6.atf_plugin.listeners;

import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import org.jetbrains.annotations.NotNull;


public class discordListener extends ListenerAdapter {
    @Override
    public void onReady(@NotNull ReadyEvent event) {
        TextChannel txtChannel = event.getJDA().getTextChannelById("1134805403625082980");

        if (txtChannel.canTalk()) {
            txtChannel.sendMessage("Bot is online").queue();
        }
    }


}
#

theese are all different files

void crescent
#

The "Bot is online" message gets sent right?

light sleet
#

yes

void crescent
#

Allright, you will need to add a method on your plugin class to obtain JDA instance

#

And in the command, follow the logic you've used to send that message

light sleet
#

it gives an error when putting the send message logic in saying that event isnt a symbol

#

how do i obtain a JDA instonce

#

is that lik the keyword event

void crescent
#

Sorry, I went for my laptop so I can help you better;
After some research, I've found this, wich I think is exactly what you need:
https://github.com/discord-jda/JDA/wiki/10)-FAQ#how-can-i-send-a-message-to-a-specific-channel-without-an-event

GitHub

Java wrapper for the popular chat & VOIP service: Discord https://discord.com - discord-jda/JDA

#

Basically, what you need to do is the following;

public class ATF_Plugin ... {
  public JDA jda;
}
public class discordListener extends ListenerAdapter {
    @Override
    public void onReady(@NotNull ReadyEvent event) {
        TextChannel txtChannel = event.getJDA().getTextChannelById("1134805403625082980");

        if (txtChannel.canTalk()) {
            txtChannel.sendMessage("Bot is online").queue();
        }

        ATF_Plugin.getInstance().jda = event.getJDA();
    }


}
public class report implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("report")) {
            // Check if the command sender is a player
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (args.length >= 2) {
                    String targetPlayerName = args[0];
                    String reportReason = String.join(" ", Arrays.copyOfRange(args, 1, args.length));

                    // Handle your report logic here, e.g., store it in a database or notify admins
                    // You can also use Bukkit's built-in report system if you prefer
                    JDA jda = ATF_Plugin.getInstace().jda; //Yay!
                } else {
                    player.sendMessage("Usage: /report <player> <reason>");
                }
            } else {
                sender.sendMessage("Only players can use this command.");
            }
            return true;
        }
        return false;
    }
}
#

Some things to mention

  • In java, class names must be CammelCase, basically, each new word must start with upper case, for example, a class that handles player data, won't be "playerData", instead, will be PlayerData
  • Fields, variables and methods (except constructors, of course) are lowerCase without spaces and without _, so for example String my_player\_name = ""; would be String myPlayerName = ""
light sleet
#

Ok thank you so much

#

So are these all seperate files then

#

Also do i need the onEnable and disable in the main file

void crescent
light sleet
light sleet
#

One of the EventListeners had an uncaught exception
java.lang.NullPointerException: Cannot assign field "jda" because the return value of "me.mraxeman6.atf_plugin.ATF_plugin.getInstance()" is null

#

this is an error that i have got in a game consol please can someone please help me

void crescent
#

You are registering the bot (and for so, calling the bot online event) before you set the plugin instance

#

How to fix

#
public ATF_plugin() {
+       plugin = this;
        String Token = /*Discord token*/;
        DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createDefault(Token);
        builder.setStatus(OnlineStatus.ONLINE);
        builder.setActivity(Activity.playing("testing"));
        shardManager = builder.build();

        //Registier events
        shardManager.addEventListener(new discordListener());
}

public void onEnable() {
-       plugin = this;
}
#

Also please, modify your messages to remove the discord token

#

Anyone here could use it to do malicious things

#

And not everyone here is a developer neither ethical so...

light sleet
#

dont worry i resetr the token

#

i did the change and i got the same error

void crescent
#

In that case, use dependency injection (which is what you should be doing anyway)

package me.mraxeman6.atf_plugin.listeners;

import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import org.jetbrains.annotations.NotNull;

public class DiscordListener extends ListenerAdapter {

    private final ATFPlugin plugin;

    public DiscordListener(final ATFPlugin plugin) {
      this.plugin = plugin;
    }

    @Override
    public void onReady(@NotNull ReadyEvent event) {
        JDA jda = event.getJDA();
        TextChannel txtChannel = jda.getTextChannelById("1134805403625082980");

        if (txtChannel.canTalk()) {
            txtChannel.sendMessage("Bot is online").queue();
        }

        this.plugin.setJda(jda); //We have a setter, because we should never leave a field public unless strictly necessary
    }
}
#
package me.mraxeman6.atf_plugin;

import me.mraxeman6.atf_plugin.listeners.DiscordListener;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
import net.dv8tion.jda.api.sharding.ShardManager;

import org.bukkit.plugin.java.JavaPlugin;

import javax.security.auth.login.LoginException;

public class ATFPlugin extends JavaPlugin  {

    private JDA jda;

    public ATFPlugin() throws LoginException {
        String token = /*Your discord token*/;
        DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createDefault(Token);
        builder.setStatus(OnlineStatus.ONLINE);
        builder.setActivity(Activity.playing("testing"));
        shardManager = builder.build();

        //Registier events
        shardManager.addEventListener(new DiscordListener(this));
    }

    public ShardManager getShardManager(){
        return shardManager;
    }

    @Override
    public void onEnable() {
        getLogger().info("ATF Plugin has been Enabled");
        PluginCommand reportPC = getCommand("report"); //You didn't have this, but I assume that's your command name
        ReportCommand reportExecutor = new ReportCommand(this);

        if (reportPC != null) {
          reportPC.setExecutor(reportExecutor);
        }
    }

    @Override
    public void onDisable(){
        getLogger().severe("Shutting down, doesn't mean that something has went wrong");
    }

    public void setJda(final JDA jda) {
      this.jda = jda;
    }

    public JDA getJda() {
      return this.jda;
    }
}
#
public class ReportCommand implements CommandExecutor {

    private final ATFPlugin plugin;

    public ReportCommand(final ATFPlugin plugin) {
      this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        //if (cmd.getName().equalsIgnoreCase("report")) { Not needed
            // Check if the command sender is a player
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (args.length >= 2) {
                    String targetPlayerName = args[0];
                    String reportReason = String.join(" ", Arrays.copyOfRange(args, 1, args.length));

                    // Handle your report logic here, e.g., store it in a database or notify admins
                    // You can also use Bukkit's built-in report system if you prefer
                    JDA jda = plugin.getJda(); //Yay!
                } else {
                    player.sendMessage("Usage: /report <player> <reason>");
                }
            } else {
                sender.sendMessage("Only players can use this command.");
            }

            return false; //Returning true means the command is valid, and if so, it prints the "usage" message if any or the "permissions" mes if any and the executor doesn't have permission
        //}
        //return false;
    }
}