Skip to content

Commit

Permalink
fixed deployment & updated bot.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
GhomKrosmonaute committed Jun 26, 2024
1 parent 1ea9f0e commit 7084c2f
Show file tree
Hide file tree
Showing 33 changed files with 139 additions and 129 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
"error",
{
"ignore": [
"^#app$"
"^#app$",
"^#config$",
"^#env$",
"^#client$",
"^#logger$"
]
}
],
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@
},
"imports": {
"#app": "./dist/app.js",
"#config": "./dist/config.js",
"#env": "./dist/app/env.js",
"#app/*": "./dist/*",
"#tables/*": "./dist/tables/*"
"#tables/*": "./dist/tables/*",
"#client": "./dist/app/client.js",
"#logger": "./dist/app/logger.js"
}
}
4 changes: 2 additions & 2 deletions src/app.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export * from "./app/database.ts"
export * from "./app/listener.ts"
export * from "./app/command.ts"
export * from "./app/config.ts"
export * from "./app/client.ts"
export * from "./app/logger.ts"
export * from "./app/slash.ts"
export * from "./app/util.ts"
Expand All @@ -15,8 +14,9 @@ export * as argument from "./app/argument.ts"
export * as database from "./app/database.ts"
export * as listener from "./app/listener.ts"
export * as command from "./app/command.ts"
export * as logger from "./app/logger.ts"
export * as slash from "./app/slash.ts"
export * as util from "./app/util.ts"

export { default as env } from "./app/env.ts"
export { default as client } from "./app/client.ts"
export { default as logger } from "./app/logger.ts"
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from "./namespaces/fetcher.ts"
export * from "./namespaces/backup.ts"
export * from "./namespaces/automod.ts"
export * from "./namespaces/openai.ts"
export * from "./namespaces/emotes.ts"

export * as Discord from "discord.js"
export * as ladder from "./namespaces/ladder.ts"
Expand All @@ -29,3 +30,4 @@ export * as fetcher from "./namespaces/fetcher.ts"
export * as backup from "./namespaces/backup.ts"
export * as automod from "./namespaces/automod.ts"
export * as openai from "./namespaces/openai.ts"
export * as emotes from "./namespaces/emotes.ts"
15 changes: 3 additions & 12 deletions src/app/client.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
// system file, please don't modify it

import discord from "discord.js"
import * as config from "./config.ts"
import config from "#config"

export class ClientSingleton {
private static instance: discord.Client
const client = new discord.Client(config.client)

private constructor() {}

static get(): discord.Client {
if (!ClientSingleton.instance) {
ClientSingleton.instance = new discord.Client(config.getConfig().client)
}
return ClientSingleton.instance
}
}
export default client
8 changes: 4 additions & 4 deletions src/app/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import yargsParser from "yargs-parser"

import * as handler from "@ghom/handler"

import env from "./env.ts"

import * as util from "./util.ts"
import * as logger from "./logger.ts"
import * as argument from "./argument.ts"
import * as config from "./config.ts"

import env from "#env"
import config from "#config"

import { filename } from "dirname-filename-esm"
const __filename = filename(import.meta)
Expand Down Expand Up @@ -841,7 +841,7 @@ export async function sendCommandDetails(
message: IMessage,
cmd: ICommand,
): Promise<void> {
const { detailCommand, openSource } = config.getConfig()
const { detailCommand, openSource } = config

if (detailCommand) {
const options = await detailCommand(message, cmd)
Expand Down
6 changes: 0 additions & 6 deletions src/app/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import type * as logger from "./logger.ts"
import type * as slash from "./slash.ts"
import type * as util from "./util.ts"

import { config } from "../config.ts"

export interface Config {
/**
* Ignore bots messages for textual commands if enabled
Expand Down Expand Up @@ -73,7 +71,3 @@ export interface Config {
*/
logger?: logger.LoggerOptions
}

export function getConfig(): Config {
return config
}
2 changes: 1 addition & 1 deletion src/app/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export const orm = new ORM({
logger,
})

export * from "@ghom/orm"
export * from "@ghom/orm"
31 changes: 13 additions & 18 deletions src/app/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import apiTypes from "discord-api-types/v8"

import * as handler from "@ghom/handler"

import * as logger from "./logger.ts"
import * as client from "./client.ts"
import logger from "#logger"
import client from "#client"

const readyListeners = new discord.Collection<Listener<"ready">, boolean>()

Expand All @@ -22,28 +22,23 @@ export const listenerHandler = new handler.Handler(
return file.default as Listener<any>
},
onLoad: async (filepath, listener) => {
const clientInstance = client.ClientSingleton.get()

if (listener.event === "ready") readyListeners.set(listener, false)

clientInstance[listener.once ? "once" : "on"](
listener.event,
async (...args) => {
try {
await listener.run(...args)
client[listener.once ? "once" : "on"](listener.event, async (...args) => {
try {
await listener.run(...args)

if (listener.event === "ready") {
readyListeners.set(listener, true)
if (listener.event === "ready") {
readyListeners.set(listener, true)

if (readyListeners.every((launched) => launched)) {
clientInstance.emit("afterReady", ...args)
}
if (readyListeners.every((launched) => launched)) {
client.emit("afterReady", ...args)
}
} catch (error: any) {
logger.error(error, filepath, true)
}
},
)
} catch (error: any) {
logger.error(error, filepath, true)
}
})

const isNative = filepath.includes(".native.")

Expand Down
7 changes: 4 additions & 3 deletions src/app/logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { logger, Logger } from "@ghom/logger"
import { getConfig } from "./config.ts"

const { logger: loggerOptions } = getConfig()
import config from "#config"

export const systemLogger = loggerOptions ? new Logger(loggerOptions) : logger
export const systemLogger = config.logger ? new Logger(config.logger) : logger

const log = systemLogger.log.bind(systemLogger)
const warn = systemLogger.warn.bind(systemLogger)
Expand All @@ -22,3 +21,5 @@ export {
} from "@ghom/logger"

export type * from "@ghom/logger"

export default systemLogger
5 changes: 3 additions & 2 deletions src/app/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import discord from "discord.js"

import config from "#config"

import * as logger from "./logger.ts"
import * as config from "./config.ts"
import * as util from "./util.ts"

import { filename } from "dirname-filename-esm"
Expand Down Expand Up @@ -315,6 +316,6 @@ export class StaticPaginator extends Paginator {
}

export async function initPagination() {
const { paginatorEmojis } = config.getConfig()
const { paginatorEmojis } = config
if (paginatorEmojis) Paginator.defaultEmojis = paginatorEmojis
}
8 changes: 4 additions & 4 deletions src/app/slash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import chalk from "chalk"

import * as handler from "@ghom/handler"

import env from "./env.ts"
import env from "#env"
import logger from "#logger"
import config from "#config"

import * as logger from "./logger.ts"
import * as config from "./config.ts"
import * as util from "./util.ts"

import { filename } from "dirname-filename-esm"
Expand Down Expand Up @@ -389,7 +389,7 @@ export async function sendSlashCommandDetails(
interaction: ISlashCommandInteraction,
computed: discord.ApplicationCommand,
) {
const { detailSlashCommand } = config.getConfig()
const { detailSlashCommand } = config

interaction.base.reply(
detailSlashCommand
Expand Down
23 changes: 9 additions & 14 deletions src/app/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import relative from "dayjs/plugin/relativeTime.js"
import timezone from "dayjs/plugin/timezone.js"
import toObject from "dayjs/plugin/toObject.js"

import env from "./env.ts"

import * as logger from "./logger.ts"
import * as config from "./config.ts"
import * as client from "./client.ts"
import env from "#env"
import client from "#client"
import config from "#config"
import logger from "#logger"

export type PermissionsNames = keyof typeof v10.PermissionFlagsBits

Expand Down Expand Up @@ -462,13 +461,9 @@ const defaultSystemEmojis: SystemEmojis = {
}

export function getSystemEmoji(name: keyof SystemEmojis): string {
const rawEmoji =
config.getConfig().systemEmojis?.[name] ?? defaultSystemEmojis[name]
const rawEmoji = config.systemEmojis?.[name] ?? defaultSystemEmojis[name]

return (
client.ClientSingleton.get().emojis.resolve(rawEmoji)?.toString() ??
rawEmoji
)
return client.emojis.resolve(rawEmoji)?.toString() ?? rawEmoji
}

export interface SystemMessageOptions {
Expand Down Expand Up @@ -626,7 +621,7 @@ export function getSystemMessage<Key extends keyof SystemMessages>(
? Options
: never,
): Promise<SystemMessage> {
return (
config.getConfig().systemMessages?.[name] ?? defaultSystemMessages[name]
)(options as any)
return (config.systemMessages?.[name] ?? defaultSystemMessages[name])(
options as any,
)
}
15 changes: 9 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as app from "#app"
import type { Config } from "#app/app/config.ts"
import { Emotes } from "./namespaces/emotes.ts"

import { Emotes } from "./namespaces/tools.ts"

export const config: app.Config = {
const config: Config = {
ignoreBots: true,
openSource: true,
getPrefix: (message) => {
return app.prefix(message.guild)
async getPrefix(message) {
return import("./namespaces/tools.ts").then((tools) =>
tools.prefix(message.guild),
)
},
client: {
intents: [
Expand Down Expand Up @@ -40,3 +41,5 @@ export const config: app.Config = {
loading: Emotes.Loading,
},
}

export default config
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ try {
await app.listenerHandler.init()
await app.initPagination()
await app.checkUpdates()
await app.ClientSingleton.get().login(env.BOT_TOKEN)
await app.client.login(env.BOT_TOKEN)
} catch (error: any) {
app.error(error, __filename, true)
process.exit(1)
Expand Down
3 changes: 1 addition & 2 deletions src/listeners/command.messageCreate.native.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// system file, please don't modify it

import * as app from "#app"
import config from "#config"
import yargsParser from "yargs-parser"

const listener: app.Listener<"messageCreate"> = {
event: "messageCreate",
description: "Handle messages for commands",
async run(message) {
const config = app.getConfig()

if (config.ignoreBots && message.author.bot) return

if (!app.isNormalMessage(message)) return
Expand Down
3 changes: 2 additions & 1 deletion src/namespaces/active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as app from "#app"
import { Guild } from "#tables/guild.ts"
import active from "#tables/active.ts"
import message from "#tables/message.ts"
import env from "#env"

/**
* @param guild_id internal guild id
Expand Down Expand Up @@ -43,7 +44,7 @@ export async function updateActive(
guildConfig: Guild
},
): Promise<number> {
if (process.env.BOT_MODE === "dev") return 0
if (env.BOT_MODE === "development") return 0

guild.members.cache.clear()

Expand Down
18 changes: 18 additions & 0 deletions src/namespaces/emotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Client } from "discord.js"

export enum Emotes {
Loading = "865282736041361468",
CheckMark = "865281743333228604",
Cross = "865281743560638464",
Minus = "865281743422226443",
Plus = "865281743648194610",
Left = "865281743371894794",
Right = "865281743510044723",
}

export function emote(
{ client }: { client: Client },
name: keyof typeof Emotes,
) {
return client.emojis.resolve(Emotes[name])
}
6 changes: 3 additions & 3 deletions src/namespaces/ladder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as command from "../app/command.ts"
import * as argument from "../app/argument.ts"
import * as pagination from "../app/pagination.ts"

import * as tools from "./tools.ts"
import * as emotes from "./emotes.ts"

export function formatRank(rank: number) {
return `\`[ ${util.forceTextSize(rank, 3, true)} ]\``
Expand Down Expand Up @@ -43,7 +43,7 @@ export class Ladder<Line extends LadderLine> {
.setTitle(`${this.options.title} leaderboard`)
.setDescription(
(await this.fetchPage(options)) ||
`${tools.emote(ctx, "Cross")} No ladder available`,
`${emotes.emote(ctx, "Cross")} No ladder available`,
)
.setFooter({
text: `Page: ${options.pageIndex + 1} / ${await this.fetchPageCount(
Expand Down Expand Up @@ -77,7 +77,7 @@ export class Ladder<Line extends LadderLine> {

if (page.length === 0)
return {
content: `${tools.emote(channel, "Cross")} No ladder available.`,
content: `${emotes.emote(channel, "Cross")} No ladder available.`,
}

return {
Expand Down
Loading

0 comments on commit 7084c2f

Please sign in to comment.