zappyzep/src/data/GuildReactionRoles.ts

63 lines
1.5 KiB
TypeScript
Raw Normal View History

import { ReactionRole } from "./entities/ReactionRole";
import { BaseRepository } from "./BaseRepository";
import { getRepository, Repository } from "typeorm";
2018-07-29 15:18:26 +03:00
export class GuildReactionRoles extends BaseRepository {
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> {
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 = null) {
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) {
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
});
}
}