Added more moderation commands and upgraded the 'audio' command

This commit is contained in:
ION606
2022-07-12 20:10:35 +03:00
parent 6ceb8cb381
commit 5a17e3811f
16 changed files with 881 additions and 114 deletions
+109
View File
@@ -0,0 +1,109 @@
const { MongoClient, ServerApiVersion, ConnectionClosedEvent } = require('mongodb');
const { exit } = require('process');
const { checkResponses } = require('./wordlist.js');
//Error checking function (message deleted error fix, workaround already applied but...)
//message.channel.send("Oops, there's been an error, please contact support!");
async function messageExists(message) {
return new Promise((resolve, reject) => {
try {
message.channel.messages.fetch(message.id)
.then((fetchedMessage) => {
resolve(true);
})
.catch((err) => {
console.log(err);
resolve(false);
})
} catch (err) { resolve(false); }
resolve(true);
});
}
async function getResponse(convo, bot) {
const response = await bot.openai.createCompletion({
model: "text-davinci-002",
prompt: convo,
temperature: 0.9,
max_tokens: 150,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0.6,
stop: [" Human:", " AI:"],
});
return response;
}
async function convoManager(clientinp, bot, message) {
//Just in case, make sure it can't be changed
const client = clientinp;
const dbo = client.db("DM").collection(message.author.id);
if (message.content.startsWith('!')) {
if (message.content.split(' ')[0] == '!startconvo') {
//Check if a conversation already exists
dbo.find({'_id': {$exists: true}}).toArray((err, docs) => {
if (docs[0] != undefined) {
return message.channel.send("You're already in a conversation");
} else {
dbo.insertOne({convo: 'Human: Hello\nAI: Hello'});
return message.channel.send('-----Started Conversation-----\nuse _!endconvo_ to end the conversation!\n\n_Disclaimer: Your conversation data is stored for the duration of the conversation to help Selmer Bot better understand what you are saying *then deleted*_\n\n');
}
});
} else if (message.content.split(' ')[0] == '!endconvo') {
dbo.drop();
return message.channel.send('-----Ended Conversation-----\nSee you next time!');
} else {
return message.channel.send('UNUSABLE DM COMMAND DETECTED');
}
} else {
dbo.find({convo: {$exists: true}}).toArray(async function (err, docs) {
const doc = docs[0];
if (!doc) { return message.reply('You aren\'t currently in a conversation\nUse _!startconvo_ to start one!'); }
//Checking Section
const check = checkResponses(message.content, "I'm sorry, I can't do that");
if (check != null) { return message.reply(check); }
let convo = doc.convo;
convo += `\nHuman: ${message.content}\n`;;
//Get the response
const r = await getResponse(convo, bot);
let response = r.data.choices[0].text;
convo += (response + '\n');
dbo.updateOne(doc, {$set: {convo: convo}});
response = response.replaceAll('AI: ', '').replaceAll('AI:\n', '');
//Very buggy so I'm adding this for now
message.channel.send(response);
//Note: Work with the following later
/* messageExists(message).then((e) => {
console.log(e);
if (e) { return message.reply(response); }
message.channel.send(response);
}) */
});
}
}
//"Hello! discord_user:"
module.exports = {
name: 'chat',
description: 'chat',
convoManager,
execute(message, args, Discord, Client, bot) {
message.reply("Please DM Selmer bot to use this command!");
}
}
+184
View File
@@ -0,0 +1,184 @@
/*
-----WEBHOOKS ARE MONITORED AND PROCESSED HERE-----
https://glitch.com/edit/#!/selmer-bot-listener
--------------------------------------------------
*/
const { MongoClient, ServerApiVersion } = require('mongodb');
const { MessageActionRow, MessageSelectMenu } = require('discord.js');
//Called from the dropdown menu
async function createSubscriptionManual(bot, interaction, id, priceID) {
const stripe = bot.stripe;
const mongouri = bot.mongouri;
//Error Checking (unlikely, but just in case)
if (!id) { console.log('....What? How?'); return interaction.editReply("Uh oh, something happened with the Stripe Discord ID check, please contact support!"); }
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
new Promise(async function(resolve, reject) {
client.connect(async (err) => {
if (err) { return console.log(err); }
const dbo = client.db('main').collection('authorized');
await dbo.findOne({'discordID': id}).then(async (doc) => {
var userID;
if (doc != undefined) {
client.close();
reject(`An account with the tag <@${id}> already exists!`);
} else {
const stripeUser = await stripe.customers.create({
metadata: { 'discordID': id }
});
userID = stripeUser.id;
//Add to the database (I have to wait for the insertion)
await dbo.insertOne({stripeID: userID, discordID: id, paid: false, startDateUTC: null, tier: 0}).then(() => { client.close(); resolve(userID); });
}
});
});
}).then(async (userID) => {
//Deal with the session
const billingPortalSession = await stripe.billingPortal.sessions.create({
customer: userID,
return_url: "https://linktr.ee/selmerbot",
});
const session = await stripe.checkout.sessions.create(
{
payment_method_types: ["card"],
line_items: [
{
price: priceID,
quantity: 1,
},
],
customer: userID,
mode: "subscription",
success_url: billingPortalSession.url,
cancel_url: "https://linktr.ee/selmerbot"
});
interaction.editReply(session.url);
}).catch((err) => {
if (String(typeof(err)) == 'string') {
interaction.editReply(err);
} else {
console.log(err);
interaction.editReply("A Stripe error occured! Please contact support ASAP!")
}
});
}
async function changeSubscriptionManual(bot, message) {
const stripe = bot.stripe;
const mongouri = bot.mongouri;
const id = message.author.id;
//Just in case
if (!id) { return console.log('....What? How?'); }
const client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
new Promise(async function(resolve, reject) {
client.connect(async (err) => {
if (err) { return console.log(err); }
const dbo = client.db('main').collection('authorized');
await dbo.findOne({'discordID': id}).then(async (doc) => {
var userID;
if (doc != undefined) {
userID = doc.stripeID;
client.close();
resolve(userID);
} else {
client.close();
reject(`No user with the ID of <@${message.author.id}>`);
}
});
});
}).then(async (userID) => {
await stripe.billingPortal.sessions.create({
customer: userID,
return_url: "https://linktr.ee/selmerbot",
}).then((session) => {
message.reply(session.url);
})
}).catch((err) => {
if (String(typeof(err)) == 'string') {
message.reply(err);
} else {
console.log(err);
message.reply("A Stripe error occured! Please contact support ASAP!");
}
});
}
function createDropDown(bot, message) {
const stripe = bot.stripe;
const pl = [];
const vl = [];
stripe.products.list({
limit: 3,
}).then((prod) => {
prod.data.forEach((obj) => {
const pricePromise = stripe.prices.retrieve(obj.default_price);
var newObj = {label: obj.name, description: null, value: `${obj.default_price}`}
pl.push(pricePromise);
vl.push(newObj);
});
let n = Promise.all(pl);
let i = 0;
n.then((t) => {
t.forEach(data => {
let price = data.unit_amount/100;
vl[i].description = `The $${price} tier`;
i++;
});
const row = new MessageActionRow()
.addComponents(
new MessageSelectMenu()
.setCustomId(`${message.author.id}|premium`)
.setPlaceholder('Nothing selected')
.addOptions(vl)
);
message.channel.send({ content: `Please choose a tier`, components: [row] });
});
});
}
function handleInp(bot, message) {
if (message.content == '!premium' || message.content == '!premium help') {
message.reply('Use _!premium buy_ to get premium or use _!premium manage_ to change or cancel your subscription\n\n_Disclaimer: Selmer Bot uses Stripe to manage payments. Read more at *https://stripe.com/ *_');
} else if (message.content == '!premium buy') {
createDropDown(bot, message);
} else if (message.content == '!premium manage') {
changeSubscriptionManual(bot, message);
}
}
module.exports = {
name: 'premium',
description: 'everything payment',
execute(message, args, Discord, Client, bot) {
message.reply("Please DM Selmer bot to use this command!");
}, handleInp, createSubscriptionManual
}
+31
View File
@@ -0,0 +1,31 @@
//Remember to strip all non-alpha chars from string (including ' )
wordlist = {
pay: ['pay me', 'give me money', 'send money', 'send me money', 'send cash', 'PayPal me', 'venmo me', 'cashapp me', 'cash app me'],
name: ['what is your name', 'whats your name', 'what are you called']
}
function checkResponses(convoOG, answer) {
if (answer.indexOf("I'm sorry, I can't do that") == -1) { console.log('what?'); return 'none'}
// remove all uneccesary chars
convo = convoOG.replace(/\W/g, ' ').toLowerCase();
var b = 'none';
wordlist.name.forEach((w) => { if (convo.includes(w)) { b = 'name'; return }})
wordlist.pay.forEach((w) => { if (convo.includes(w)) { b = 'pay'; return }})
if (b === 'pay') {
//Exctract the number
// var amt = convoOG.match(/(\d+)/)[0];
// currency = convoOG[convoOG.indexOf(amt) - 1];
return ('Use _!premium_ to get Selmer Bot Premium now!');
} else if (b == 'name') {
return ('My name is Selmer Bot!');
} else { return null; }
return b;
}
module.exports = { checkResponses }