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

Commit

Permalink
models to typescript and translation
Browse files Browse the repository at this point in the history
  • Loading branch information
ggvbo committed Jan 25, 2023
1 parent e6d8e4d commit 41c2c71
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 76 deletions.
11 changes: 11 additions & 0 deletions 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 @@ -7,7 +7,7 @@
"builddata": "node ./scripts/generateBuildData.mjs",
"build": "tsc",
"lint": "eslint . --fix && prettier --write .",
"dev": "ts-node ./src/index.ts --experimental-specifier-resolution=node"
"dev": "node --experimental-specifier-resolution=node --loader ts-node/esm ./src/index.ts"
},
"repository": {
"type": "git",
Expand All @@ -31,6 +31,7 @@
"sequelize": "^6.28.0",
"sequelize-typescript": "^2.1.5",
"umzug": "^3.2.1",
"uninstall": "^0.0.0",
"winston": "^3.8.2"
},
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion resources/lang/en.json5
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
guildcount_one: '{{count}} guild',
guildcount_other: '{{count}} guilds',
version: 'Version',
commit: '(commit {{commit}})'
build: 'Build',
commit: 'Commit `{{commit}}`',
branch: 'Branch `{{branch}}`',
time: 'Built at {{time}}'
}
},

Expand Down
40 changes: 24 additions & 16 deletions src/Pogbot.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Client, Intents, Options, Constants, Collection } from 'discord.js';
import { Logger, format, transports } from 'winston';
import winston from 'winston';

import { readdirSync } from 'fs';

import { PogListener } from './object/PogListener.js';
import { FinalizedCommand } from './object/command';
import { Translation } from './object/Translation.js';
// TODO: Get this working as an directory import
import { DiscordHandler, EventKind } from './object/DiscordHandler';

import { finalize } from './utils';
import { Storage } from './data';
import { AlreadyInitializedError } from './errors.js';
import { DiscordHandler } from './object/DiscordHandler';

export class Pogbot extends Client {

Expand All @@ -21,7 +21,6 @@ export class Pogbot extends Client {
private static _instance: Pogbot;

/**
* Use the {@link translator} getter/setter instead.
* @internal
*/
private _translator: Translation;
Expand Down Expand Up @@ -80,19 +79,20 @@ export class Pogbot extends Client {
// TODO: string
this._logger.info(Translation.of(''))
}).catch((error: Error) => {
// TODO: string
this._logger.error(Translation.of(''))
this._logger.error(Translation.of('error.cannotLogin', {
error: error
}))
})
}

private setupLogger(): Logger {
return new Logger({
format: format.combine(
format.colorize(),
format.splat(),
format.simple()
return new winston.Logger({
format: winston.format.combine(
winston.format.colorize(),
winston.format.splat(),
winston.format.simple()
),
transports: [ new transports.Console ]
transports: [ new winston.transports.Console ]
});
}

Expand All @@ -111,13 +111,18 @@ export class Pogbot extends Client {
import('./handlers/' + file).then(({ default: module }) => {
const { name, kind, execute }: DiscordHandler = module;

if (kind === EventKind.ONCE) {
this.prependOnceListener(name, (data?: unknown) => execute(this, data));
} else {
this.prependListener(name, (data?: unknown) => execute(this, data));
}

this.logger.debug('Registered handler %s', name)
});
}
}

private setupCommands(): void {
this.logger.debug('Registering commands');

const files = readdirSync('./commands').filter((filename) => {
return filename.endsWith('.js');
});
Expand All @@ -129,7 +134,7 @@ export class Pogbot extends Client {
this.commands.set(name, module);
this.application?.commands.create(_json).then((command) => {
module._id = command.id;
this.logger.silly(`Registered command ${name}, version ${command.version}`);
this.logger.debug('Registered command %s %s', name, command.version);
});
});
}
Expand All @@ -147,7 +152,7 @@ export class Pogbot extends Client {
}

static get instance(): Pogbot {
return Pogbot._instance;
return this._instance;
}

static set instance(bot) {
Expand All @@ -168,3 +173,6 @@ export class Pogbot extends Client {
throw new AlreadyInitializedError('Pogbot#storage');
}
}

// winston logger type helper
export type Logger = winston.Logger
37 changes: 25 additions & 12 deletions src/commands/about.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
import { Command } from '../object/command/Command';
import { MessageEmbed, version } from 'discord.js';

import { duration, list } from '../utils';
import * as build from '../../build.json' assert {
type: 'json'
};
import { BUILD_BRANCH, BUILD_COMMIT, BUILD_TIME, BUILD_VERSION, duration, list } from '../utils';
import { Translation } from '../object/Translation';

// TODO: Strings

export default <Command> {
name: 'about',
description: 'See some stats for the bot.',
restrictions: {
enabled: true,
isGuildOnly: false,
isAdminOnly: false,
},
run: async ({ client, interaction }) => {
const embed = new MessageEmbed()
.setTitle(`About ${client.user?.username}`)
.setDescription('Pogbot is an open-sourced bot meant for counting user reactions.')
.setTitle(Translation.of('reply.about.title'))
.setDescription(Translation.of('reply.about.body'))
.setColor('BLURPLE')
.addFields([
{
name: 'Client Uptime',
name: Translation.of('reply.about.uptime'),
value: duration(Date.now() - (client.uptime as number))
},
{
name: 'Guilds',
value: `${client.guilds.cache.size} guilds`
name: Translation.of('reply.about.guilds'),
value: Translation.of('reply.about.guildcount', {
count: client.guilds.cache.size
})
},
{
name: 'Version',
name: Translation.of('reply.about.version'),
value: list([
`Pogbot ${build.version} (commit ${build.vcs.commit})`,
`Pogbot ${BUILD_VERSION}`,
`Node.js ${process.version}`,
`Discord.js ${version}`
], false)
},
{
name: Translation.of('reply.about.build'),
value: list([
Translation.of('reply.about.commit', {
commit: BUILD_COMMIT
}),
Translation.of('reply.about.branch', {
branch: BUILD_BRANCH
}),
Translation.of('reply.about.time', {
time: BUILD_TIME
}),
])
}
])

Expand Down
5 changes: 4 additions & 1 deletion src/data/database.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Sequelize } from 'sequelize';

import Guild from './models/guild.js';
import Guild from './models/Guild.ts';
import Member from './models/member.js';

/**
* @deprecated
*/
export default class Database {
/**
* Please use the methods in the Database class or individual models instead.
Expand Down
50 changes: 45 additions & 5 deletions src/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { Sequelize } from 'sequelize';
import { Sequelize } from 'sequelize-typescript';
import { Umzug } from 'umzug';
import { Context } from 'vm';

import { Pogbot } from '../Pogbot.js';
import { AlreadyInitializedError } from '../errors';
import { Member } from './models/Member.js';
import { Guild } from './models/Guild.js';

export class Storage {

/**
* Use the {@link instance} getter/setter instead.
* @internal
*/
private static _instance: Storage;

/**
* @internal
*/
Expand All @@ -14,8 +26,10 @@ export class Storage {
private _umzug: Umzug;

constructor() {
this.setupSequelize();
this.setupUmzug();
Storage.instance = this;

this._sequelize = this.setupSequelize();
this._umzug = this.setupUmzug();
}

private setupSequelize(): Sequelize {
Expand All @@ -27,13 +41,39 @@ export class Storage {
require: true,
rejectUnauthorized: false
}
}
},
models: [ Guild, Member ]
})
}

private setupUmzug(): Umzug {
return new Umzug({
migrations
migrations: {
glob: './migrations/*.js'
},
context: this._sequelize.getQueryInterface() as Context,
logger: Pogbot.instance.logger
});
}

/**
* Umzug type helper
* @internal
*/
get migrationType() {
return this._umzug._types.migration;
}

static get instance(): Storage {
return this._instance;
}

static set instance(db) {
if (this._instance === undefined)
this._instance = db;
else
throw new AlreadyInitializedError('Pogbot#instance');
}
}

export type Migration = typeof Storage.instance.migrationType;
24 changes: 24 additions & 0 deletions src/data/models/Guild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DataTypes } from 'sequelize';
import { Column, HasOne, Model, PrimaryKey, Table } from 'sequelize-typescript';
import { Member } from './Member';

@Table
export class Guild extends Model {

@PrimaryKey
@Column
guildId?: string;

@Column
triggers?: string;

@Column
channels?: string;

@Column
@HasOne(() => Member)
master?: string;

@Column
reaction?: string;
}
16 changes: 16 additions & 0 deletions src/data/models/Member.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BelongsTo, Column, Model, PrimaryKey, Table } from 'sequelize-typescript';
import { Guild } from './Guild';

@Table
export class Member extends Model {

@Column
public memberId?: string;

@BelongsTo(() => Guild)
@Column
public guildId?: string;

@Column
public score?: number;
}
17 changes: 0 additions & 17 deletions src/data/models/guild.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/data/models/member.js

This file was deleted.

Loading

0 comments on commit 41c2c71

Please sign in to comment.