zappyzep/backend/src/data/GuildReactionRoles.ts

64 lines
1.5 KiB
TypeScript
Raw Normal View History

import { ReactionRole } from "./entities/ReactionRole";
import { BaseGuildRepository } from "./BaseGuildRepository";
import { getRepository, Repository } from "typeorm";
2018-07-29 15:18:26 +03:00
export class GuildReactionRoles extends BaseGuildRepository {
private reactionRoles: Repository<ReactionRole>;
2018-07-29 15:18:26 +03:00
constructor(guildId) {
super(guildId);
this.reactionRoles = getRepository(ReactionRole);
2018-07-29 15:18:26 +03:00
}
async all(): Promise<ReactionRole[]> {
return this.reactionRoles.find({
where: {
guild_id: this.guildId,
},
});
2018-07-29 15:18:26 +03:00
}
async getForMessage(messageId: string): Promise<ReactionRole[]> {
return this.reactionRoles.find({
where: {
guild_id: this.guildId,
message_id: messageId,
},
});
2018-07-29 15:18:26 +03:00
}
async getByMessageAndEmoji(messageId: string, emoji: string): Promise<ReactionRole | undefined> {
return this.reactionRoles.findOne({
where: {
guild_id: this.guildId,
message_id: messageId,
emoji,
},
});
2018-07-29 15:18:26 +03:00
}
async removeFromMessage(messageId: string, emoji?: string) {
const criteria: any = {
guild_id: this.guildId,
message_id: messageId,
};
2018-07-29 15:18:26 +03:00
if (emoji) {
criteria.emoji = emoji;
2018-07-29 15:18:26 +03:00
}
await this.reactionRoles.delete(criteria);
2018-07-29 15:18:26 +03:00
}
async add(channelId: string, messageId: string, emoji: string, roleId: string, exclusive?: boolean) {
await this.reactionRoles.insert({
2018-07-29 15:18:26 +03:00
guild_id: this.guildId,
channel_id: channelId,
message_id: messageId,
emoji,
role_id: roleId,
is_exclusive: Boolean(exclusive),
2018-07-29 15:18:26 +03:00
});
}
}