zappyzep/backend/src/data/GuildReactionRoles.ts

75 lines
1.7 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,
},
2021-06-06 02:41:06 +02:00
order: {
order: "ASC",
},
});
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
}
2021-06-06 02:41:06 +02:00
async add(
channelId: string,
messageId: string,
emoji: string,
roleId: string,
exclusive?: boolean,
position?: number,
) {
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),
2021-06-06 02:41:06 +02:00
order: position,
2018-07-29 15:18:26 +03:00
});
}
}