Skip to content

Commit

Permalink
feat: improved ticketing
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlauer-Hax committed Jun 4, 2023
1 parent ea06537 commit 2113518
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 37 deletions.
60 changes: 46 additions & 14 deletions db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ export default class DB {
db: Db;
usercollection: Collection;
accesscollection: Collection;
servercollection: Collection;
usereventcollection: Collection;
constructor(url: string) {
this.client = new MongoClient(url);
this.db = this.client.db("one_bbn");
this.usercollection = this.db.collection("users");
this.accesscollection = this.db.collection("@bbn/hosting/access");
this.servercollection = this.db.collection("@bbn/hosting/servers");
this.usereventcollection = this.db.collection("user-events");
}

async connect() {
await this.client.connect();
}

async finduser(id: string) {
async finduser(discordid: string) {
const user = await this.usercollection.findOne({
authentication: {
$elemMatch: {
id,
id: discordid,
type: "oauth",
provider: "discord"
}
Expand All @@ -32,8 +36,8 @@ export default class DB {
return user._id;
}

async getCoins(id: string) {
const user = await this.finduser(id);
async getCoins(discordid: string) {
const user = await this.finduser(discordid);
if (!user) return null;
const access = await this.accesscollection.findOne({
owner: user
Expand All @@ -42,9 +46,9 @@ export default class DB {
return access.coins;
}

async setCoins(id: string, coins: number) {
async setCoins(discordid: string, coins: number) {
// check if user exists
const user = await this.finduser(id);
const user = await this.finduser(discordid);
if (!user) return null;
// update user
return await this.accesscollection.updateOne({
Expand All @@ -56,9 +60,9 @@ export default class DB {
});
}

async addCoins(id: string, coins: number) {
async addCoins(discordid: string, coins: number) {
// check if user exists
const user = await this.finduser(id);
const user = await this.finduser(discordid);
if (!user) return null;
// update user
return await this.accesscollection.updateOne({
Expand All @@ -70,9 +74,9 @@ export default class DB {
});
}

async removeCoins(id: string, coins: number) {
async removeCoins(discordid: string, coins: number) {
// check if user exists
const user = await this.finduser(id);
const user = await this.finduser(discordid);
if (!user) return null;
// update user
return await this.accesscollection.updateOne({
Expand All @@ -84,9 +88,9 @@ export default class DB {
});
}

async getLastDaily(id: string) {
async getLastDaily(discordid: string) {
// check if user exists
const user = await this.finduser(id);
const user = await this.finduser(discordid);
if (!user) return null;
const access = await this.accesscollection.findOne({
owner: user
Expand All @@ -95,8 +99,8 @@ export default class DB {
return access.lastDaily;
}

async setLastDaily(id: string, lastDaily: number) {
const user = await this.finduser(id);
async setLastDaily(discordid: string, lastDaily: number) {
const user = await this.finduser(discordid);
if (!user) return null;
return await this.accesscollection.updateOne({
owner: user
Expand All @@ -106,4 +110,32 @@ export default class DB {
}
});
}

async getServerURLs(discordid: string) {
const user = await this.finduser(discordid);
if (!user) return null;
const servers = await this.servercollection.find({
user: user
}).toArray();
return servers.map(server => `https://panel.bbn.one/server/${server.identifier}`);
}

async lastLogin(discordid: string) {
const user = await this.finduser(discordid);
if (!user) return null;
const userevent = await this.usereventcollection.findOne({
userId: user
}, {
sort: {
_id: -1
}
});
if (!userevent) return null;
const location = await fetch(`https://ipinfo.io/${userevent.ip}/json`).then(res => res.json());
return [{
platform: userevent.source.platform,
platformVersion: userevent.source.platformVersion,
legacyUserAgent: userevent.source.legacyUserAgent,
}, String.fromCodePoint(...(location.country as string).toUpperCase().split('').map(char => 127397 + char.charCodeAt(0))) + " " + location.city + " (" + location.timezone + ")", location.timezone];
}
}
70 changes: 47 additions & 23 deletions interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export async function handleInteraction(interaction: Interaction, db: DB) {

const user_reason = new TextInputBuilder()
.setCustomId("ticket_reason")
.setLabel(`Why do you want to open a ticket?`.substring(0, 45))
.setMinLength(30)
.setMaxLength(250)
.setLabel(`Why do you want to open a ticket?`)
.setRequired(true)
.setStyle(TextInputStyle.Paragraph);

Expand All @@ -46,20 +44,20 @@ export async function handleInteraction(interaction: Interaction, db: DB) {
}

if (interaction.isUserSelectMenu() && interaction.guild && interaction.customId === 'verify_modal') {
const member = interaction.guild.members.cache.get(interaction.values[ 0 ])
const member = interaction.guild.members.cache.get(interaction.values[0])
const role = interaction.guild.roles.cache.get("757983851032215673")

if (member && role) {
member.roles.add(role, "Verified by " + interaction.user.tag)
interaction.reply("Successfully verified <@" + interaction.values[ 0 ] + ">!")
interaction.reply("Successfully verified <@" + interaction.values[0] + ">!")
} else {
interaction.reply("An error occured while assigning the role to <@" + interaction.values[ 0 ] + ">")
interaction.reply("An error occured while assigning the role to <@" + interaction.values[0] + ">")
}
}

if (interaction.isModalSubmit()) {
const ticket_user_reason = interaction.fields.getTextInputValue("ticket_reason");

const dbuser = await db.finduser(interaction.user.id);
let ticketname = `ticket-${interaction.user.id}`;
await interaction.guild!.channels
.create({
Expand All @@ -70,19 +68,45 @@ export async function handleInteraction(interaction: Interaction, db: DB) {

})
.then(async (ch: TextChannel) => {
const fields = [
{
name: `Reason:`,
value: `> ${ticket_user_reason}`,
}
];
let embed = new EmbedBuilder()
.setColor("#5539cc")
.setTitle(`Ticket of ${interaction.user.username}`)
.addFields([
{
name: `Reason:`,
value: `> ${ticket_user_reason}`,
}
]);
.addFields(fields);
if (dbuser) {
const login = await db.lastLogin(interaction.user.id) || [];
embed.addFields({
name: `User ID:`,
value: `> ${dbuser.toHexString()}`,
}, {
name: `Server URLs:`,
value: `> ${await db.getServerURLs(interaction.user.id)}`,
}, {
name: `Last Login:`,
value: '```'+JSON.stringify(login[0])+'```',
});
embed.setFooter({
text: login[1] as string,
iconURL: interaction.user.displayAvatarURL(),
})
embed.setTimestamp(new Date(new Date().toLocaleString('en-US', { timeZone: login[2] })))
}


ch.permissionOverwrites.create(interaction.user.id, {
"ViewChannel": true
})
setTimeout(() => {
ch.permissionOverwrites.create(interaction.user.id, {
"ViewChannel": true
}).then((channel) => {

}, (err) => {
console.log(err);
});
}, 1000);

let btnrow = new ActionRowBuilder<ButtonBuilder>().addComponents([
new ButtonBuilder()
Expand All @@ -92,8 +116,8 @@ export async function handleInteraction(interaction: Interaction, db: DB) {
]);
ch.send({
content: `${interaction.member} || <@&757969277063266407>`,
embeds: [ embed ],
components: [ btnrow ],
embeds: [embed],
components: [btnrow],
});
interaction.reply({
content: `> Successfully created your ticket here: ${ch}`,
Expand All @@ -105,7 +129,7 @@ export async function handleInteraction(interaction: Interaction, db: DB) {
if (!interaction.isChatInputCommand()) return

/*if (interaction.commandName === 'setup') {
const channel = interaction.guild?.channels.cache.get("788844358592888863") as TextChannel
const channel = interaction.guild?.channels.cache.get("757992735171936347") as TextChannel
const embed = new EmbedBuilder()
.setTitle("Voicelocker")
Expand Down Expand Up @@ -134,7 +158,7 @@ export async function handleInteraction(interaction: Interaction, db: DB) {
interaction.reply("message sent!")
// code
let ticketChannel = interaction.guild!.channels.cache.get("1081337337704886392") as TextChannel;
let ticketChannel = interaction.guild!.channels.cache.get("757992735171936347") as TextChannel;
if (!ticketChannel) return;
let embed = new EmbedBuilder()
Expand All @@ -150,8 +174,8 @@ export async function handleInteraction(interaction: Interaction, db: DB) {
]);
await ticketChannel.send({
embeds: [ embed ],
components: [ btnrow ],
embeds: [embed],
components: [btnrow],
});
interaction.reply({
Expand All @@ -165,7 +189,7 @@ export async function handleInteraction(interaction: Interaction, db: DB) {

const row_username = new ActionRowBuilder<UserSelectMenuBuilder>().addComponents(verify_modal)

await interaction.reply({ content: 'Which user do you want to verify?', components: [ row_username ], ephemeral: true })
await interaction.reply({ content: 'Which user do you want to verify?', components: [row_username], ephemeral: true })
}

if (interaction.commandName == "daily") {
Expand Down

0 comments on commit 2113518

Please sign in to comment.