Cleverbot Integration
Cleverbot NO LONGER offers a free trial; this guide page will NOT be updated to use any other module.
I've had this request since I started An Idiot's Guide, in fact it was one of the very first requests I had, but I had a feeling it would be a disappointing short episode, maybe a 5 minute long episode. But for a written guide it'd be perfect!
So to get started, let's grab the example from getting started and shove it in a file.
Once you've got that, we should go check out cleverbot-node
on npmjs.com and grab their example code
Alright, we've got both parts we need, now before we continue we should get the module installed, just run npm i cleverbot-node
.
Installed? Good! Now, let's get to the final step... the code.
We have both our example codes, now we need to combine them for a working bot.
NOTE: A lot of the naive developers would just shove the cleverbot example straight in their message event and wonder why it wasn't working. It would create a new instance of Cleverbot and would eventually cause a memory leak.
Right, we need to take the first two lines of the cleverbot example...
...and put them with our discord definitions.
I renamed cleverbot
to clbot
to reduce any possible confusion between the variable names as JavaScript is case sensitive.
Then we take the rest of the code and place that inside our message event handler, but for this example I only want the bot to talk to me in DM's, so we'll check the channel type
with the following code, you can make it respond on mentions or even in channels (I would honestly advise against that.) but we also need to add the DIRECT_MESSAGES
intent and the CHANNEL
partial as DM channels can be uncached.
Checking the channel type.
Don't forget to add these within your client object.
Your code should look something like this...
If everything is as above, then just send your bot a DM and watch the magic unfold!
Last updated