3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-16 14:11:50 +00:00
zeppelin/src/data/GuildMutes.ts

108 lines
2.5 KiB
TypeScript
Raw Normal View History

import moment from "moment-timezone";
import { Mute } from "./entities/Mute";
import { BaseRepository } from "./BaseRepository";
import { getRepository, Repository, Brackets } from "typeorm";
2018-07-07 17:03:13 +03:00
export class GuildMutes extends BaseRepository {
private mutes: Repository<Mute>;
2018-07-07 17:03:13 +03:00
constructor(guildId) {
super(guildId);
this.mutes = getRepository(Mute);
2018-07-07 17:03:13 +03:00
}
async getExpiredMutes(): Promise<Mute[]> {
return this.mutes
.createQueryBuilder("mutes")
.where("guild_id = :guild_id", { guild_id: this.guildId })
.andWhere("expires_at IS NOT NULL")
.andWhere("expires_at <= NOW()")
.getMany();
2018-07-07 17:03:13 +03:00
}
async findExistingMuteForUserId(userId: string): Promise<Mute> {
return this.mutes.findOne({
where: {
guild_id: this.guildId,
user_id: userId
}
});
2018-07-07 17:03:13 +03:00
}
async addMute(userId, expiryTime): Promise<Mute> {
2018-07-07 17:03:13 +03:00
const expiresAt = expiryTime
? moment()
.add(expiryTime, "ms")
.format("YYYY-MM-DD HH:mm:ss")
: null;
const result = await this.mutes.insert({
guild_id: this.guildId,
user_id: userId,
expires_at: expiresAt
});
2018-12-23 01:00:56 +02:00
return this.mutes.findOne({ where: result.identifiers[0] });
2018-07-07 17:03:13 +03:00
}
async updateExpiryTime(userId, newExpiryTime) {
const expiresAt = newExpiryTime
? moment()
.add(newExpiryTime, "ms")
.format("YYYY-MM-DD HH:mm:ss")
: null;
return this.mutes.update(
{
guild_id: this.guildId,
user_id: userId
},
{
2018-07-07 17:03:13 +03:00
expires_at: expiresAt
}
);
2018-07-07 17:03:13 +03:00
}
async addOrUpdateMute(userId, expiryTime): Promise<Mute> {
2018-07-07 17:03:13 +03:00
const existingMute = await this.findExistingMuteForUserId(userId);
if (existingMute) {
await this.updateExpiryTime(userId, expiryTime);
return this.findExistingMuteForUserId(userId);
2018-07-07 17:03:13 +03:00
} else {
return this.addMute(userId, expiryTime);
}
}
async getActiveMutes(): Promise<Mute[]> {
return this.mutes
.createQueryBuilder("mutes")
.where("guild_id = :guild_id", { guild_id: this.guildId })
.andWhere(
new Brackets(qb => {
qb.where("expires_at > NOW()").orWhere("expires_at IS NULL");
})
)
.getMany();
}
async setCaseId(userId: string, caseId: number) {
await this.mutes.update(
{
guild_id: this.guildId,
user_id: userId
},
{
case_id: caseId
}
);
}
async clear(userId) {
await this.mutes.delete({
guild_id: this.guildId,
user_id: userId
});
2018-07-07 17:03:13 +03:00
}
}