I integrated Twitch4J into my Discord bot
In the past 12 hours I’ve managed to integrate my Twitch channel into a bot I wrote for my dev Discord server (you’re invited, by the way). Now, I’m able to have the bot send a message whenever my channel goes live. I’d gotten 98% of the way in writing the code that accomplished all of this, but I stopped working on it weeks ago to prioritize other work and it was broken at that point because I was missing a single line:
this.twitchClient = twitchClient;
I had failed to instantiate the local variable from the constructor (shown in the TwitchServiceImpl
snippet below), and so the app encountered a runtime error and wouldn’t start. Because the constructor exists in a class with the @Service
annotation, Spring will infer arguments from the constructor and provide a @Bean
for twitchClient
. Because the provided twitchClient
wasn’t assigned locally using this
, the register()
method ended up trying to use a null client.
public TwitchServiceImpl(TwitchClient twitchClient) {
this.twitchClient = twitchClient;
register();
}
The register()
method uses the twitchClient
to get the eventManager
, which is how the bot is able to set listeners for ChannelGoLiveEvent
and ChannelGoOfflineEvent
for my channel at twitch.tv/michaellambgelo