I integrated Twitch4J into my Discord bot

Michael Lamb
2 min readDec 2, 2022
Photo by Caspar Camille Rubin on Unsplash

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 TwitchServiceImplsnippet 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;

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

The listeners create embed messages and send them to specific channels in my Discord server.

If you’re interested in integrating Twitch into your app, I’d recommend getting started with the documentation on Twitch4J.

The code is closed source for my Discord bot. In this post, I describe a simple problem I encountered which I didn’t follow up on for weeks because I’d run out of time to justify the work on it. Sometimes, the most basic of mistakes hold us back from moving forward.

Recommended Reading

Michael Lamb

software engineer | culture nerd | reader