Skip to content

Commit

Permalink
feat: ticket closing & transcripts
Browse files Browse the repository at this point in the history
  • Loading branch information
flowergardn committed Nov 1, 2023
1 parent 7fe7df8 commit 40a5d14
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"@prisma/client": "^5.5.2",
"@t3-oss/env-core": "^0.6.1",
"@types/node": "^18.6.1",
"axios": "^1.6.0",
"dayjs": "^1.11.10",
"discord.js": "^14.2.0",
"discordx": "^11.1.9",
"dotenv": "^16.0.1",
Expand Down
106 changes: 106 additions & 0 deletions src/commands/close.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {
ActionRowBuilder,
ApplicationCommandOptionType,
ButtonBuilder,
ButtonStyle,
CommandInteraction,
EmbedBuilder,
GuildTextBasedChannel,
userMention
} from 'discord.js';
import { Discord, Slash, SlashOption } from 'discordx';
import { client, prisma } from '..';
import Colors from '../constants/Colors';
import dayjs from 'dayjs';
import axios from 'axios';
import { env } from '../env/server';

@Discord()
class Close {
async createTranscript(channel: GuildTextBasedChannel) {
let msgs = await channel.messages.fetch();
msgs = msgs.reverse();

let formatted = msgs.map((msg) => {
const time = dayjs().format('MM/DD/YYYY HH:mm:ss');
return `[${time}] ` + msg.author.username + ': ' + msg.cleanContent;
});

const hasteResponse: {
data: {
key: string;
};
} = await axios.post(`https://paste.tarna.dev/documents`, formatted.join('\n'));

return `https://paste.tarna.dev/${hasteResponse.data.key}`;
}

@Slash({ description: 'Close a ticket' })
async close(
@SlashOption({
description: 'Reason for closing this ticket',
name: 'reason',
required: false,
type: ApplicationCommandOptionType.String
})
reason: string,
interaction: CommandInteraction
) {
interaction.deferReply();
const ticket = await prisma.tickets.findUnique({
where: {
id: interaction.channelId
}
});

if (ticket == null) {
interaction.reply({
content: 'You can only run this command inside of ticket',
ephemeral: true
});
return;
}

const creator = await client.users.fetch(ticket.createdBy);
const closedEmbed = new EmbedBuilder()
.setColor(Colors.purple)
.setTitle('Ticket closed')
.setFields({
name: 'Reason',
value: reason ?? 'No reason specified'
});

const transcript = await this.createTranscript(interaction.channel);
const button = new ButtonBuilder()
.setStyle(ButtonStyle.Link)
.setURL(transcript)
.setLabel('View Transcript');
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(button);

try {
await creator.send({
embeds: [closedEmbed],
components: [row]
});
} catch (err) {
console.log(`Failed to send ticket log to ${creator.id}.`);
}

const logChannel = (await interaction.guild.channels.fetch(
env.LOGS_CHANNEL
)) as GuildTextBasedChannel;
closedEmbed.setDescription(`Closed by ${userMention(interaction.user.id)}`).setFooter({
text: `Ticket ID: ${interaction.channel.id}`
});
await logChannel.send({
embeds: [closedEmbed],
components: [row]
});

interaction.editReply({
content: 'Closed ticket!'
});

interaction.channel.delete('Ticket closed');
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const client = new Client({
});

client.on('ready', async () => {
// await client.clearApplicationCommands();
// await client.initApplicationCommands();
await client.clearApplicationCommands();
await client.initApplicationCommands();

console.log('> Bot online, logged in as: ' + client.user!!.tag);
});
Expand Down
62 changes: 62 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ arg@^4.1.0:
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.0.tgz#f1e5292f26b2fd5c2e66876adc5b06cdbd7d2102"
integrity sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
Expand Down Expand Up @@ -286,6 +300,13 @@ color-name@~1.1.4:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
Expand All @@ -300,6 +321,16 @@ cross-spawn@^7.0.0:
shebang-command "^2.0.0"
which "^2.0.1"

dayjs@^1.11.10:
version "1.11.10"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.10.tgz#68acea85317a6e164457d6d6947564029a6a16a0"
integrity sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ==

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
Expand Down Expand Up @@ -374,6 +405,11 @@ file-type@^18.3.0:
strtok3 "^7.0.0"
token-types "^5.0.1"

follow-redirects@^1.15.0:
version "1.15.3"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a"
integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==

foreground-child@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/foreground-child/-/foreground-child-3.1.1.tgz#1d173e776d75d2772fed08efe4a0de1ea1b12d0d"
Expand All @@ -382,6 +418,15 @@ foreground-child@^3.1.0:
cross-spawn "^7.0.0"
signal-exit "^4.0.1"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

glob@^10.0.0:
version "10.2.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-10.2.6.tgz#1e27edbb3bbac055cb97113e27a066c100a4e5e1"
Expand Down Expand Up @@ -442,6 +487,18 @@ make-error@^1.1.1:
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

mime-db@1.52.0:
version "1.52.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-types@^2.1.12:
version "2.1.35"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
dependencies:
mime-db "1.52.0"

minimatch@^9.0.1:
version "9.0.1"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.1.tgz#8a555f541cf976c622daf078bb28f29fb927c253"
Expand Down Expand Up @@ -491,6 +548,11 @@ prisma@^4.10.1:
dependencies:
"@prisma/engines" "4.14.1"

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

readable-stream@^3.6.0:
version "3.6.2"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.2.tgz#56a9b36ea965c00c5a93ef31eb111a0f11056967"
Expand Down

0 comments on commit 40a5d14

Please sign in to comment.