A poetry writing bot! It reads websites, learns how to write based on those websites, and then uses that knowledge to generate unique poems! I created this bot for the June 2019 Discord Hack Week. I am no longer hosting this bot, so see below if you wish to run this bot.
poem [url]
Generate a unique poem based on the text on a webpage. Urban Dictionary pages or websites with lots of text work well for this. Example: poem https://wikipedia.org/wiki/Discord_(software)
poemsearch [space separated keywords]
Searches Google for keywords, selects a URL, and then generates a poem based on that URL. Example: poemsearch discord hack week
invite
Get an invite link to invite me to your server
help
Show this list of commands in the chat.
ping
Get the bot's API latency.
stats
Get bot statistics
The gist of PoetryBot's algorithm:
- Load a webpage into a DOM. Then use Mozilla's Readbility article extraction code to get a plain text representation of the content of the webpage.
- Create a Markov Chain of the writing on the page. The model is a dictionary, which contains words mapped to a Counted Set representing the words that follow that word, and their frequency. This allows PoetryBot to generate sentences that are more natural than those generated by treating all words equally.
- Generate lines (strings of words of random length). These lines seed the generation of those that follow, to keep the poem on topic (for the most part)
- Combine the lines and post to the chat for your enjoyment!
You can use this bot two ways:
- Run the bot locally on your computer (information about how to do that below)
Invite the version I'm hosting, by clicking here.Hosted version has been shut down.
- Download this repository as a zip (or clone with a shallow depth)
- Create
env.json
with the following contents:
{
"TOKEN":"your discord bot token here",
"CREATOR_ID":"your user ID here"
}
- run
npm install
to install dependencies. - run
node server.js
to start the bot and the webserver. In the console the bot will print the webserver's port. In your web browser, navigate tolocalhost://PORT_HERE
to see the webpage.
Run the commands a few times! Not all websites are favorable for the algorithm, and sometimes the bot chooses a bad starting word on good websites. The bot caches where it can so subsequent runs should be faster.
If the poemsearch command is failing, perform the following:
- Follow the local hosting instructions
- Set a breakpoint in the
resolve()
call inside theurlToDOM()
function inutil.js
- Run the bot with a debugger attached. Have it run
poemsearch
. The breakpoint will trigger. - Use the debugger to get the contents of
data
. Copy all of it to your clipboard. - Paste into CodeVisualizer. The render view should populate with the google search results page in your clipboard.
- In CodeVisualizer, use your browser's find and replace to locate a link. Make sure you are clicked inside the code view.
- Find the CSS class name of the a containing element to the anchor tag. For example, if the find-replace found
...
<div class="kCrYT"><a href="/url?q=https://www.merriam-webster.com/dictionary"></a>
...
you would copy kCrYT
to your clipboard.
- Scroll to
getUrls()
inutil.js
. Replace the assignment toclassname
with the value you just copied. - Restart the bot in the debugger.
- Set a breakpoint under the
getElementsByClassName()
call to ensure that this class query is working. - Run another
poemsearch
query. Make sure that the resulting HTMLCollection on the breakpoint has elements. - Using the debugger, step to ensure
url
contains a correct URL. - Submit a pull request or open an Issue. Only open an Issue if you have completed the above!