Skip to content

Commit

Permalink
feat: tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
paring-chan committed Oct 17, 2021
1 parent 68e026e commit b8e0e10
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test.pikokr.dev {
reverse_proxy 127.0.0.1:3000
}
2 changes: 1 addition & 1 deletion src/modules/builtin/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Dev extends BuiltInModule {
ephemeral: true,
})
const data = await this.cts.registry.reloadAll()
await this.cts.registry.syncCommands()
// await this.cts.registry.syncCommands()
await i.editReply({
content: '```\n' + data.map((x) => (x.success ? `✅ ${x.path}` : `❌ ${x.path}\n${x.error}`)).join('\n') + '```',
})
Expand Down
116 changes: 116 additions & 0 deletions src/modules/ticket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { listener, Module } from '@pikokr/command.ts'
import { Channel, Message, TextChannel } from 'discord.js'
import { getTicketArchiveChannel, getTicketChannel, getTicketDeleteChannel } from '../utils/channels'

class Ticket extends Module {
@listener('messageCreate')
async text(msg: Message) {
if (msg.channel.type !== 'GUILD_TEXT') return
if (msg.author.bot || msg.author.system) return
const category = await getTicketChannel()

if (msg.channel.parentId !== category.id) return

const id = msg.channel.name.split('-').pop()

if (!id) return

const tc = await category.guild.members.fetch({user: id, cache: true})

if (!tc) return

if (!msg.content && !msg.attachments.size) return

try {
await tc.user.send({
content: msg.content ? msg.author.tag + ' - ' + msg.content : null,
files: msg.attachments.map(x=>x),
})
} catch {
await msg.react('❌')
return
}

await msg.react('✅')
}

@listener('messageCreate')
async dm(msg: Message) {
if (msg.author.bot || msg.author.system) return
if (msg.channel.type !== 'DM') return
const category = await getTicketChannel()

let tc: TextChannel = category.children.find(x=>x.name.split('-').pop()===msg.author.id && x.type === 'GUILD_TEXT') as TextChannel

if (!tc) {
const archive = await getTicketArchiveChannel()
const c = archive.children.find(x=>x.name.split('-').pop()===msg.author.id && x.type === 'GUILD_TEXT') as TextChannel

if (c) {
await c.setParent(category)
tc = c
}
}

if (!tc) {
const id = msg.author.id

const channelName = `${msg.author.tag.slice(0, 100-id.length-1)}-${msg.author.id}`

tc = await category.guild.channels.create(channelName,
{
type: 'GUILD_TEXT',
reason: 'create ticket',
parent: category,
topic: `${msg.author}님의 티켓`
})

await tc.send(`${msg.author.tag}님의 티켓입니다.`)
}

let webhook = (await tc.fetchWebhooks()).first()

if (!webhook) {
webhook = await tc.createWebhook('Ticket', {
reason: 'Create ticket webhook'
})
}

await webhook.send({
username: msg.author.username,
avatarURL: msg.author.displayAvatarURL({size: 4096,dynamic: true, format: 'png'}),
content: msg.content || null,
files: msg.attachments.map(x=>x),
})
}

@listener('channelUpdate')
async channelUpdated(oldChannel: Channel, newChannel: Channel) {
if (oldChannel.type !== 'GUILD_TEXT' || newChannel.type !== 'GUILD_TEXT') return
const oldC = oldChannel as TextChannel
const newC = newChannel as TextChannel

const ticketId = ((await getTicketChannel()).id)
const archiveId = (await getTicketArchiveChannel()).id
const deleteId = (await getTicketDeleteChannel()).id

const ids = [ticketId, archiveId, deleteId]
if (!ids.includes(oldC.parentId!)) return
if (!ids.includes(oldC.parentId!)) return

if (oldC.parentId === ticketId && newC.parentId === archiveId) {
await newC.send('티켓이 아카이브 처리되었습니다.')
return
} else if (oldC.parentId === archiveId && newC.parentId === ticketId) {
await newC.send('티켓이 아카이브 해제되었습니다.')
return
} else if (newC.parentId === deleteId) {
await newC.delete()
return
}
}
}

export function install() {
return new Ticket()
}
9 changes: 9 additions & 0 deletions src/utils/channels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getMainGuild } from './guild'
import { config } from '../config'
import { CategoryChannel } from 'discord.js'

export const getTicketChannel = async () => (await getMainGuild()!.channels.fetch(config.ticketCategoryID, {cache: true})) as CategoryChannel

export const getTicketArchiveChannel = async () => (await getMainGuild()!.channels.fetch(config.ticketArchiveCategoryID, {cache: true})) as CategoryChannel

export const getTicketDeleteChannel = async () => (await getMainGuild()!.channels.fetch(config.ticketDeleteCategoryID, {cache: true})) as CategoryChannel

0 comments on commit b8e0e10

Please sign in to comment.