132 lines
4.1 KiB
TypeScript
132 lines
4.1 KiB
TypeScript
import "reflect-metadata"
|
|
import { Client, Events, GatewayIntentBits, Guild, GuildBasedChannel, Role } from "discord.js"
|
|
import { config } from "./config"
|
|
import { DataSource } from "typeorm"
|
|
import { DBServer } from "./utilties/storage/entities/DBServer"
|
|
import path from "path"
|
|
import { DBChannel } from "./utilties/storage/entities/DBChannel"
|
|
import { DBRole } from "./utilties/storage/entities/DBRole"
|
|
import { insertGuild } from "./utilties/discord/guilds"
|
|
import { insertChannel } from "./utilties/discord/channels"
|
|
import { insertRole } from "./utilties/discord/roles"
|
|
import { setupRoleCapture } from "./utilties/events/roles"
|
|
import { DBUser } from "./utilties/storage/entities/DBUser"
|
|
|
|
console.log(__dirname + path.sep + "utilities" + path.sep + "storage" + path.sep + "entities" + path.sep + "*.ts")
|
|
|
|
export const dataSource = new DataSource({
|
|
type: "sqlite",
|
|
database: "breadbot.db",
|
|
entities: [DBServer, DBChannel, DBRole, DBUser],
|
|
synchronize: true,
|
|
logging: true
|
|
})
|
|
|
|
export const client : Client = new Client({
|
|
intents: [
|
|
GatewayIntentBits.Guilds,
|
|
GatewayIntentBits.GuildMessages,
|
|
GatewayIntentBits.GuildMembers,
|
|
GatewayIntentBits.MessageContent,
|
|
GatewayIntentBits.GuildVoiceStates
|
|
]
|
|
})
|
|
|
|
client.once(Events.ClientReady, async () => {
|
|
await dataSource.initialize()
|
|
|
|
const serverRepo = dataSource.getRepository(DBServer)
|
|
const channelRepo = dataSource.getRepository(DBChannel)
|
|
const roleRepo = dataSource.getRepository(DBRole)
|
|
|
|
client.guilds.cache.forEach(async (guild: Guild) => {
|
|
const server: DBServer | null = await insertGuild(serverRepo, guild)
|
|
|
|
if (server != null) {
|
|
guild.channels.cache.forEach(async (channel: GuildBasedChannel) => {
|
|
await insertChannel(channelRepo, channel)
|
|
})
|
|
|
|
guild.roles.cache.forEach(async (role: Role) => {
|
|
await insertRole(roleRepo, role)
|
|
})
|
|
}
|
|
})
|
|
|
|
setupRoleCapture(client, serverRepo, roleRepo)
|
|
|
|
console.log("Breadbot is Ready")
|
|
})
|
|
|
|
client.login(config.DISCORD_TOKEN)
|
|
|
|
/*
|
|
export let db: SQLCommon
|
|
|
|
if (config.DB_MODE == "sqlite") {
|
|
db = new utilities.sqlite.SqliteDB("breadbot_test.db")
|
|
|
|
db.run("PRAGMA foreign_keys = ON")
|
|
|
|
utilities.tables.makeTables(db)
|
|
|
|
//TODO I really don't want this to be here.
|
|
utilities.events.messages.setupMessageCapture(client, db)
|
|
utilities.events.roles.setupRoleCapture(client, db)
|
|
}
|
|
|
|
client.once(Events.ClientReady, () => {
|
|
// TODO Winston should handle this
|
|
console.log("Breadbot is ready")
|
|
|
|
client.guilds.cache.forEach(async (guild: Guild) => {
|
|
await utilities.commands.deployCommands(guild.id)
|
|
|
|
// TODO handle failures?
|
|
await utilities.guilds.insertGuild(db, guild)
|
|
|
|
guild.channels.cache.forEach(async (channel: GuildBasedChannel) => {
|
|
await utilities.channels.insertChannel(db, channel)
|
|
})
|
|
|
|
guild.roles.cache.forEach(async (role: Role) => {
|
|
await utilities.roles.insertRole(db, role)
|
|
})
|
|
})
|
|
})
|
|
|
|
client.on(Events.GuildCreate, async (guild : Guild) => {
|
|
await utilities.commands.deployCommands(guild.id)
|
|
await utilities.guilds.insertGuild(db, guild)
|
|
|
|
guild.channels.cache.forEach(async (channel: GuildBasedChannel) => {
|
|
await utilities.channels.insertChannel(db, channel)
|
|
})
|
|
})
|
|
|
|
client.on(Events.ChannelCreate, async (channel) => {
|
|
console.log("CHANNEL CREATE CALLED")
|
|
await utilities.channels.insertChannel(db, channel)
|
|
})
|
|
|
|
client.on(Events.ThreadCreate, async (channel) => {
|
|
console.log("THREAD CREATE CALLED")
|
|
console.log(channel.toString())
|
|
await utilities.channels.insertChannel(db, channel)
|
|
})
|
|
|
|
client.on(Events.InteractionCreate, async (interaction: Interaction) => {
|
|
if (!interaction.isCommand()) {
|
|
return
|
|
}
|
|
|
|
if (commands[interaction.commandName as keyof typeof commands]) {
|
|
commands[interaction.commandName as keyof typeof commands].execute(interaction as ChatInputCommandInteraction)
|
|
}
|
|
})
|
|
|
|
setInterval(async () => {
|
|
await utilities.breadthread.breadthreadProcessLocks(db, client)
|
|
}, 5000)
|
|
|
|
client.login(config.DISCORD_TOKEN)*/ |