2018-07-12 02:53:26 +03:00
|
|
|
import * as knex from "../knex";
|
2018-07-08 13:57:27 +03:00
|
|
|
import * as moment from "moment-timezone";
|
2018-07-07 17:03:13 +03:00
|
|
|
import Mute from "../models/Mute";
|
|
|
|
|
|
|
|
export class GuildMutes {
|
|
|
|
protected guildId: string;
|
|
|
|
|
|
|
|
constructor(guildId) {
|
|
|
|
this.guildId = guildId;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getExpiredMutes(): Promise<Mute[]> {
|
|
|
|
const result = await knex("mutes")
|
|
|
|
.where("guild_id", this.guildId)
|
|
|
|
.whereNotNull("expires_at")
|
2018-07-08 13:57:27 +03:00
|
|
|
.whereRaw("expires_at <= NOW()")
|
2018-07-07 17:03:13 +03:00
|
|
|
.select();
|
|
|
|
|
|
|
|
return result.map(r => new Mute(r));
|
|
|
|
}
|
|
|
|
|
2018-07-08 13:57:27 +03:00
|
|
|
async findExistingMuteForUserId(userId: string): Promise<Mute> {
|
2018-07-07 17:03:13 +03:00
|
|
|
const result = await knex("mutes")
|
|
|
|
.where("guild_id", this.guildId)
|
|
|
|
.where("user_id", userId)
|
|
|
|
.first();
|
|
|
|
|
2018-07-08 13:57:27 +03:00
|
|
|
return result ? new Mute(result) : null;
|
2018-07-07 17:03:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async addMute(userId, expiryTime) {
|
|
|
|
const expiresAt = expiryTime
|
|
|
|
? moment()
|
|
|
|
.add(expiryTime, "ms")
|
|
|
|
.format("YYYY-MM-DD HH:mm:ss")
|
|
|
|
: null;
|
|
|
|
|
|
|
|
return knex
|
|
|
|
.insert({
|
|
|
|
guild_id: this.guildId,
|
|
|
|
user_id: userId,
|
|
|
|
expires_at: expiresAt
|
|
|
|
})
|
|
|
|
.into("mutes");
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateExpiryTime(userId, newExpiryTime) {
|
|
|
|
const expiresAt = newExpiryTime
|
|
|
|
? moment()
|
|
|
|
.add(newExpiryTime, "ms")
|
|
|
|
.format("YYYY-MM-DD HH:mm:ss")
|
|
|
|
: null;
|
|
|
|
|
|
|
|
return knex("mutes")
|
|
|
|
.where("guild_id", this.guildId)
|
|
|
|
.where("user_id", userId)
|
|
|
|
.update({
|
|
|
|
expires_at: expiresAt
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async addOrUpdateMute(userId, expiryTime) {
|
|
|
|
const existingMute = await this.findExistingMuteForUserId(userId);
|
|
|
|
|
|
|
|
if (existingMute) {
|
|
|
|
return this.updateExpiryTime(userId, expiryTime);
|
|
|
|
} else {
|
|
|
|
return this.addMute(userId, expiryTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 13:57:27 +03:00
|
|
|
async clear(userId) {
|
|
|
|
return knex("mutes")
|
2018-07-07 17:03:13 +03:00
|
|
|
.where("guild_id", this.guildId)
|
|
|
|
.where("user_id", userId)
|
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
}
|