zappyzep/backend/src/data/GuildAutoReactions.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-07-01 12:17:45 +00:00
import { Repository } from "typeorm";
import { BaseGuildRepository } from "./BaseGuildRepository";
2023-07-01 12:17:45 +00:00
import { dataSource } from "./dataSource";
2019-01-12 13:42:11 +02:00
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);
2023-07-01 12:17:45 +00:00
this.autoReactions = dataSource.getRepository(AutoReaction);
2019-01-12 13:42:11 +02:00
}
async all(): Promise<AutoReaction[]> {
return this.autoReactions.find({
where: {
guild_id: this.guildId,
},
2019-01-12 13:42:11 +02:00
});
}
2023-07-01 12:17:45 +00:00
async getForChannel(channelId: string): Promise<AutoReaction | null> {
2019-01-12 13:42:11 +02:00
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
});
}
}
}