Reputation: 11
I am using Jda, Spring boot, h2 database, i tried to make random joke command for my discord bot
@Service
public class BotJokeService extends ListenerAdapter {
private final ChuckNorrisQuotes quotes = new ChuckNorrisQuotes();
private final PlayerRepository playerRepository;
public BotJokeService(PlayerRepository playerRepository) {
this.playerRepository = playerRepository;
}
@Override
public void onMessageDelete(MessageDeleteEvent event) {
event.getGuild().getDefaultChannel().asTextChannel().sendMessage(quotes.getRandomQuote()).queue();
}
//
@Override
public void onMessageReceived(MessageReceivedEvent event) {
// User user = event.getAuthor();
if (!event.getAuthor().isBot()) {
String message = "!joke";
if ("!joke".equals(message)) {
// event.getGuild().mute(user, true).queue(); mute
event.getChannel().asTextChannel().sendMessage(quotes.getRandomQuote()).queue();
Player random = new Player();
random.setName(message);
random.setRole("yes");
random.setCreatedDate(LocalDateTime.MAX);
playerRepository.save(random);
}
}
}
}
and now i want that my jda listen to BotJokeService class
@SpringBootApplication
public class DcBot1Application {
@Autowired
private EmailSenderService emailSenderService;
public static String prefix = "-";
public static void main(String[] args) throws LoginException {
SpringApplication.run(DcBot1Application.class, args);
JDA bot = JDABuilder.createDefault("here goes my bot token")
.setActivity(Activity.playing("example"))
.addEventListeners(new MessageSending(),
new Help(), new BotJokeService())
.build();
}
}
but new BotJokeService() gives me eror constructor BotJokeSerivce in class BotJokeSerivce cannot be applied to given types, i know it need constructor but how to initalize my PlayerRepository constructor here ?
Upvotes: 0
Views: 73
Reputation: 7005
You can get a bean in the main method like this:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
BotJokeService service = context.getBeanFactory().getBean(BotJokeService.class);
//do whatever you need with the service now
}
}
I haven't used the discord api before, but application startup does not look like the best place to create the bot instance.
Upvotes: 0