worked on premium

pull/11/head
TheCodedProf 3 years ago
parent 161136741c
commit 633866ff68

@ -1,3 +1,4 @@
Role all (?) Role all (?)
Server rules Server rules
verificationRequired on welcome verificationRequired on welcome
Role User GUI

@ -25,23 +25,23 @@ const callback = async (interaction: CommandInteraction): Promise<void> => {
], components: [new ActionRowBuilder<ButtonBuilder>().addComponents(new ButtonBuilder().setStyle(ButtonStyle.Link).setLabel("Join").setURL("https://discord.gg/bPaNnxe"))] }); ], components: [new ActionRowBuilder<ButtonBuilder>().addComponents(new ButtonBuilder().setStyle(ButtonStyle.Link).setLabel("Join").setURL("https://discord.gg/bPaNnxe"))] });
return; return;
} }
const dbMember = await client.database.premium.fetchTotal(interaction.user.id) const dbMember = await client.database.premium.fetchUser(interaction.user.id)
let premium; let premium = `You do not have premium! You can't activate premium on any servers.`;
let count = 0; let count = 0;
if (member.roles.cache.has("1066468879309750313")) { const {level, appliesTo} = dbMember || {level: 0, appliesTo: []}
if (level === 99) {
premium = `You have Infinite Premium! You have been gifted this by the developers as a thank you. You can give premium to any and all servers you are in.`; premium = `You have Infinite Premium! You have been gifted this by the developers as a thank you. You can give premium to any and all servers you are in.`;
count = 200; count = 200;
} else if (member.roles.cache.has("1066465491713003520")) { } else if (level === 1) {
premium = `You have Premium tier 1! You can give premium to ${1 - dbMember}.`; premium = `You have Premium tier 1! You can give premium to ${1 - appliesTo.length} more servers.`;
count = 1; count = 1;
} else if (member.roles.cache.has("1066439526496604194")) { } else if (level === 2) {
premium = `You have Premium tier 2! You can give premium to ${3 - dbMember}.`; premium = `You have Premium tier 2! You can give premium to ${3 - appliesTo.length} more servers.`;
count = 3; count = 3;
} else if (member.roles.cache.has("1066464134322978912")) { } else if (level === 3) {
premium = `You have Premium Mod! You already give premium to all servers you have a "manage" permission in.` premium = `You have Premium Mod! You can give premium to ${3 - appliesTo.length} more servers, as well as automatically giving premium to all servers you have a "manage" permission in.`
count = 3; count = 3;
} }
const hasPremium = await client.database.premium.hasPremium(interaction.guild!.id); const hasPremium = await client.database.premium.hasPremium(interaction.guild!.id);
let premiumGuild = "" let premiumGuild = ""
if (hasPremium) { if (hasPremium) {
@ -82,8 +82,7 @@ const callback = async (interaction: CommandInteraction): Promise<void> => {
if (i) { if (i) {
i.deferUpdate(); i.deferUpdate();
let guild = i.guild!; let guild = i.guild!;
let m = await client.database.premium.fetchTotal(interaction.user.id); if (count - appliesTo.length <= 0) {
if (count - m <= 0) {
interaction.editReply({ interaction.editReply({
embeds: [ embeds: [
new EmojiEmbed() new EmojiEmbed()

@ -1,5 +1,4 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import FormData from "form-data";
import fs, { writeFileSync, createReadStream } from "fs"; import fs, { writeFileSync, createReadStream } from "fs";
import generateFileName from "../utils/temp/generateFileName.js"; import generateFileName from "../utils/temp/generateFileName.js";
import Tesseract from "node-tesseract-ocr"; import Tesseract from "node-tesseract-ocr";
@ -20,8 +19,9 @@ export async function testNSFW(link: string): Promise<NSFWSchema> {
const [p, hash] = await saveAttachment(link); const [p, hash] = await saveAttachment(link);
let alreadyHaveCheck = await client.database.scanCache.read(hash) let alreadyHaveCheck = await client.database.scanCache.read(hash)
if(alreadyHaveCheck) return { nsfw: alreadyHaveCheck.data }; if(alreadyHaveCheck) return { nsfw: alreadyHaveCheck.data };
const data = new FormData(); const data = new URLSearchParams();
data.append("file", createReadStream(p)); let r = createReadStream(p)
data.append("file", r.read(fs.statSync(p).size));
const result = await fetch("https://unscan.p.rapidapi.com/", { const result = await fetch("https://unscan.p.rapidapi.com/", {
method: "POST", method: "POST",
headers: { headers: {

@ -1,6 +1,7 @@
import type Discord from "discord.js"; import type Discord from "discord.js";
import { Collection, MongoClient } from "mongodb"; import { Collection, MongoClient } from "mongodb";
import config from "../config/main.json" assert { type: "json" }; import config from "../config/main.json" assert { type: "json" };
import client from "../utils/client.js";
const mongoClient = new MongoClient(config.mongoUrl); const mongoClient = new MongoClient(config.mongoUrl);
await mongoClient.connect(); await mongoClient.connect();
@ -230,18 +231,55 @@ export class Premium {
this.premium = database.collection<PremiumSchema>("premium"); this.premium = database.collection<PremiumSchema>("premium");
} }
async updateUser(user: string, level: number) {
if(!(await this.userExists(user))) await this.createUser(user, level);
await this.premium.updateOne({ user: user }, { $set: { level: level } }, { upsert: true });
}
async userExists(user: string): Promise<boolean> {
const entry = await this.premium.findOne({ user: user });
return entry ? true : false;
}
async createUser(user: string, level: number) {
await this.premium.insertOne({ user: user, appliesTo: [], level: level });
}
async hasPremium(guild: string) { async hasPremium(guild: string) {
const entry = await this.premium.findOne({ const entry = await this.premium.findOne({
appliesTo: { $in: [guild] } appliesTo: { $in: [guild] }
}); });
if (!entry) return false; return entry ? true : false;
return entry.expires.valueOf() > Date.now();
} }
async fetchTotal(user: string): Promise<number> { async fetchUser(user: string): Promise<PremiumSchema | null> {
const entry = await this.premium.findOne({ user: user }); const entry = await this.premium.findOne({ user: user });
if (!entry) return 0; if (!entry) return null;
return entry.appliesTo.length; return entry;
}
async checkAllPremium() {
const entries = await this.premium.find({}).toArray();
for(const {user, expiresAt} of entries) {
if(expiresAt) expiresAt < new Date().getTime() ? await this.premium.deleteOne({user: user}) : null;
const member = await (await client.guilds.fetch("684492926528651336")).members.fetch(user)
let level: number = 0;
if (member.roles.cache.has("1066468879309750313")) {
level = 99;
} else if (member.roles.cache.has("1066465491713003520")) {
level = 1;
} else if (member.roles.cache.has("1066439526496604194")) {
level = 2;
} else if (member.roles.cache.has("1066464134322978912")) {
level = 3;
}
if (level > 0) {
await this.updateUser(user, level);
} else {
await this.premium.updateOne({ user: user }, { expiresAt: (new Date().getTime() + (1000*60*60*24*3)) })
}
}
} }
addPremium(user: string, guild: string) { addPremium(user: string, guild: string) {
@ -409,6 +447,6 @@ export interface ModNoteSchema {
export interface PremiumSchema { export interface PremiumSchema {
user: string; user: string;
level: number; level: number;
expires: Date;
appliesTo: string[]; appliesTo: string[];
expiresAt?: number;
} }

Loading…
Cancel
Save