2018-07-12 02:58:34 +03:00
|
|
|
import moment from "moment-timezone";
|
2018-10-26 06:41:20 +03:00
|
|
|
import { Mute } from "./entities/Mute";
|
|
|
|
import { BaseRepository } from "./BaseRepository";
|
|
|
|
import { getRepository, Repository, Brackets } from "typeorm";
|
2018-07-07 17:03:13 +03:00
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
export class GuildMutes extends BaseRepository {
|
|
|
|
private mutes: Repository<Mute>;
|
2018-07-07 17:03:13 +03:00
|
|
|
|
|
|
|
constructor(guildId) {
|
2018-10-26 06:41:20 +03:00
|
|
|
super(guildId);
|
|
|
|
this.mutes = getRepository(Mute);
|
2018-07-07 17:03:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async getExpiredMutes(): Promise<Mute[]> {
|
2018-10-26 06:41:20 +03:00
|
|
|
return this.mutes
|
|
|
|
.createQueryBuilder("mutes")
|
|
|
|
.where("guild_id = :guild_id", { guild_id: this.guildId })
|
2018-11-24 14:01:06 +02:00
|
|
|
.andWhere("expires_at IS NOT NULL")
|
|
|
|
.andWhere("expires_at <= NOW()")
|
2018-10-26 06:41:20 +03:00
|
|
|
.getMany();
|
2018-07-07 17:03:13 +03:00
|
|
|
}
|
|
|
|
|
2018-07-08 13:57:27 +03:00
|
|
|
async findExistingMuteForUserId(userId: string): Promise<Mute> {
|
2018-10-26 06:41:20 +03:00
|
|
|
return this.mutes.findOne({
|
|
|
|
where: {
|
|
|
|
guild_id: this.guildId,
|
2019-04-13 17:35:02 +03:00
|
|
|
user_id: userId,
|
|
|
|
},
|
2018-10-26 06:41:20 +03:00
|
|
|
});
|
2018-07-07 17:03:13 +03:00
|
|
|
}
|
|
|
|
|
2019-04-13 17:35:02 +03:00
|
|
|
async isMuted(userId: string): Promise<boolean> {
|
|
|
|
const mute = await this.findExistingMuteForUserId(userId);
|
|
|
|
return mute != null;
|
|
|
|
}
|
|
|
|
|
2018-11-25 17:04:26 +02: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;
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
const result = await this.mutes.insert({
|
2018-10-26 06:41:20 +03:00
|
|
|
guild_id: this.guildId,
|
|
|
|
user_id: userId,
|
2019-04-13 17:35:02 +03:00
|
|
|
expires_at: expiresAt,
|
2018-10-26 06:41:20 +03:00
|
|
|
});
|
2018-11-25 17:04:26 +02:00
|
|
|
|
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;
|
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
return this.mutes.update(
|
|
|
|
{
|
|
|
|
guild_id: this.guildId,
|
2019-04-13 17:35:02 +03:00
|
|
|
user_id: userId,
|
2018-10-26 06:41:20 +03:00
|
|
|
},
|
|
|
|
{
|
2019-04-13 17:35:02 +03:00
|
|
|
expires_at: expiresAt,
|
|
|
|
},
|
2018-10-26 06:41:20 +03:00
|
|
|
);
|
2018-07-07 17:03:13 +03:00
|
|
|
}
|
|
|
|
|
2018-08-05 00:18:50 +03:00
|
|
|
async getActiveMutes(): Promise<Mute[]> {
|
2018-10-26 06:41:20 +03:00
|
|
|
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");
|
2019-04-13 17:35:02 +03:00
|
|
|
}),
|
2018-10-26 06:41:20 +03:00
|
|
|
)
|
|
|
|
.getMany();
|
2018-08-05 00:18:50 +03:00
|
|
|
}
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
async setCaseId(userId: string, caseId: number) {
|
2018-10-26 06:41:20 +03:00
|
|
|
await this.mutes.update(
|
|
|
|
{
|
|
|
|
guild_id: this.guildId,
|
2019-04-13 17:35:02 +03:00
|
|
|
user_id: userId,
|
2018-10-26 06:41:20 +03:00
|
|
|
},
|
|
|
|
{
|
2019-04-13 17:35:02 +03:00
|
|
|
case_id: caseId,
|
|
|
|
},
|
2018-10-26 06:41:20 +03:00
|
|
|
);
|
2018-08-05 00:18:50 +03:00
|
|
|
}
|
|
|
|
|
2018-07-08 13:57:27 +03:00
|
|
|
async clear(userId) {
|
2018-10-26 06:41:20 +03:00
|
|
|
await this.mutes.delete({
|
|
|
|
guild_id: this.guildId,
|
2019-04-13 17:35:02 +03:00
|
|
|
user_id: userId,
|
2018-10-26 06:41:20 +03:00
|
|
|
});
|
2018-07-07 17:03:13 +03:00
|
|
|
}
|
|
|
|
}
|