mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 20:35:02 +00:00
Add Slowmode plugin
This commit is contained in:
parent
b02dae2890
commit
0431b3c225
7 changed files with 406 additions and 1 deletions
121
src/data/GuildSlowmodes.ts
Normal file
121
src/data/GuildSlowmodes.ts
Normal file
|
@ -0,0 +1,121 @@
|
|||
import { BaseRepository } from "./BaseRepository";
|
||||
import { getRepository, Repository } from "typeorm";
|
||||
import { SlowmodeChannel } from "./entities/SlowmodeChannel";
|
||||
import { SlowmodeUser } from "./entities/SlowmodeUser";
|
||||
import moment from "moment-timezone";
|
||||
|
||||
export class GuildSlowmodes extends BaseRepository {
|
||||
private slowmodeChannels: Repository<SlowmodeChannel>;
|
||||
private slowmodeUsers: Repository<SlowmodeUser>;
|
||||
|
||||
constructor(guildId) {
|
||||
super(guildId);
|
||||
this.slowmodeChannels = getRepository(SlowmodeChannel);
|
||||
this.slowmodeUsers = getRepository(SlowmodeUser);
|
||||
}
|
||||
|
||||
async getChannelSlowmode(channelId): Promise<SlowmodeChannel> {
|
||||
return this.slowmodeChannels.findOne({
|
||||
where: {
|
||||
guild_id: this.guildId,
|
||||
channel_id: channelId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async setChannelSlowmode(channelId, seconds): Promise<void> {
|
||||
const existingSlowmode = await this.getChannelSlowmode(channelId);
|
||||
if (existingSlowmode) {
|
||||
await this.slowmodeChannels.update(
|
||||
{
|
||||
guild_id: this.guildId,
|
||||
channel_id: channelId
|
||||
},
|
||||
{
|
||||
slowmode_seconds: seconds
|
||||
}
|
||||
);
|
||||
} else {
|
||||
await this.slowmodeChannels.insert({
|
||||
guild_id: this.guildId,
|
||||
channel_id: channelId,
|
||||
slowmode_seconds: seconds
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async clearChannelSlowmode(channelId): Promise<void> {
|
||||
await this.slowmodeChannels.delete({
|
||||
guild_id: this.guildId,
|
||||
channel_id: channelId
|
||||
});
|
||||
}
|
||||
|
||||
async getChannelSlowmodeUser(channelId, userId): Promise<SlowmodeUser> {
|
||||
return this.slowmodeUsers.findOne({
|
||||
guild_id: this.guildId,
|
||||
channel_id: channelId,
|
||||
user_id: userId
|
||||
});
|
||||
}
|
||||
|
||||
async userHasSlowmode(channelId, userId): Promise<boolean> {
|
||||
return (await this.getChannelSlowmodeUser(channelId, userId)) != null;
|
||||
}
|
||||
|
||||
async addSlowmodeUser(channelId, userId): Promise<void> {
|
||||
const slowmode = await this.getChannelSlowmode(channelId);
|
||||
if (!slowmode) return;
|
||||
|
||||
const expiresAt = moment()
|
||||
.add(slowmode.slowmode_seconds, "seconds")
|
||||
.format("YYYY-MM-DD HH:mm:ss");
|
||||
|
||||
if (await this.userHasSlowmode(channelId, userId)) {
|
||||
// Update existing
|
||||
await this.slowmodeUsers.update(
|
||||
{
|
||||
guild_id: this.guildId,
|
||||
channel_id: channelId,
|
||||
user_id: userId
|
||||
},
|
||||
{
|
||||
expires_at: expiresAt
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// Add new
|
||||
await this.slowmodeUsers.insert({
|
||||
guild_id: this.guildId,
|
||||
channel_id: channelId,
|
||||
user_id: userId,
|
||||
expires_at: expiresAt
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async clearSlowmodeUser(channelId, userId): Promise<void> {
|
||||
await this.slowmodeUsers.delete({
|
||||
guild_id: this.guildId,
|
||||
channel_id: channelId,
|
||||
user_id: userId
|
||||
});
|
||||
}
|
||||
|
||||
async getChannelSlowmodeUsers(channelId): Promise<SlowmodeUser[]> {
|
||||
return this.slowmodeUsers.find({
|
||||
where: {
|
||||
guild_id: this.guildId,
|
||||
channel_id: channelId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async getExpiredSlowmodeUsers(): Promise<SlowmodeUser[]> {
|
||||
return this.slowmodeUsers
|
||||
.createQueryBuilder()
|
||||
.where("guild_id = :guildId", { guildId: this.guildId })
|
||||
.andWhere("expires_at <= NOW()")
|
||||
.getMany();
|
||||
}
|
||||
}
|
14
src/data/entities/SlowmodeChannel.ts
Normal file
14
src/data/entities/SlowmodeChannel.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { Entity, Column, PrimaryColumn } from "typeorm";
|
||||
|
||||
@Entity("slowmode_channels")
|
||||
export class SlowmodeChannel {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
guild_id: string;
|
||||
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
channel_id: string;
|
||||
|
||||
@Column() slowmode_seconds: number;
|
||||
}
|
18
src/data/entities/SlowmodeUser.ts
Normal file
18
src/data/entities/SlowmodeUser.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import { Entity, Column, PrimaryColumn } from "typeorm";
|
||||
|
||||
@Entity("slowmode_users")
|
||||
export class SlowmodeUser {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
guild_id: string;
|
||||
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
channel_id: string;
|
||||
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
user_id: string;
|
||||
|
||||
@Column() expires_at: string;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue