Table of Contents
- Origin of the idea
- Potions, please!
- Discord Discovery
- Fighting Demons in The Cloud
- Putting the Disco in Discord
Origin of the idea
With the release of Blizzard's Diablo 4 came plenty of demon-slaying and dopamine hits watching my experience bar fill up. As the grind to level 100 took more experience the higher we got, we soon realised that there were potions available in-game that would increase our exp. gains by 5%. We began applying the effect to our characters but soon realised that the potions expired after 30 minutes. While this usually wouldn't be a problem we became too focused on defeating demons that we forgot to activate the potions buff every 30 minutes, resulting in a significant XP loss.
Some of my friends opted to put timers on their phones - but where's the fun in that?
Potions, please!
A solution seemed simple and came from a question: what was always open when playing Diablo 4 with friends?
Monitor 1: Diablo 4 (obviously)
Monitor 2: Discord/Diablo 4 guide
Having used Discord for many years I was familiar with its support of bots: music-playing, game hosting, information gathering, you name it and there was probably a bot for it.
My idea was to create a bot that pinged my discord server and mentioned my fellow Diablo 4 demon-slayers discord usernames so they would get a notification to remind them it was time to guzzle & grind.
A few more questions popped up during ideation:
- How can I securely store any credentials required?
- How can I implement this so it is always on?
- Could I stretch the functionality to do other things such as share music?
So my discovery phase began.
Discord Discovery
After some initial googling I realised to create a Discord Bot I would need to get familiar with the Discord Developer Portal.
I quickly learnt that each "entity" had its own identifier (myself as a user, each voice/text channel, each bot) and my code would need to either reference each ID or I'd need to make it dynamic enough to recognise when a command was entered, such as '/play [search term here]'.
Another important learning was the use of bot tokens, which would allow my code to reference my bot and have it perform commands. However best practice is to not disclose this token as it could enable malicious code to be executed using my bot. Through my work I had some experience with the keyring python library, a cryptography library that allows for the encryption of sensitive information so I decided to use it in my main code block to remove the direct reference to a bot key.
#retrieves the token assigned to the profile "system" & the username "DISCORD_BOT_TOKEN"
token = keyring.get_password("system", "DISCORD_BOT_TOKEN")
#runs the bot using the provided token
bot.run(token)
After implementing this into my code I attempted to run it and voila - PotBot was born! My first bot iteration was a success and it tagged my discord channel every 30 minutes as required.
Fighting Demons in The Cloud
I was now able to run my python script locally and my bot would come alive and send helpful reminders to my group of friends. However my next question was how can I have this run 24/7 so that even when I'm not online this bot could still function? I could share the code with my friends to run locally themselves, however they would require enough python familiarity to set up keyring which still requires manual entry of a bot token at least once. So I turned to cloud hosting services.
While having some experience with AWS I decided to give Google Cloud a go as they offered a generous free tier. Registering and setting up was easy enough however I had to research how to keep my bot always on ready to notify the required channel. A big shout out to the Linuxize blogpost which goes in depth into the Linux screen functionality, keeping a session open even if it were to disconnect. Using screen, I now had my bot running 24/7 in the cloud ready to notify of us juicy XP gains.
However as our gaming group tends to get bored quite quickly we soon moved on from Diablo 4, meaning that PotBot's constant reminders would no longer be needed. Rather than abandon my creation I decided to iterate on its capabilities and pivot to build a music bot.
Putting the Disco in Discord
I was not surprised to find existing python music bots out there on the internet, however I wanted more control and the ability to run and play back not only YouTube files, but local audio files too. So I created an overview of what I wanted:
As I looked into the discord libraries I realised that the discord python library was discontinued but found the nextcord library which was a community fork of the original. That allowed me to interface with discord servers, while yt-dlp handled the YouTube download requests.
After some chatGPT powered brainstorming and iterations I had finally gotten a working script that functioned as I wanted it to. It was intelligent enough to identify which channel its requester was in and deploy to that channel to play the required request. I then had a simplified version that would instead read whichever audio file path I provided.
Seeing as PotBot is a relic from its previous life I had to come up with a new name. Something to do with playing back audio. Singbot? Nah. IdolBot? Pfft. All this name brainstorming was making me hungry, so I decided to have a snack of some crackers and my favourite dip Hummus. Wait...Hummus.
Voila! My bot was served, er, born.
Resources
https://linuxize.com/post/how-to-use-linux-screen/
ChatGPT