Revamp Cleverbot

This commit is contained in:
Dragon Fire
2021-03-06 13:38:20 -05:00
parent d5d6abfa35
commit 168b8ec9ab
6 changed files with 94 additions and 32 deletions
+12
View File
@@ -154,6 +154,18 @@ client.on('message', async msg => {
const hasEmbed = msg.embeds.length !== 0;
if (msg.author.bot || (!hasText && !hasImage && !hasEmbed)) return;
if (client.blacklist.user.includes(msg.author.id)) return;
// Cleverbot handler
const cleverbot = client.cleverbots.get(msg.channel.id);
if (cleverbot) {
if (!hasText) return;
if (!cleverbot.shouldRespond(msg)) return;
const response = await cleverbot.respond(msg.cleanContent);
await msg.reply(response);
return;
}
// Phone message handler
const origin = client.phone.find(call => call.origin.id === msg.channel.id);
const recipient = client.phone.find(call => call.recipient.id === msg.channel.id);
if (!origin && !recipient) return;
+21
View File
@@ -0,0 +1,21 @@
const Command = require('../../structures/Command');
module.exports = class CleverbotEndCommand extends Command {
constructor(client) {
super(client, {
name: 'cleverbot-end',
aliases: ['clevs-end', 'chat-end'],
group: 'other',
memberName: 'cleverbot-end',
description: 'Ends the current Cleverbot chat.'
});
}
run(msg) {
if (!this.client.cleverbots.has(msg.channel.id)) {
return msg.say('There is not a Cleverbot conversation in this channel.');
}
this.client.cleverbots.delete(msg.channel.id);
return msg.reply('Ended the current conversation.');
}
};
+10 -31
View File
@@ -1,7 +1,5 @@
const Command = require('../../structures/Command');
const request = require('node-superfetch');
const { CLEVERBOT_KEY } = process.env;
const blankResponses = ['What?', 'Huh?', 'I don\'t understand.', 'Speak up, please.'];
const { stripIndents } = require('common-tags');
module.exports = class CleverbotCommand extends Command {
constructor(client) {
@@ -10,9 +8,7 @@ module.exports = class CleverbotCommand extends Command {
aliases: ['clevs', 'chat'],
group: 'other',
memberName: 'cleverbot',
description: 'Talk to Cleverbot.',
details: 'Only the bot owner(s) may use this command.',
ownerOnly: true,
description: 'Starts a Cleverbot conversation.',
credit: [
{
name: 'Cleverbot',
@@ -20,35 +16,18 @@ module.exports = class CleverbotCommand extends Command {
reason: 'API',
reasonURL: 'https://www.cleverbot.com/api/'
}
],
args: [
{
key: 'text',
prompt: 'What do you want to say to Cleverbot?',
type: 'string'
}
]
});
this.convos = new Map();
}
async run(msg, { text }) {
try {
const convo = this.convos.get(msg.channel.id);
const { body } = await request
.get('https://www.cleverbot.com/getreply')
.query({
key: CLEVERBOT_KEY,
cs: convo ? convo.cs : '',
input: text
});
if (convo) clearTimeout(convo.timeout);
const timeout = setTimeout(() => this.convos.delete(msg.channel.id), 600000);
this.convos.set(msg.channel.id, { cs: body.cs, timeout });
return msg.reply(body.output || blankResponses[Math.floor(Math.random() * blankResponses.length)]);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
run(msg) {
if (this.client.cleverbots.has(msg.channel.id)) {
return msg.say('There is already a Cleverbot conversation in this channel.');
}
const usage = this.client.registry.commands.get('cleverbot-end').usage();
return msg.reply(stripIndents`
Cleverbot is now active in this channel, replying to ${msg.author}.
To end the conversation, use ${usage}.
`);
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "131.4.0",
"version": "131.5.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+49
View File
@@ -0,0 +1,49 @@
const request = require('node-superfetch');
const { CLEVERBOT_KEY } = process.env;
const blankResponses = ['What?', 'Huh?', 'I don\'t understand.', 'Speak up, please.'];
module.exports = class Cleverbot {
constructor(client, channelID, authorID, key = CLEVERBOT_KEY) {
Object.defineProperty(this, 'client', { value: client });
this.channelID = channelID;
this.authorID = authorID;
this.cs = null;
this.timeout = this.setTimeout();
this.key = key;
}
async respond(input) {
const { body } = await request
.get('https://www.cleverbot.com/getreply')
.query({
key: this.key,
cs: this.cs || '',
input
});
clearTimeout(this.timeout);
this.timeout = this.setTimeout();
this.cs = body.cs;
return body.output || blankResponses[Math.floor(Math.random() * blankResponses.length)];
}
shouldRespond(msg) {
return msg.channel.id === this.channelID && msg.author.id === this.authorID;
}
setTimeout() {
return setTimeout(() => {
this.manager.delete(this.channelID);
if (!this.channel) return;
this.channel.send('Conversation timed out.').catch(() => null);
}, 600000);
}
get channel() {
return this.client.channels.cache.get(channelID);
}
get author() {
return this.client.users.cache.get(authorID);
}
};
+1
View File
@@ -39,6 +39,7 @@ module.exports = class XiaoClient extends CommandoClient {
this.pokemon = new PokemonStore();
this.games = new Collection();
this.dispatchers = new Map();
this.cleverbots = new Map();
this.phone = new PhoneManager(this);
this.activities = activities;
this.leaveMessages = leaveMsgs;