Building a simple Discord Chat Bot in Python for fun

Building a simple Discord Chat Bot in Python for fun

·

3 min read

Ok, but why?

When I launched my Discord community server, I needed a bot to give new members the Huhn role after agreeing to the rules. Since third-party solutions like Mee6 have proven unreliable, Huhnbot was born!

Agree to the rules by clicking an emoji

Besides giving people the Huhn role, it also had some fun responses. But most of the time, it simply responded with "Hmmm?". (That didn't keep people from trying to talk to it, though!)

Huhnbot responding with Hmmm

So now that Discord has a built-in feature for agreeing on the server rules, the relevance of Huhnbot was being questioned. But because everybody had so much fun talking to it, I decided to reprogram it into a proper chatbot!

How it started

Since old Huhnbot was written in Python, I wanted to see if there were any similar projects or tutorials out there, and I found this video:

It's based around the neuralintents package, which makes creating chatbots really easy!

Here is an example of the intents.json file where you define the patterns and responses of your bot:

{"intents": [
  {"tag": "greeting",
    "patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up", "Hey", "greetings"],
    "responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"],
    "context_set": ""
  },
  {"tag": "goodbye",
    "patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day", "bye", "cao", "see ya"],
    "responses": ["Sad to see you go :(", "Talk to you later", "Goodbye!"],
    "context_set": ""
  },
  {"tag": "stocks",
    "patterns": ["what stocks do I own?", "how are my shares?", "what companies am I investing in?", "what am I doing in the markets?"],
    "responses": ["You own the following shares: ABBV, AAPL, FB, NVDA and an ETF of the S&P 500 Index!"],
    "context_set": ""
  }
]
}

And this is all the Python code you need to make it work:

from neuralintents import GenericAssistant

assistant = GenericAssistant('intents.json', model_name="test_model")
assistant.train_model()
assistant.save_model()

done = False

while not done:
    message = input("Enter a message: ")
    if message == "STOP":
        done = True
    else:
        assistant.request(message)

Crazy simple right?

The results

A simple python library that allows me to define responses, and the bot would automatically decide which one to choose based on the messages it receives, was exactly what I was looking for.

So I went ahead and reprogrammed Huhnbot to have responses to commonly asked questions, some basic chatbot replies, and easter eggs.

Huhnbot telling me to stream tonight

It's been so much fun already, and I will probably continue to add more responses to it over time.

Huhnbot telling a joke

Conclusion

If you have a discord server and want to add some entertainment for your community, or you get a lot of frequently asked questions, a bot like this is perfect. It's super easy to set up and works reliably.

The source code of Huhnbot is available on Github. Maybe this will inspire someone to build their own Discord chatbot 😊.

Building bots is easy, fun, and a good coding exercise for any beginner. It's also a great way to share your progress as other server members can interact with it and test it.

I've never built a bot like this before, and I'm happy I was able to make this one so quickly. Of course, it's not perfect, but it works, and people are having fun with it - and this is all that matters.

Huhnbot being happy about this blog post