This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stop using classes for storing channels
- Loading branch information
1 parent
16eb053
commit 4a9ee97
Showing
49 changed files
with
825 additions
and
948 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.