nodbot/functions.js

726 lines
23 KiB
JavaScript
Raw Permalink Normal View History

2021-09-22 17:15:31 +00:00
/* eslint-disable comma-dangle */
// dotenv for handling environment variables
const dotenv = require('dotenv');
dotenv.config();
// Assignment of environment variables for database access
2021-09-22 17:15:31 +00:00
const dbHost = process.env.dbHost;
const dbUser = process.env.dbUser;
const dbName = process.env.dbName;
const dbPass = process.env.dbPass;
const dbPort = process.env.dbPort;
const isDev = process.env.isDev;
2021-09-22 17:15:31 +00:00
const ownerId = process.env.ownerId;
2021-09-22 17:15:31 +00:00
// filesystem
2021-06-28 03:20:21 +00:00
const fs = require('fs');
2021-09-22 17:15:31 +00:00
// Discord.js
2021-09-22 17:15:31 +00:00
const Discord = require('discord.js');
// Fuzzy text matching for db lookups
const FuzzySearch = require('fuzzy-search');
2023-05-28 00:41:08 +00:00
// OpenAI
2024-06-15 02:06:54 +00:00
// const OpenAI = require("openai");
// const openai = new OpenAI();
2023-05-28 00:41:08 +00:00
// Various imports from other files
const config = require('./config.json');
2021-09-22 17:15:31 +00:00
const strings = require('./strings.json');
const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js'));
const dotCommandFiles = fs.readdirSync('./dot-commands/').filter(file => file.endsWith('.js'));
// MySQL database connection
2021-09-22 17:15:31 +00:00
const mysql = require('mysql');
const { GifData, PastaData } = require('./CustomModules/NodBot');
2022-06-09 22:22:42 +00:00
const db = new mysql.createPool({
connectionLimit: 10,
host: dbHost,
user: dbUser,
password: dbPass,
database: dbName,
port: dbPort,
});
2021-06-28 03:20:21 +00:00
2021-09-22 17:15:31 +00:00
const functions = {
// Functions for managing and creating Collections
2021-09-22 17:15:31 +00:00
collections: {
// Create the collection of slash commands
2021-09-22 17:15:31 +00:00
slashCommands(client) {
if (!client.slashCommands) client.slashCommands = new Discord.Collection();
client.slashCommands.clear();
for (const file of slashCommandFiles) {
const slashCommand = require(`./slash-commands/${file}`);
if (slashCommand.data != undefined) {
client.slashCommands.set(slashCommand.data.name, slashCommand);
}
}
if (isDev) console.log('Slash Commands Collection Built');
2021-09-22 17:15:31 +00:00
},
setvalidCommands(client) {
for (const entry of client.dotCommands.map(command => command)) {
config.validCommands.push(entry.name);
if (Array.isArray(entry.alias)) {
entry.alias.forEach(element => {
config.validCommands.push(element);
});
} else if (entry.alias != undefined) {
config.validCommands.push(entry.alias);
}
2021-09-22 17:15:31 +00:00
}
2022-12-02 20:43:46 +00:00
if (isDev) console.log(`Valid Commands Added to Config\n${config.validCommands}`);
2021-09-22 17:15:31 +00:00
},
dotCommands(client) {
if (!client.dotCommands) client.dotCommands = new Discord.Collection();
client.dotCommands.clear();
for (const file of dotCommandFiles) {
const dotCommand = require(`./dot-commands/${file}`);
client.dotCommands.set(dotCommand.name, dotCommand);
if (Array.isArray(dotCommand.alias)) {
dotCommand.alias.forEach(element => {
client.dotCommands.set(element, dotCommand);
});
} else if (dotCommand.alias != undefined) {
client.dotCommands.set(dotCommand.alias, dotCommand);
}
2021-09-22 17:15:31 +00:00
}
if (isDev) console.log('Dot Commands Collection Built');
2021-09-22 17:15:31 +00:00
},
gifs(rows, client) {
if (!client.gifs) client.gifs = new Discord.Collection();
client.gifs.clear();
for (const row of rows) {
// const gif = {
// id: row.id,
// name: row.name,
// embed_url: row.embed_url
// };
const gifData = new GifData().setInfo(row.name, row.embed_url);
client.gifs.set(gifData.name, gifData);
2021-09-22 17:15:31 +00:00
}
if (isDev) console.log('GIFs Collection Built');
2021-09-22 17:15:31 +00:00
},
joints(rows, client) {
if (!client.joints) client.joints = new Discord.Collection();
client.joints.clear();
for (const row of rows) {
const joint = {
id: row.id,
content: row.content
};
client.joints.set(joint.id, joint);
}
if (isDev) console.log('Joints Collection Built');
2021-09-22 17:15:31 +00:00
},
pastas(rows, client) {
if (!client.pastas) client.pastas = new Discord.Collection();
client.pastas.clear();
for (const row of rows) {
const pastaData = new PastaData().setInfo(row.name, row.content, row.iconurl, row.id);
client.pastas.set(pastaData.name, pastaData);
2021-09-22 17:15:31 +00:00
}
if (isDev) console.log('Pastas Collection Built');
2021-09-22 17:15:31 +00:00
},
requests(rows, client) {
if (!client.requests) client.requests = new Discord.Collection();
client.requests.clear();
for (const row of rows) {
const request = {
id: row.id,
author: row.author,
request: row.request,
};
client.requests.set(request.id, request);
}
if (isDev) console.log('Requests Collection Built');
2021-09-22 17:15:31 +00:00
},
strains(rows, client) {
if (!client.strains) client.strains = new Discord.Collection();
client.strains.clear();
for (const row of rows) {
const strain = {
id: row.id,
2022-12-02 20:43:46 +00:00
name: row.strain,
2021-09-22 17:15:31 +00:00
};
client.strains.set(strain.name, strain);
2022-12-02 20:43:46 +00:00
// if (isDev) console.log(strain)
2021-09-22 17:15:31 +00:00
}
if (isDev) console.log('Strains Collection Built');
2023-01-07 17:28:55 +00:00
},
medicalAdvice(rows, client) {
if (!client.medicalAdviceColl) client.medicalAdviceColl = new Discord.Collection();
2023-01-07 17:28:55 +00:00
client.medicalAdviceColl.clear();
for (const row of rows) {
const medicalAdvice = {
id: row.id,
content: row.content
};
client.medicalAdviceColl.set(medicalAdvice.id, medicalAdvice);
}
if (isDev) console.log('Medical Advice Collection Built');
},
roaches(client) {
if (!client.roaches) client.roaches = new Discord.Collection();
client.roaches.clear();
if (isDev) console.log('Medical Advice Collection Built');
}
},
2021-09-22 17:15:31 +00:00
dot: {
getCommandData(message) {
const commandData = {};
// Split the message content at the final instance of a period
const finalPeriod = message.content.lastIndexOf('.');
// if(isDev) console.log(message.content);
// If the final period is the last character, or doesn't exist
2022-06-20 23:00:42 +00:00
if (finalPeriod < 0) {
if (isDev) console.log(finalPeriod);
2021-09-22 17:15:31 +00:00
commandData.isCommand = false;
return commandData;
}
commandData.isCommand = true;
// Get the first part of the message, everything leading up to the final period
2021-09-22 21:14:14 +00:00
commandData.args = message.content.slice(0,finalPeriod).toLowerCase();
// Get the last part of the message, everything after the final period
commandData.command = message.content.slice(finalPeriod + 1).toLowerCase();
commandData.author = `${message.author.username}`;
2021-09-22 17:15:31 +00:00
return this.checkCommand(commandData);
},
checkCommand(commandData) {
if (commandData.isCommand) {
const validCommands = require('./config.json').validCommands;
commandData.isValid = validCommands.includes(commandData.command);
// Add exceptions for messages that contain only a link
if (commandData.args.startsWith('http')) commandData.isValid = false;
2021-09-22 17:15:31 +00:00
}
else {
commandData.isValid = false;
console.error('Somehow a non-command made it to checkCommands()');
}
return commandData;
2021-06-28 03:20:21 +00:00
}
},
2021-09-22 17:15:31 +00:00
embeds: {
help(interaction) {
// Construct the Help Embed
const helpEmbed = new Discord.MessageEmbed()
.setColor('BLUE')
.setAuthor({name: 'Help Page'})
2021-09-22 17:15:31 +00:00
.setDescription(strings.help.description)
.setThumbnail(strings.urls.avatar);
// Construct the Slash Commands help
let slashCommandsFields = [];
const slashCommandsMap = interaction.client.slashCommands.map(e => {
return {
name: e.data.name,
description: e.data.description
};
})
2021-09-22 17:15:31 +00:00
for (const e of slashCommandsMap) {
slashCommandsFields.push({
name: `- /${e.name}`,
value: e.description,
inline: false,
});
2021-07-19 02:23:40 +00:00
}
2021-09-22 17:15:31 +00:00
// Construct the Dot Commands Help
let dotCommandsFields = [];
const dotCommandsMap = interaction.client.dotCommands.map(e => {
return {
name: e.name,
description: e.description,
usage: e.usage
};
2021-07-19 02:23:40 +00:00
});
2021-09-22 17:15:31 +00:00
for (const e of dotCommandsMap) {
dotCommandsFields.push({
name: `- .${e.name}`,
value: `${e.description}\nUsage: ${e.usage}`,
inline: false,
});
}
2021-09-22 17:15:31 +00:00
helpEmbed.addField('Slash Commands', strings.help.slash);
helpEmbed.addFields(slashCommandsFields);
helpEmbed.addField('Dot Commands', strings.help.dot);
helpEmbed.addFields(dotCommandsFields);
2022-06-09 22:29:23 +00:00
return { embeds: [
helpEmbed
2022-06-10 01:27:43 +00:00
], ephemeral: true };
2021-09-22 17:15:31 +00:00
},
gif(commandData) {
return { embeds: [new Discord.MessageEmbed()
.setAuthor({name: `${commandData.args}.${commandData.command}`})
2021-09-22 17:15:31 +00:00
.setImage(commandData.embed_url)
.setTimestamp()
.setFooter({text: commandData.author})]};
2021-09-22 17:15:31 +00:00
},
pasta(commandData, pastaData) {
2021-09-22 17:15:31 +00:00
return { embeds: [ new Discord.MessageEmbed()
.setAuthor({name: `${commandData.args}.${commandData.command}`})
.setDescription(pastaData.content)
2023-12-13 21:49:48 +00:00
.setThumbnail("https://assets.vfsh.dev/shednod.png")
2021-09-22 17:15:31 +00:00
.setTimestamp()
.setFooter({text: commandData.author})]};
2021-09-22 17:15:31 +00:00
},
pastas(commandData) {
const pastasArray = [];
const pastasEmbed = new Discord.MessageEmbed()
.setAuthor({name: commandData.command})
2021-09-22 17:15:31 +00:00
.setTimestamp()
.setFooter({text: commandData.author});
2021-09-22 17:15:31 +00:00
for (const row of commandData.pastas) {
pastasArray.push(`#${row.id} - ${row.name}.pasta`);
}
const pastasString = pastasArray.join('\n');
pastasEmbed.setDescription(pastasString);
2022-06-11 21:35:08 +00:00
return { embeds: [pastasEmbed], ephemeral: true };
2021-09-22 17:15:31 +00:00
},
gifs(commandData, gifList) {
2021-09-22 17:15:31 +00:00
const gifsEmbed = new Discord.MessageEmbed()
.setAuthor({name: commandData.command})
2021-09-22 17:15:31 +00:00
.setTimestamp()
.setFooter({text: commandData.author})
.setDescription(gifList.join('\n'));
2021-09-22 17:15:31 +00:00
return { embeds: [gifsEmbed] };
},
text(commandData) {
return { embeds: [new Discord.MessageEmbed()
.setAuthor({name: commandData.command})
2021-09-22 17:15:31 +00:00
.setDescription(commandData.content)
.setTimestamp()
.setFooter({text: commandData.author})]};
2021-09-22 17:15:31 +00:00
},
requests(commandData) {
const requestsEmbed = new Discord.MessageEmbed()
.setAuthor({name: commandData.command})
2021-09-22 17:15:31 +00:00
.setTimestamp()
.setFooter({text: commandData.author});
2021-09-22 17:15:31 +00:00
const requestsArray = [];
for (const row of commandData.requests) {
requestsArray.push(
`**#${row.id} - ${row.author}**`,
`Request: ${row.request}`
);
}
requestsEmbed.setDescription(requestsArray.join('\n'));
2022-06-11 02:48:29 +00:00
return { embeds: [requestsEmbed], ephemeral: true };
2021-09-22 17:15:31 +00:00
},
2023-01-08 01:00:15 +00:00
strain(strainInfo, interaction) {
2021-09-22 17:15:31 +00:00
const strainEmbed = new Discord.MessageEmbed()
2023-01-08 01:00:15 +00:00
.setTimestamp();
2021-09-22 17:15:31 +00:00
strainEmbed.addFields([
{
name: 'Strain Name',
2022-12-02 20:43:46 +00:00
value: `${strainInfo.strain}`,
inline: true,
2021-09-22 17:15:31 +00:00
},
{
name: 'Type',
value: `${strainInfo.type}`,
inline: true,
},
{
name: 'Effects',
value: `${strainInfo.effects}`,
inline: true,
},
{
2022-12-02 20:43:46 +00:00
name: 'Flavor',
value: `${strainInfo.flavor}`,
2021-09-22 17:15:31 +00:00
inline: true,
},
{
2022-12-02 20:43:46 +00:00
name: 'Rating',
2023-01-07 20:27:47 +00:00
value: `⭐️${strainInfo.rating}`,
2021-09-22 17:15:31 +00:00
inline: true,
},
2022-12-02 20:43:46 +00:00
{
name: 'Description',
value: `${strainInfo.description}`,
inline: false,
},
2021-09-22 17:15:31 +00:00
]);
2023-01-08 01:00:15 +00:00
interaction.reply({ embeds: [ strainEmbed ]});
2021-09-22 17:15:31 +00:00
},
2023-05-31 12:07:12 +00:00
dalle(prompt, imageUrl, size) {
const dalleEmbed = new Discord.MessageEmbed()
2023-05-29 13:22:11 +00:00
.setAuthor({ name: "NodBot powered by DALL-E", iconURL: "https://assets.vfsh.dev/openai-logos/PNGs/openai-logomark.png" })
.addFields(
{ name: "Prompt", value: prompt }
)
.setImage(imageUrl)
2023-05-31 12:07:12 +00:00
.setFooter({ text: `This ${size} image cost ${strings.costs.dalle[size]}¢ to generate.` })
return { embeds: [dalleEmbed] };
2023-05-29 13:22:11 +00:00
},
2023-05-31 12:07:12 +00:00
gpt(prompt, response, usage) {
2023-05-29 13:22:11 +00:00
const gptEmbed = new Discord.MessageEmbed()
2023-10-18 21:33:03 +00:00
.setAuthor({ name: "NodBot powered by GPT-3.5", iconURL: "https://assets.vfsh.dev/openai-logos/PNGs/openai-logomark.png" })
2023-05-31 12:07:12 +00:00
.setDescription(`**Prompt**\n${prompt}\n\n**Response**\n${response}`)
2023-10-18 21:33:03 +00:00
.setFooter({ text: `This prompt used ${usage.tokens} tokens for a cost of ${usage.usdc}¢. Generated using ${strings.ai.chatModel}` })
2023-05-29 13:22:11 +00:00
return { embeds: [gptEmbed] };
2023-06-15 23:07:16 +00:00
},
generatingResponse() {
const embed = new Discord.MessageEmbed()
.setAuthor({ name: "NodBot powered by OpenAI", iconURL: "https://assets.vfsh.dev/openai-logos/PNGs/openai-logomark.png" })
2023-06-15 23:16:50 +00:00
.setImage("https://media.tenor.com/aHMHzNGCb4kAAAAC/sucks.gif")
2023-06-15 23:07:16 +00:00
.setDescription("Generating a response, please stand by.")
.setFooter({ text: "Ligma balls" });
return { embeds: [embed] };
}
},
2021-09-22 17:15:31 +00:00
collect: {
gifName(interaction) {
const gifNameFilter = m => m.author.id == strings.temp.gifUserId;
return interaction.channel.createMessageCollector({ filter: gifNameFilter, time: 30000 });
},
},
2021-09-22 17:15:31 +00:00
upload: {
request(commandData, client) {
2022-06-01 17:40:27 +00:00
const query = `INSERT INTO requests (author, request, status) VALUES (${db.escape(commandData.author)},${db.escape(commandData.args)},'Active')`;
2021-09-22 17:15:31 +00:00
db.query(query, (err, rows, fields) => {
if (err) throw err;
functions.download.requests(client);
});
},
2023-12-13 21:49:48 +00:00
async pasta(pastaData, client) {
const query = `INSERT INTO pastas (name, content) VALUES (${db.escape(pastaData.name)},${db.escape(pastaData.content)}) ON DUPLICATE KEY UPDATE content=${db.escape(pastaData.content)}`;
2023-12-13 21:49:48 +00:00
await db.query(query, (err, rows, fields) => {
2021-09-22 17:15:31 +00:00
if (err) throw err;
functions.download.pastas(client);
});
2023-12-13 21:49:48 +00:00
return;
2021-09-22 17:15:31 +00:00
},
joint(content, client) {
2022-06-01 17:40:27 +00:00
const query = `INSERT INTO joints (content) VALUES (${db.escape(content)})`;
2021-09-22 17:15:31 +00:00
db.query(query, (err, rows, fields) => {
if (err) throw err;
functions.download.joints(client);
});
},
async gif(gifData, client) {
2023-08-08 19:06:29 +00:00
const query = `INSERT INTO gifs (name, embed_url) VALUES (${db.escape(gifData.name)}, ${db.escape(gifData.url)}) ON DUPLICATE KEY UPDATE embed_url=${db.escape(gifData.url)}`;
await db.query(query, (err, rows, fields) => {
2021-09-22 17:15:31 +00:00
if (err) throw err;
functions.download.gifs(client);
});
2022-06-11 22:10:49 +00:00
},
setup(interaction) {
/* Tables:
* - gifs
* - joints
* - pastas
* - requests
* - strains */
const gifsQuery = "CREATE TABLE 'gifs' (id int(11), name varchar(100), embed_url varchar(1000), PRIMARY KEY(id))";
const jointsQuery = "CREATE TABLE 'joints' (id int(11), content varchar(1000), PRIMARY KEY(id))";
const pastasQuery = "CREATE TABLE 'pastas' (id int(11), name varchar(100), content varchar(1900), iconurl varchar(200) DEFAULT 'https://cdn.discordapp.com/avatars/513184762073055252/12227aa23a06d5178853e59b72c7487b.webp?size=128', PRIMARY KEY(id))";
const requestsQuery = "CREATE TABLE 'requests' (id int(11), author varchar(100), request varchar(1000), status varchar(10) DEFAULT 'Active', PRIMARY KEY(id))";
const strainsQuery = "CREATE TABLE 'strains' (id smallint(6), name varchar(60), type varchar(10), effects varchat(80), ailment varchar(70), flavor varchar(30), PRIMARY KEY(id))";
// Check for owner
if (interaction.user.id == ownerId) {
db.query(gifsQuery, (err, rows, fields) => {
if (err) throw err;
});
db.query(jointsQuery, (err, rows, fields) => {
if (err) throw err;
});
db.query(pastasQuery, (err, rows, fields) => {
if (err) throw err;
});
db.query(requestsQuery, (err, rows, fields) => {
if (err) throw err;
});
db.query(strainsQuery, (err, rows, fields) => {
if (err) throw err;
});
return 'I\'ve created the required tables. Please check your database to validate this.';
} else {
return 'Sorry, you don\'t have permission to do that.';
}
2023-01-07 17:28:55 +00:00
},
medicalAdvice(content, client) {
const query = `INSERT INTO medical_advice (content) VALUES (${db.escape(content)})`;
db.query(query, (err, rows, fields) => {
if (err) throw err;
functions.download.medicalAdvice(client);
});
2023-01-07 20:27:47 +00:00
},
strain(interaction) {
const strain = db.escape(interaction.options.getString('name'));
const type = db.escape(interaction.options.getString('type'));
const effects = db.escape(( interaction.options.getString('effects') || 'Unkown' ));
const description = db.escape(( interaction.options.getString('description') || 'Unknown' ));
const flavor = db.escape(( interaction.options.getString('flavor') || 'Unknown' ));
const rating = db.escape(( interaction.options.getString('rating') || '3' ));
const strainQuery = `INSERT INTO strains (strain, type, effects, description, flavor, rating) VALUES (${strain}, ${type}, ${effects}, ${description}, ${flavor}, ${rating}) ON DUPLICATE KEY UPDATE strain=${db.escape(strain)}, type=${db.escape(type)}, effects=${db.escape(effects)}, description=${db.escape(description)}, flavor=${db.escape(flavor)}, rating=${db.escape(rating)}`;
2023-01-07 20:27:47 +00:00
console.log(strainQuery);
return new Promise((resolve, reject) => {
db.query(strainQuery, (err, rows, fields) => {
if (err) reject(err);
functions.download.strains(interaction.client);
resolve();
});
})
2023-05-31 12:33:03 +00:00
},
openai(user, prompt, engine, tokens, usdc) {
const query = `INSERT INTO openai (user, prompt, engine, tokens, usdc) VALUES (${db.escape(user)}, ${db.escape(prompt)}, ${db.escape(engine)}, ${db.escape(tokens)}, ${db.escape(usdc)})`;
db.query(query, (err) => {
if (err) throw err;
});
2021-09-22 17:15:31 +00:00
}
},
2021-09-22 17:15:31 +00:00
download: {
requests(client) {
2023-01-08 01:57:03 +00:00
const query = 'SELECT * FROM requests WHERE status = \'Active\' ORDER BY id DESC';
2021-09-22 17:15:31 +00:00
db.query(query, (err, rows, fields) => {
if (err) throw err;
functions.collections.requests(rows, client);
});
},
pastas(client) {
const query = 'SELECT * FROM pastas ORDER BY id ASC';
db.query(query, (err, rows, fields) => {
if (err) throw err;
functions.collections.pastas(rows, client);
});
},
async gifs(client) {
2021-09-22 17:15:31 +00:00
const query = 'SELECT * FROM gifs ORDER BY id ASC';
await db.query(query, (err, rows, fields) => {
2021-09-22 17:15:31 +00:00
if (err) throw err;
functions.collections.gifs(rows, client);
});
2021-09-22 17:15:31 +00:00
},
joints(client) {
const query = 'SELECT * FROM joints ORDER BY id ASC';
db.query(query, (err, rows, fields) => {
if (err) throw err;
functions.collections.joints(rows, client);
});
},
2023-01-08 01:00:15 +00:00
strain(strainName, interaction) {
2022-12-02 20:43:46 +00:00
const query = `SELECT id, strain, type, effects, description, flavor, rating FROM strains WHERE strain = ${db.escape(strainName)}`;
2021-09-22 17:15:31 +00:00
db.query(query, (err, rows, fields) => {
if (rows != undefined) {
2023-01-08 01:00:15 +00:00
const strainInfo = {
2021-09-22 17:15:31 +00:00
id: `${rows[0].id}`,
2022-12-02 20:43:46 +00:00
strain: `${rows[0].strain}`,
2021-09-22 17:15:31 +00:00
type: `${rows[0].type}`,
effects: `${rows[0].effects}`,
2022-12-02 20:43:46 +00:00
description: `${rows[0].description}`,
2021-09-22 17:15:31 +00:00
flavor: `${rows[0].flavor}`,
2022-12-02 20:43:46 +00:00
rating: `${rows[0].rating}`,
2021-09-22 17:15:31 +00:00
};
2023-01-08 01:00:15 +00:00
functions.embeds.strain(strainInfo, interaction);
2021-09-22 17:15:31 +00:00
}
});
},
strains(client) {
2022-12-02 20:43:46 +00:00
const query = 'SELECT id, strain FROM strains';
2021-09-22 17:15:31 +00:00
db.query(query, (err, rows, fields) => {
if (err) throw err;
functions.collections.strains(rows, client);
});
},
2023-01-07 17:28:55 +00:00
medicalAdvice(client) {
const query = 'SELECT * FROM medical_advice ORDER BY id ASC';
db.query(query, (err, rows, fields) => {
if (err) throw err;
functions.collections.medicalAdvice(rows, client);
});
}
2021-09-22 17:15:31 +00:00
},
weed: {
strain: {
lookup(strainName, client) {
const strainSearcher = new FuzzySearch(client.strains.map(e => e.name));
2023-01-08 01:00:15 +00:00
return strainSearcher.search(strainName).slice(0,25);
2021-09-22 17:15:31 +00:00
},
submit(strainName) {
2022-12-11 18:03:38 +00:00
const query = ``
2021-09-22 17:15:31 +00:00
return strainName;
}
}
},
2023-05-28 00:41:08 +00:00
openAI: {
chatPrompt(userPrompt) {
return new Promise(async (resolve, reject) => {
2023-10-18 21:33:03 +00:00
const response = await openai.chat.completions.create({
messages: [{
role: 'user',
content: userPrompt
}],
model: strings.ai.chatModel
2023-05-28 00:41:08 +00:00
}).catch(e => {
reject(e);
return null;
});
2023-10-18 21:33:03 +00:00
resolve(response);
2023-05-28 00:41:08 +00:00
});
2023-05-28 14:08:51 +00:00
},
imagePrompt(userPrompt, size, userId) {
2023-05-28 14:08:51 +00:00
return new Promise(async (resolve, reject) => {
try {
const response = await openai.createImage({
prompt: userPrompt,
size: size,
2023-05-28 14:08:51 +00:00
user: userId
});
resolve(response.data.data[0].url);
} catch (e) {
reject(e);
return;
}
});
2023-05-28 00:41:08 +00:00
}
},
search: {
gifs(query, client) {
const gifSearcher = new FuzzySearch(client.gifs.map(element => element.name));
return gifSearcher.search(query).slice(0,25);
},
pastas(query, client) {
const pastaSearcher = new FuzzySearch(client.pastas.map(element => element.name));
return pastaSearcher.search(query).slice(0,25);
}
},
2021-09-22 17:15:31 +00:00
// Parent-Level functions (miscellaneuous)
closeRequest(requestId, interaction) {
if (interaction.user.id == ownerId) {
const { client } = interaction;
const query = `UPDATE requests SET status = 'Closed' WHERE id = ${db.escape(requestId)}`;
db.query(query, (err, rows, fields) => {
if (err) throw err;
functions.download.requests(client);
});
interaction.reply({ content: `Request #${requestId} has been closed.`, ephemeral: true });
} else {
interaction.reply({ content: 'You do not have permission to do that.', ephemeral: true });
}
if (isDev) {
console.log(requestId, interaction, ownerId);
}
2021-09-22 17:15:31 +00:00
},
2021-09-24 00:08:29 +00:00
spongebob(commandData) {
let newText = '';
2023-08-06 12:51:28 +00:00
let lastIsUpper = 0;
2021-09-24 00:08:29 +00:00
for (const letter of commandData.args) {
if (letter == ' ') {
2022-12-16 19:56:00 +00:00
newText += letter;
2021-09-24 00:08:29 +00:00
continue;
}
if (letter == 'i' || letter == 'I') {
2022-12-16 19:56:00 +00:00
newText += 'i';
2023-08-06 12:51:28 +00:00
lastIsUpper = 0;
2021-09-24 00:08:29 +00:00
continue;
}
if (letter == 'l' || letter == 'L') {
2022-12-16 19:56:00 +00:00
newText += 'L';
2023-08-06 12:51:28 +00:00
lastIsUpper = 1;
2021-09-24 00:08:29 +00:00
continue;
}
2023-08-06 12:51:28 +00:00
if (lastIsUpper === 0) {
2022-12-16 19:56:00 +00:00
newText += letter.toUpperCase();
2023-08-06 13:04:24 +00:00
lastIsUpper = 1;
2021-09-24 00:08:29 +00:00
} else {
2022-12-16 19:56:00 +00:00
newText += letter.toLowerCase();
2023-08-06 13:04:24 +00:00
lastIsUpper = 0;
2021-09-24 00:08:29 +00:00
}
}
2022-12-16 20:06:40 +00:00
return newText + ' <:spongebob:1053398825965985822>';
2021-09-24 00:08:29 +00:00
},
autoresponses: { // Specific responses for certain keywords in sent messages
checkForAll(messageContent) {
let responses = [];
if (this.bigDoinks(messageContent)) responses.push("bigDoinks");
if (this.ligma(messageContent)) responses.push("ligma");
if (this.ong(messageContent)) responses.push("ong");
2024-06-21 19:11:34 +00:00
if (this.fuckYou(messageContent)) responses.push("fuckYou");
return responses;
},
bigDoinks(messageContent) {
let count = 0;
const { keywords } = strings.autoresponses.bigDoinks;
keywords.forEach(e => {
if (messageContent.includes(e)) count++;
});
if (count === keywords.length) {
return true;
}
},
ligma(messageContent) {
let count = 0;
const { keywords } = strings.autoresponses.ligma;
keywords.forEach(e => {
if (messageContent.includes(e)) count++;
});
if (count > 0) {
return true;
}
},
ong(messageContent) {
let count = 0;
const { keywords } = strings.autoresponses.ong;
keywords.forEach(e => {
if (messageContent.includes(e)) count++;
});
if (count > 0) {
return true;
}
},
2024-06-21 19:11:34 +00:00
fuckYou(messageContent) {
let count = 0;
const { keywords } = strings.autoresponses.fuckYou;
keywords.forEach(e => {
if (messageContent.includes(e)) count++;
});
if (count === keywords.length) {
return true;
}
},
send(message, responseType) {
const { responses } = strings.autoresponses[responseType];
const randomIndex = Math.floor(Math.random() * responses.length);
const response = responses[randomIndex];
try {
message.reply(response);
} catch(e) {
console.log(new Error(e));
}
}
},
2023-05-28 14:08:51 +00:00
generateErrorId() {
const digitCount = 10;
const digits = [];
for (let i = 0; i < digitCount; i++) {
const randBase = Math.random();
const randNumRaw = randBase * 10;
const randNumRound = Math.floor(randNumRaw);
digits.push(randNumRound);
}
const errorId = digits.join("");
return errorId;
}
2021-09-22 17:15:31 +00:00
};
module.exports = functions;