我的不和谐机器人不显示我添加到事件侦听器的命令。它只显示较旧的和两个甚至不在我的代码中的。
我尝试将机器人重新邀请到我的服务器并关闭服务器上的 Slash 命令一天 并添加了一个临时命令。
我的命令管理器看起来像这样:
package com.recon.commands;
import com.recon.services.DatabaseService;
import com.recon.services.MusicService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.recon.DiscordBot.jda;
@Slf4j
@RequiredArgsConstructor
@Component
public class CommandManager extends ListenerAdapter {
private final AddTrack addTrack ;
private final DisplayQueue displayQueue ;
private final SkipTrack skipTrack ;
private final JoinChannel joinChannel;
private final SetBotChannel setBotChannel ;
private final ViewGameCards viewGameCards;
private final CreateGameCard createGameCard;
private final DeleteGameCard deleteGameCard;
private final GetWebLink getWebLink;
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
String commandName = event.getName();
try {
switch (CommandNames.valueOf(commandName)) {
case addtrack:
addTrack.execute(event);
break;
case skiptrack:
skipTrack.execute(event);
break;
case displayqueue:
displayQueue.execute(event);
break;
case joinchannel:
joinChannel.execute(event);
break;
case setbotchannel:
setBotChannel.execute(event);
break;
case viewallgamecards:
viewGameCards.execute(event);
break;
case creategamecard:
createGameCard.execute(event);
break;
case deletegamecard:
deleteGameCard.execute(event);
break;
case getweblink:
getWebLink.execute(event);
break;
}
}catch (NullPointerException e){
log.info("Something went wrong wille executing command :" + e.getMessage() );
}
}
}
我的命令工厂看起来像这样:
package com.recon.commands;
import com.recon.data.GuildDataMap;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import static com.recon.DiscordBot.databaseService;
@Component
public class CommandFactory {
public List<CommandData> commandData = new ArrayList<>();
public CommandFactory(AddTrack addTrack,DisplayQueue displayQueue,SkipTrack skipTrack,JoinChannel joinChannel,SetBotChannel setBotChannel,ViewGameCards viewGameCards,CreateGameCard createGameCard, DeleteGameCard deleteGameCard, GetWebLink getWebLink) {
commandData.add(Commands.slash(addTrack.name(), addTrack.description()).addOptions(addTrack.options()));
commandData.add(Commands.slash(skipTrack.name(), addTrack.description()));
commandData.add(Commands.slash(displayQueue.name(), displayQueue.description()));
commandData.add(Commands.slash(joinChannel.name(), joinChannel.description()));
commandData.add(Commands.slash(setBotChannel.name(), setBotChannel.description()));
commandData.add(Commands.slash(viewGameCards.name(), viewGameCards.description()));
commandData.add(Commands.slash(createGameCard.name(),createGameCard.description()).addOptions(createGameCard.options()));
commandData.add(Commands.slash(deleteGameCard.name(), deleteGameCard.description()).addOptions(deleteGameCard.options()));
commandData.add(Commands.slash(getWebLink.name(), getWebLink.description()));
}
public void add(ICommand command){
commandData.add(Commands.slash(command.name(), command.description()).addOptions(command.options()));
}
}
我像这样创建我的 Jda
package com.recon;
import com.recon.commands.CommandManager;
import com.recon.data.GuildDataMap;
import com.recon.def.BotChannel;
import com.recon.def.GuildData;
import com.recon.listeners.Listener;
import com.recon.repo.BotChannelRepo;
import com.recon.services.DatabaseService;
import com.recon.services.GameCardService;
import com.recon.services.ReactionListener;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DiscordBot {
public static JDA jda;
public static DatabaseService databaseService;
public static GuildDataMap guildDataMap;
private static ApplicationContext applicationContext;
@Bean
@SneakyThrows
public JDA createJDA(CommandManager commandManager, ReactionListener reactionListener) {
jda = JDABuilder.createDefault("Dont want to leak my token")
.addEventListeners(reactionListener)
.addEventListeners(commandManager)
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
.enableIntents(GatewayIntent.GUILD_EMOJIS_AND_STICKERS)
.setStatus(OnlineStatus.ONLINE)
.setActivity(Activity.of(Activity.ActivityType.WATCHING, "The Matrix"))
.build();
guildDataMap = new GuildDataMap(databaseService);
return jda;
}
@SneakyThrows
public static void main(String[] args) {
SpringApplication.run(DiscordBot.class);
}
public static void displayAllBeans() {
String[] allBeanNames = applicationContext.getBeanDefinitionNames();
for (String beanName : allBeanNames) {
System.out.println(beanName);
}
}
}
命令界面如下所示:
package com.recon.commands;
import com.recon.services.MusicService;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import java.util.List;
public interface ICommand {
String name();
String description();
List<OptionData> options();
void execute(SlashCommandInteractionEvent event);
}
这是命令名称 Enum
package com.recon.commands;
public enum CommandNames {addtrack, skiptrack, displayqueue, joinchannel, setbotchannel, viewallgamecards, creategamecard, deletegamecard, getweblink
}
Discord 机器人命令(如果不是临时命令)最多可能需要一个小时才能更新。使用临时命令
guild.updateCommands().addCommands(Data)
用于调试/测试