You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.7 KiB
TypeScript

import dotenv from 'dotenv'
import { SlashCreator, GatewayServer, Member } from 'slash-create'
import path from 'path'
import CatLoggr from 'cat-loggr/ts'
import { Client, GatewayDispatchEvents, Events, Guild, Message, GuildMember, TextChannel } from 'discord.js'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
const client = new Client({ intents: [131071] })
let dotenvPath = path.join(process.cwd(), '.env')
if (path.parse(process.cwd()).name === 'dist') dotenvPath = path.join(process.cwd(), '..', '.env')
dotenv.config({ path: dotenvPath })
const logger = new CatLoggr().setLevel(process.env.COMMANDS_DEBUG === 'true' ? 'debug' : 'info')
const creator = new SlashCreator({
client,
applicationID: process.env.APPLICATION_ID ?? 'AASDF',
publicKey: process.env.PUBLIC_KEY,
token: process.env.TOKEN
})
creator.on('debug', (message) => logger.log(message))
creator.on('warn', (message) => logger.warn(message))
creator.on('error', (error) => logger.error(error))
creator.on('synced', () => logger.info('Commands synced!'))
creator.on('commandRun', (command, _, ctx) =>
logger.info(`${ctx.user.username}#${ctx.user.discriminator} (${ctx.user.id}) ran command ${command.commandName}`)
)
creator.on('commandRegister', (command) => logger.info(`Registered command ${command.commandName}`))
creator.on('commandError', (command, error) => logger.error(`Command ${command.commandName}:`, error))
creator
.withServer(new GatewayServer((handler) => client.ws.on(GatewayDispatchEvents.InteractionCreate, handler)))
.registerCommandsIn(path.join(__dirname, 'commands'))
.syncCommands()
client.once('ready', async () => console.log('ready!'))
client.on(Events.GuildCreate, async (g: Guild) => {
await prisma.guildConfig.create({
data: {
id: g.id
}
})
})
client.on(Events.MessageCreate, async (msg: Message) => {
if (!msg.guild || msg.author == client.user) return
const u = await prisma.user.findFirst({
where: {
guild_id: msg.guild.id,
id: msg.author.id
}
})
if (!u) {
await prisma.user.create({
data: {
guild_id: msg.guild.id,
id: msg.author.id,
messages: 1
}
})
return
}
const newMessagesCount = (u.messages ?? 0) + 1
await prisma.user.update({
where: {
id_guild_id: { id: msg.author.id, guild_id: msg.guild.id }
},
data: {
messages: newMessagesCount
}
})
})
client.on(Events.GuildMemberAdd, async (m: GuildMember) => {
const gc = await prisma.guildConfig.findFirst({
where: {
id: m.guild.id
}
})
if (gc.welcome_on) {
const ch = (await m.guild.channels.fetch(gc.welcome_channel)) as TextChannel
await ch.send(gc.welcome_message.replace('%USER%', `<@${m.id}>`))
}
})
client.login(process.env.TOKEN)