Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #96 from TeamSekai/dev/74-refactoring
Browse files Browse the repository at this point in the history
リファクタリング&canvas使用コマンドをImages Featureに (#74)
  • Loading branch information
ringo360 authored May 2, 2024
2 parents 1c50e66 + b5f1845 commit 4f1e4ec
Show file tree
Hide file tree
Showing 73 changed files with 236 additions and 180 deletions.
1 change: 1 addition & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"features": {
"admin": true,
"cdn": true,
"images": true,
"misc": true,
"player": true,
"templink": true,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export * from './common/CompoundCommand';
export * from './common/Feature';
export * from './common/SimpleCommand';

export { default as Activities } from './internal/activity';
export * from './internal/commands';
export { default as Config } from './internal/config';
export * as Logger from './internal/logger';
export * as Schedules from './internal/schedules';

export * from './util/calendar';
export * from './util/languages';
export { default as Pager } from './util/pager';
export * as Result from './util/result';
export * from './util/strings';
export { default as Timespan } from './util/timespan';
export * from './util/types';
File renamed without changes.
15 changes: 15 additions & 0 deletions internal/commands.ts → core/internal/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from 'fs/promises';
import path from 'path';
import { strFormat, LANG } from '../util/languages';
import { ChatInputCommandInteraction, Client } from 'discord.js';
import { Command } from '../util/types';
Expand Down Expand Up @@ -45,6 +47,19 @@ export class CommandManager {
return this.#commands.size;
}

async loadDirectory(name: string): Promise<void> {
const files = await fs.readdir(name, { withFileTypes: true });
for (const file of files) {
const ext = path.extname(file.name);
if (!file.isFile() || (ext != '.js' && ext != '.ts')) return;
let cmds = await import(path.join(name, file.name));
if ('default' in cmds) {
cmds = cmds.default;
}
this.addCommands(cmds);
}
}

/**
* コマンドの処理を行う。
*/
Expand Down
8 changes: 5 additions & 3 deletions internal/config.ts → core/internal/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import * as fs from 'fs';
import * as path from 'path';
import z from 'zod';
import { setLanguage } from '../util/languages';

Expand Down Expand Up @@ -34,6 +34,7 @@ const configSchema = z
.object({
admin: z.boolean().optional(),
cdn: z.boolean().optional(),
images: z.boolean().optional(),
misc: z.boolean().optional(),
player: z.boolean().optional(),
templink: z.boolean().optional(),
Expand Down Expand Up @@ -66,14 +67,15 @@ function loadJson(path: string) {
}

const config = configSchema.parse(
loadJson(path.join(__dirname, '..', 'config.json')),
loadJson(path.join(__dirname, '..', '..', 'config.json')),
);

setLanguage(
loadJson(
path.join(
__dirname,
'..',
'..',
'language',
(config.language ?? 'default') + '.json',
),
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "core",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "GPL-3.0-only"
}
2 changes: 1 addition & 1 deletion packages/misc/util/calendar.ts → core/util/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { LANG, strFormat } from '../../../util/languages';
import { LANG, strFormat } from 'core';

export const DayOfWeek = Object.freeze({
Sunday: 0,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion util/languages.ts → core/util/languages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as LANG from '../language/default.json';
import * as LANG from '../../language/default.json';

function assignDeep(target: Record<string, unknown>, source: object) {
for (const [key, value] of Object.entries(source)) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 16 additions & 14 deletions discordbot.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { teeWrite } from './internal/logger';
import {
Logger,
Config,
Schedules,
Activities,
CommandManager,
Feature,
LANG,
strFormat,
} from 'core';

//* Discord.js Bot - by ringoXD -
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '1';
require('colors');
import 'colors';
import { Client, GatewayIntentBits, ActivityType } from 'discord.js';
import fs from 'fs/promises';
import path from 'path';
import { token, syslogChannel } from './internal/config';
const { token, syslogChannel } = Config;
process.env['FFMPEG_PATH'] = path.join(__dirname, 'ffmpeg');

//!Load Internal dir code
import { onShutdown } from './internal/schedules';
import activity from './internal/activity';

import { LANG, strFormat } from './util/languages';
import { CommandManager } from './internal/commands';
import assert from 'assert';
import { Feature } from './common/Feature';

const creset = '\x1b[0m';
const cgreen = '\x1b[32m';

//!LOGGER
teeWrite(process.stdout, 'discordbot.log');
teeWrite(process.stderr, 'discordbot.log');
Logger.teeWrite(process.stdout, 'discordbot.log');
Logger.teeWrite(process.stderr, 'discordbot.log');

//!RUN=======================

Expand All @@ -42,7 +44,7 @@ const options = {

const client = new Client(options);
console.log(LANG.discordbot.main.setupActivityCalling);
activity.setupActivity(client);
Activities.setupActivity(client);

const featuresLoadPromise = fs
.readdir(path.join(__dirname, 'packages'))
Expand Down Expand Up @@ -111,7 +113,7 @@ client.on('ready', async (readyClient) => {
SyslogChannel.send(LANG.discordbot.ready.sysLog);
});

onShutdown(async () => {
Schedules.onShutdown(async () => {
const SyslogChannel = client.channels.cache.get(syslogChannel);
assert(SyslogChannel?.isTextBased());
await SyslogChannel.send(LANG.discordbot.shutdown.sysLog);
Expand Down
1 change: 0 additions & 1 deletion internal/webserver.js

This file was deleted.

21 changes: 20 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"dev": "npx tsx watch --ignore accesslog.txt --ignore discordbot.log ."
},
"workspaces": [
"packages/*"
"packages/*",
"core"
]
}
Loading

0 comments on commit 4f1e4ec

Please sign in to comment.