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

Commit

Permalink
stop using classes for storing channels
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasptsch committed Oct 17, 2022
1 parent 16eb053 commit 4a9ee97
Show file tree
Hide file tree
Showing 49 changed files with 825 additions and 948 deletions.
10 changes: 9 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "node"
},

{
"name": "Launch via NPM",
"request": "launch",
"runtimeArgs": ["watch"],
"runtimeArgs": ["dev"],
"runtimeExecutable": "yarn",
"skipFiles": ["<node_internals>/**"],
"type": "node"
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"scripts": {
"start": "node --enable-source-maps .",
"watch": "tsup src/index.ts --watch --onSuccess \"node --enable-source-maps dist/index.js --inspect\"",
"dev": "tsup src/index.ts --watch",
"prettier": "prettier --write ./src",
"lint": "eslint --ext .ts",
"generate": "prisma generate",
Expand All @@ -68,7 +68,6 @@
"eslint-plugin-prettier": "^4.0.0",
"prettier": "^2.5.1",
"prisma": "^4.4.0",
"tsc-watch": "^5.0.0",
"typescript": "^4.5.4"
},
"engines": {
Expand Down
6 changes: 4 additions & 2 deletions src/autocompletes/AliasAutocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Aliases from '@/classes/Aliases';
import channelActivities from '@/utils/activity';
import Autocomplete, { AutocompleteToken } from '@classes/Autocomplete';
import Autocomplete, { AutocompleteToken } from '@/classes/Autocomplete';
import { AutocompleteInteraction, CacheType, GuildMember } from 'discord.js';
import Fuse from 'fuse.js';
import { Service } from 'typedi';
Expand All @@ -25,7 +25,9 @@ export default class AliasAutocomplete implements Autocomplete {
let options: { name: string; value: string }[] = [];

if (subcommand === 'update' || subcommand === 'remove') {
const existingAliases = this.aliases.getByGuildId(interaction.guildId);
const existingAliases = await this.aliases.getByGuild(
interaction.guildId
);
options = existingAliases.map(({ activity }) => ({
name: activity,
value: activity,
Expand Down
102 changes: 0 additions & 102 deletions src/classes/Alias.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/classes/AliasFactory.ts

This file was deleted.

95 changes: 69 additions & 26 deletions src/classes/Aliases.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,90 @@
import { Service } from 'typedi';
import type DynamicaAlias from './Alias';
import DB from '@/services/DB';
import Logger from '@/services/Logger';
import MQTT from '../services/MQTT';

@Service()
export default class Aliases {
constructor(private mqtt: MQTT) {}
constructor(private mqtt: MQTT, private db: DB, private logger: Logger) {}

private aliases: DynamicaAlias[] = [];

public add(alias: DynamicaAlias) {
this.aliases.push(alias);
/**
* Delete an alias
* @param activity The activity to delete an alias for
* @param guildId The guild ID
*/
public async remove(activity: string, guildId: string) {
await this.db.alias.delete({
where: { guildId_activity: { activity, guildId } },
});
if (this.mqtt) {
this.mqtt.publish('dynamica/aliases', this.aliases.length.toString());
this.mqtt.publish('dynamica/aliases', (await this.count).toString());
}
}

public remove(activity: string, guildId: string) {
this.aliases = this.aliases.filter(
(alias) => !(alias.activity === activity && alias.guildId === guildId)
);
if (this.mqtt) {
this.mqtt.publish('dynamica/aliases', this.aliases.length.toString());
/**
* Get an alias
* @param activity The activity to get an alias for
* @param guildId The guild ID
* @returns The database entry
* @returns null if no alias exists
*/
public async get(activity: string, guildId: string) {
const dbEntry = await this.db.alias.findUnique({
where: { guildId_activity: { activity, guildId } },
});
if (!dbEntry) {
return null;
}
return dbEntry;
}

public get(activity: string, guildId: string) {
return this.aliases.find(
(alias) => alias.activity === activity && alias.guildId === guildId
);
public async getByGuild(guildId: string) {
const dbEntries = await this.db.alias.findMany({
where: { guildId },
});
return dbEntries;
}

public getByGuildId(guildId: string) {
return this.aliases.filter((alias) => alias.guildId === guildId);
/**
* Get the count of aliases
*/
get count() {
return this.db.alias.count();
}

get count() {
return this.aliases.length;
/**
* Create a new guild-specific alias
* @param guildId The guild ID
* @param activity The activity to create an alias for
* @param alias The alias to create
* @returns The database entry
*/
public async create(guildId: string, activity: string, alias: string) {
const newAlias = await this.db.alias.create({
data: {
guildId,
activity,
alias,
},
});

return newAlias;
}

public update(activity: string, guildId: string, newAlias: DynamicaAlias) {
const aliasIndex = this.aliases.findIndex(
(alias) => alias.activity === activity && alias.guildId === guildId
);
this.aliases[aliasIndex] = newAlias;
/**
* Update an existing alias
* @param activity The activity to update an alias for
* @param guildId The guild ID
* @param updatedAlias The alias to replace the existing one with
* @returns The database entry
*/
public async update(activity: string, guildId: string, updatedAlias: string) {
const dbAlias = await this.db.alias.update({
where: { guildId_activity: { activity, guildId } },
data: {
alias: updatedAlias,
},
});
return dbAlias;
}
}
2 changes: 1 addition & 1 deletion src/classes/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default class Commands {
try {
await command.response(interaction);
} catch (error) {
// logger.error(`Error while running command ${command.name}`, error);
this.logger.error(`Error while running command ${command.name}`, error);
interaction.reply({
embeds: [
ErrorEmbed(
Expand Down
8 changes: 4 additions & 4 deletions src/classes/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Token } from 'typedi';

type Awaitable<T> = Promise<T> | T;

export default interface Event<K extends keyof ClientEvents> {
event: K;
export default abstract class Event<K extends keyof ClientEvents> {
abstract event: K;

once: boolean;
abstract once: boolean;

response: (...args: ClientEvents[K]) => Awaitable<void>;
abstract response: (...args: ClientEvents[K]) => Awaitable<void>;
}

export const EventToken = new Token<Event<keyof ClientEvents>>('events');
Loading

0 comments on commit 4a9ee97

Please sign in to comment.