A Discord chatbot that utilizes a Markov chain to generate interesting responses based off messages sent in the discord channel. It also allows users to query for youtube videos from science educators and musicians.
Download the code, add your API keys, and run it in the command line to have a fun, informative, Markov chain fueled chatbot ready to invigorate your moribund group chat.
-
The Markov Chain takes messages sent from other, human users in the Discord channel you elect to add your bot to, and, when you say the bot's name (in this case buzz) it will generate new, original messages that are mix-matches of messages sent by other users. It is incredibly fun, especially with an active, large group chat, and for more exciting than my dry write up does it justice.
-
Using these commands !kurzegsagt, !veritasium, !bigthink, !lambert, !wired, !3blue1brown, !studymusic, and !wired, it can deliver educational videos, music, and articles straight into your chat with no google search required.
-
Also, if you want the bot to send actually coherent, pre-written message, just type launch fact or space fact appended with the bot's name (again, buzz) and you will be rewarded with a random fact. If you want to alter them, just doctor the messages.
-
The bot will only send messages when you type the name "buzz" in your discord message, so don't worry about it taking over your group chat. He will only come out to play if you specifically call for him. (or if you say buzz off, I want a buzz, etc.)
-
Below is info you need to set up the bot on discord and a breakdown of the Markov chain I employ. Have fun!
The following steps are required to set up your bot on the Discord messaging app.
-
Go to the discord Developer Portal and log in or sign up one: https://discord.com/login?redirect_to=%2Fdevelopers%2Fapplications
-
Click on the "New Application" button
-
Enter a name for your application (which is your bot's name) and click "Create"
-
In the Application settings, navigate to the "Bot" tab
-
Click "Add Bot" and confirm
-
Under the bot's settings, customize your bot's username and avatar. I use Buzz Alterin for the username and uploaded an AI generated artwork. Do whatever.
-
Ensure that the "Public Bot" option is enabled if you want others to invite your bot
-
Click the "Copy" button to obtain your bot's token. KEEP THIS IN A SECURE LOCATION WHERE YOU CAN ACCESS IT BUT NO ONE ELSE
-
In the Developer Portal, navigate to the OAuth2 tab
-
Under this section, select the bot checkbox in the "Scopes" section
-
In the "Bot Permissions" section, select permissions you want to grant your bot.
-
A URL will be granted under the scopes which you will need. If there isn't a URL then there was an issue with the permissions you granted.
-
Paste the URL into your browser, select the server you want to add your bot to, and authorize it.
(Troubleshooting: If your bot initially doesn't appear when you authorize it, make sure you have enabled enough permissions and actually have authorized it. That or it does not have access to your channel. Make sure your bot can read message history.)
Alright so for some added functionality other than the Markov chain you can go and collect some API keys, delivering your favorite youtube music playlists, science educators, and articles straight to your discord channel.
-
Go to the Google Cloud Console: https://developers.google.com/apis-explorer
-
Create a new project
-
Navigate to the APIs & Services -> Library
-
Search for Youtube Data API v3 and enable it
-
Go to APIs & Services --> Credentials
-
Click on "Create Credentials" and select "API key"
-
Copy the generated API key. AGAIN, KEEP THIS PRIVATE
-
Set up your development Environment
- Install:
pip install discord.py google-api-python-client feedparser
-
Set up your API keys
-
See my other read.me for specifics on the actual code.
If you download my project, here is a breakdown of the code.
#Code modified from https://healeycodes.com/generating-text-with-markov-chains
# Function to build Markov chain
def build_markov_chain(text, state_size=2):
words = text.split()
for i in range(len(words) - state_size):
current_state = tuple(words[i:i + state_size])
next_word = words[i + state_size]
markov_chain[current_state].append(next_word)
def generate_text(start_state, length=20):
current_state = start_state
result = list(current_state)
for _ in range(length - len(current_state)):
if current_state not in markov_chain:
break
next_word = random.choice(markov_chain[current_state])
result.append(next_word)
current_state = tuple(result[-len(current_state):])
return ' '.join(result)Above the Markov chain will obtain the messages sent previously in the channel it is added to. The more messages sent the more it can drink up and spit back out for increasinly sophisticated, occasionally lucid messages. The stochasticity of it makes it really fun to talk to, since, unlike a conversational agent like ChatGPT, it can range from wildly nonsensical to oddly coherent. Shockingly fun in practice.
How it Works
The input text, messages in the channel, are split into words. For each sequence of consecutive words, we record which word comes next. These sequences and their possible next words are stored in a dictionary. In this dict, the keys are tuples representing the ucrrent state (two words). The values are lists of possible next words. The output is stylistically similar messages to those sent by your friends in the discord channel.
Brilliant Markov Chain explanation: https://brilliant.org/wiki/markov-chains/
Markov Wiki: https://en.wikipedia.org/wiki/Markov_chain
Other Discord bots and how to make your bot more readily available to the public: https://top.gg
An easy "cheat" website that streamlines this process should you not wish to suffer for hours over debugging: https://botghost.com
Connect with your peers on everyone's favorite Reddit: https://www.reddit.com/r/Discord_Bots/comments/1cac0qn/what_do_you_need_to_create_a_discord_bot/