I am currently creating a bot for Discord. So far, I have got the bot up, and have it "Online" in my server. I created a custom command named !yata that displays a motivational message after being input. Is there a reason as to why my bot will not pick up commands? When I type in !yata it does not run the command.
Buki.java
package Buki;
import javax.security.auth.login.LoginException;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
public class Buki {
public static JDA jda;
public static String prefix = "!";
//Main method
public static void main(String[] args) throws LoginException {
JDA jda = JDABuilder.createDefault("bot token").build();
jda.addEventListener(new Commands());
}
}
Commands.java
package Buki;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Commands extends ListenerAdapter {
public void onGuildMessageReceived(MessageReceivedEvent e) {
String[] message = e.getMessage().getContentRaw().split(" ");
if (message[0].equalsIgnoreCase("!yata")) {
e.getChannel().sendTyping().queue();
e.getChannel().sendMessage("The road of a ninja is long!").queue();
}
else {
}
}
}
First of all, your codes are so weird!
How could the event be called MessageReceivedEvent, meanwhile, the method called onGuildMessageReceivedEvent, JDA already renamed the methods! and second you didn't use the GatewayIntent.GUILD_MESSAGES, so basically, it going to be like this:
JDABuilder jda = JDABuilder.create(token, GatewayIntent.GUILD_MESSAGES);
and third, think is I see you using JDA, not JDABuilder which is wrong, I recommended you to check what version of JDA you use and use the last one!
public void onGuildMessageReceived(MessageReceivedEvent e)
needed to be changed to
public void onMessageReceivedEvent(MessageReceivedEvent e).
"Guild" was not required in this method's name.
Related
In JDA i keep getting the error "The constructor JDABuilder(AccountType) is deprecated"
package diaduck.Playground;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
public class Main {
public static JDA jda;
public static void main(String[] args) {
jda = new JDABuilder(AccountType.BOT).setToken("**************************").buildAsync();
}
}
This constructor has been deprecated for well over a year now. The documentation tells you exactly why and what needs to be done. In fact, this constructor no longer even exists on the latest version of JDA.
The migration guide tells you to use the new factory methods instead. Read more on GitHub
I am getting into coding Discord bots and this is my first bot.
Here's my code:
import net.dv8tion.jda.JDABuilder;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import javax.security.auth.login.LoginException;
import static net.dv8tion.jda.api.AccountType.*;
public class javaclass {
public static JDA jda;
public static void main(String[] args) throws LoginException {
JDABuilder builder = new JDABuilder(BOT).setToken("thisIsAtoken").build();
}
}
I am not that new to Java, I'm just getting this error and I can't figure out why.
Note: I've also tried JDABuilder builder = new JDABuilder(AccountType.BOT).setToken("thisIsAtoken").build();
but regardless intellij insists otherwise.
Thank you
edit: I made the code this jda = new JDABuilder(new AccountType(BOT)).setToken and I get this error 'AccountType()' has private access in 'net.dv8tion.jda.api.AccountType'
Following the JDA documentation, and with the recent version 4.2, you need a different approach to make a JDA instance.
You should use: JDABuilder builder = JDABuilder.createDefault("thisIsAtoken").build();
I'm trying to make my bot welcome someone whenever someone joins but I can't seem to get it to work. For example(this will appear in an embed by the way):
#Jason joined. You must construct additional pylons.
Can someone help me edit my code so it works please?
Here's my Main code:
import Events.*;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import javax.security.auth.login.LoginException;
public class Main {
public static void main(String[] args) throws LoginException {
JDABuilder jda = JDABuilder.createDefault("I imported key here");
jda.setActivity(Activity.watching("baldness"));
jda.addEventListeners(new Help());
jda.addEventListeners(new PingPong());
jda.addEventListeners(new Clear());
jda.addEventListeners(new Welcome());
jda.build();
}
}
and Here's my Welcome code:
package Events;
import java.util.Random;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Welcome extends ListenerAdapter {
public class GuildMemberJoin extends ListenerAdapter {
String[] messages = {
"[member] joined. You must construct additional pylons.",
"Never gonna give [member] up. Never let [member] down!",
"Hey! Listen! [member] has joined!",
"Ha! [member] has joined! You activated my trap card!",
"We've been expecting you, [member].",
"It's dangerous to go alone, take [member]!",
"Swoooosh. [member] just landed.",
"Brace yourselves. [member] just joined the server.",
"A wild [member] appeared."
};
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
Random rand = new Random();
int number = rand.nextInt(messages.length);
EmbedBuilder join = new EmbedBuilder();
join.setColor(0x66d8ff);
join.setDescription(messages[number].replace("[member]", event.getMember().getAsMention()));
event.getGuild().getDefaultChannel().sendMessage(join.build()).queue();
}
}
}
The documentation fo GuildMemberJoinEvent clearly states:
Requirements
This event requires the GUILD_MEMBERS intent to be enabled.
createDefault(String) and createLight(String) disable this by default!
So you must enable the intent. Read more in the wiki guide Gateway Intents and Member Cache Policy
Additionally, you have a nested class for no reason which means you register the enclosing class as a listener but not the nested class that actually implements it.
Better:
public class Welcome extends ListenerAdapter {
#Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
...
}
}
I've tried to modify minecraft by adding a new item called "uranium". Therefore I created the class "Trauma.java" in the main package and a few other classes listed below.
All packages and classes:
Package Explorer
Trauma.java
package main;
import items.ItemUranium;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import proxy.ServerProxy;
#Mod(modid = Trauma.MODID)
public class Trauma {
public static final String MODID = "Trauma";
#SidedProxy(clientSide = "proxy.ClientProxy", serverSide = "proxy.ServerProxy")
public static ServerProxy proxy;
public static ItemUranium uranium = new ItemUranium();
#EventHandler
public void preInit(FMLPreInitializationEvent event) {
GameRegistry.register(uranium);
}
#EventHandler
public void init(FMLInitializationEvent event) {
proxy.registerClientStuff();
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
}
BasicItem.java
package items;
import net.minecraft.item.Item;
public class BasicItem extends Item {
public BasicItem(String name) {
setUnlocalizedName(name);
setRegistryName(name);
}
}
ItemUranium.java
package items;
public class ItemUranium extends BasicItem {
public ItemUranium() {
super("uranium");
}
}
ClientProxy.java
package proxy;
import items.BasicItem;
import main.Trauma;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
public class ClientProxy extends ServerProxy {
#Override
public void registerClientStuff () {
registerItemModel(Trauma.uranium);
}
public static void registerItemModel(BasicItem item) {
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Trauma.MODID + ":" + item.getRegistryName(), "inventory"));
}
}
ServerProxy.java
package proxy;
public class ServerProxy {
public void registerClientStuff() {}
}
uranium.json
{
"parent": "item/generated",
"textures": {
"layer0": "Trauma:items/uranium"
}
}
uranium.png
ingame
Also I don't know why the item in inventory isn't called uranium...
I spent two hours on fixing the problem and it didn't help so it would be really nice if somebody of you may help me.
Thanks :)
Don't use the Model Mesher:
The model mesher is Vanilla (Mojang) code and using it correctly has always been finicky and unreliable, failing if you called it too early and failing if you called it too late. So Forge added the ModelLoader class to resolve that problem.
Replace this line:
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(...)
With this line:
ModelLoader.setCustomModelResourceLocation(...)
The ... contents are identical.
Second, depending on what version of Minecraft you're using, you should...:
Stop using GameRegistry.Register
Instead use the RegistryEvent.Register<T> events (where <T> will be <Block> to register blocks, <Item> to register items, etc)
Register your models in the ModelRegistryEvent and no where else.
This event is #SideOnly(CLIENT) and can be subscribed to in your client proxy, avoiding the need to forward references through your proxy class. Eg. I do it like this, where lines 197-199 is the most common scenario needed, where the array is populated during the item registration event. The rest of that method handles the custom state mappers and custom mesh definitions that are used by only a handful of items/blocks and not relevant here.
Include your Mod ID in your unlocalized name. The best way to do this would be setUnlocalizedName(getRegistryName().toString());
See also the Forge documentation on events.
I am new to creating minecraft plugins, however not new to programming, I am following a tutorial very thoroughly, the video has good ratings so it is trusted, when watching the video the guy has no problems what so ever (Youtube video on developing minecraft plugins) , so I did some research into solutions but always the line through the code.
Eclipse gives me the option for: #SuppressWarnings("deprecation") which allows the code to be used still but I would rather have no need of that usage.
Basically my question is why is there the need of the line going through the code and how do I find a solution to get rid of it.
Main class:
package com.jc1;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Core extends JavaPlugin
{
public Permission pPermission = new Permission("playerAbilities.allowed");
#Override
public void onEnable()
{
new BlockListener(this);
PluginManager pm = getServer().getPluginManager();
pm.addPermission(pPermission);
}
#Override
public void onDisable()
{
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("giveitems") && sender instanceof Player)
{
Player p = (Player) sender;
if(p.hasPermission("playerAbilities.allowed"))
{
p.setItemInHand(new ItemStack(Material.DIAMOND_BOOTS));
}
return true;
}
return false;
}
}
Secondary class:
package com.jc1;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class BlockListener implements Listener
{
public BlockListener(Core plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void onBlockPlace(BlockPlaceEvent e)
{
Player p = e.getPlayer();
if(!p.hasPermission("playerAbilities.allowed"))
{
e.setCancelled(true);
}
}
}
The method is deprecated, meaning that it is not recommended to be used anymore and is most likely replaced with another method.
Methods that are deprecated may still work as intended.
A simple search for the method reveals (this) documentation, stating:
players can duel wield now use the methods for the specific hand instead
which referes to the #see references:
getItemInMainHand() and getItemInOffHand().
Use this:
player.getInventory().getItemInMainHand()
Instead of:
player.getItemInHand()
Hope this helps! :D