3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-19 07:20:00 +00:00
zeppelin/backend/src/data/GuildAutoReactions.ts

58 lines
1.3 KiB
TypeScript
Raw Normal View History

import { BaseGuildRepository } from "./BaseGuildRepository";
2019-01-12 13:42:11 +02:00
import { getRepository, Repository } from "typeorm";
import { AutoReaction } from "./entities/AutoReaction";
export class GuildAutoReactions extends BaseGuildRepository {
2019-01-12 13:42:11 +02:00
private autoReactions: Repository<AutoReaction>;
constructor(guildId) {
super(guildId);
this.autoReactions = getRepository(AutoReaction);
}
async all(): Promise<AutoReaction[]> {
return this.autoReactions.find({
where: {
guild_id: this.guildId,
},
2019-01-12 13:42:11 +02:00
});
}
async getForChannel(channelId: string): Promise<AutoReaction> {
return this.autoReactions.findOne({
where: {
guild_id: this.guildId,
channel_id: channelId,
},
2019-01-12 13:42:11 +02:00
});
}
async removeFromChannel(channelId: string) {
await this.autoReactions.delete({
guild_id: this.guildId,
channel_id: channelId,
2019-01-12 13:42:11 +02:00
});
}
async set(channelId: string, reactions: string[]) {
const existingRecord = await this.getForChannel(channelId);
if (existingRecord) {
this.autoReactions.update(
{
guild_id: this.guildId,
channel_id: channelId,
2019-01-12 13:42:11 +02:00
},
{
reactions,
},
2019-01-12 13:42:11 +02:00
);
} else {
await this.autoReactions.insert({
guild_id: this.guildId,
channel_id: channelId,
reactions,
2019-01-12 13:42:11 +02:00
});
}
}
}