Add lobby entity to database
This commit is contained in:
parent
5c00601e16
commit
5de2b4bd45
3 changed files with 67 additions and 39 deletions
|
@ -1,9 +1,7 @@
|
|||
import { TextChannel } from "discord.js";
|
||||
import { SOURCE_MAPPING_PREFIX } from "ts-jest";
|
||||
import { ICommandContext } from "../../contracts/ICommandContext";
|
||||
import ErrorEmbed from "../../helpers/embeds/ErrorEmbed";
|
||||
import SettingsHelper from "../../helpers/SettingsHelper";
|
||||
import { Command } from "../../type/command";
|
||||
import { default as eLobby } from "../../entity/501231711271780357/Lobby";
|
||||
|
||||
export default class Lobby extends Command {
|
||||
constructor() {
|
||||
|
@ -16,61 +14,42 @@ export default class Lobby extends Command {
|
|||
if (!context.message.guild) return;
|
||||
|
||||
const channel = context.message.channel as TextChannel;
|
||||
const channelName = channel.name;
|
||||
const channelId = channel.id;
|
||||
|
||||
// TODO: Check if channel is enabled for this
|
||||
const lobby = await eLobby.FetchOneByChannelId(channelId);
|
||||
|
||||
const timeUsed = await SettingsHelper.GetSetting(`commands.lobby.channel.${channelId}.time`, context.message.guild.id);
|
||||
|
||||
if (!timeUsed) {
|
||||
await this.RequestLobby(context, channelId, context.message.guild.id);
|
||||
if (!lobby) {
|
||||
this.SendDisabled(context);
|
||||
return;
|
||||
}
|
||||
|
||||
const timeNow = Date.now();
|
||||
const timeCooldown = await SettingsHelper.GetSetting('commands.lobby.cooldown', context.message.guild.id) || "30";
|
||||
const timeLength = parseInt(timeCooldown) * 60 * 1000; // x minutes in ms
|
||||
const timeLength = lobby.Cooldown * 60 * 1000; // x minutes in ms
|
||||
const timeAgo = timeNow - timeLength;
|
||||
|
||||
// If it was less than x minutes ago
|
||||
if (parseInt(timeUsed) > timeAgo) {
|
||||
this.SendOnCooldown(context, timeLength, timeNow, parseInt(timeUsed));
|
||||
if (lobby.LastUsed.getTime() > timeAgo) {
|
||||
this.SendOnCooldown(context, timeLength, new Date(timeNow), lobby.LastUsed);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.RequestLobby(context, channelId, context.message.guild.id);
|
||||
await this.RequestLobby(context, lobby);
|
||||
}
|
||||
|
||||
private async RequestLobby(context: ICommandContext, channelId: string, serverId: string) {
|
||||
const roleId = await SettingsHelper.GetSetting(`commands.lobby.channel.${channelId}.role`, serverId);
|
||||
private async RequestLobby(context: ICommandContext, lobby: eLobby) {
|
||||
lobby.MarkAsUsed();
|
||||
await lobby.Save(eLobby, lobby);
|
||||
|
||||
if (!roleId) {
|
||||
const errorEmbed = new ErrorEmbed(context, "Unable to find the channel's role");
|
||||
errorEmbed.setFooter(`commands.lobby.channel.${channelId}.role`);
|
||||
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
const gameName = await SettingsHelper.GetSetting(`commands.lobby.channel.${channelId}.game`, serverId);
|
||||
|
||||
if (!gameName) {
|
||||
const errorEmbed = new ErrorEmbed(context, "Unable to find the channel's game name");
|
||||
errorEmbed.setFooter(`commands.lobby.channel.${channelId}.game`);
|
||||
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
return;
|
||||
}
|
||||
|
||||
await SettingsHelper.SetSetting(`commands.lobby.channel.${channelId}.time`, serverId, `${Date.now()}`);
|
||||
|
||||
context.message.channel.send(`${context.message.author} would like to organise a lobby of **${gameName}**! <@&${roleId}>`);
|
||||
context.message.channel.send(`${context.message.author} would like to organise a lobby of **${lobby.Name}**! <@&${lobby.RoleId}>`);
|
||||
}
|
||||
|
||||
private SendOnCooldown(context: ICommandContext, timeLength: number, timeNow: number, timeUsed: number) {
|
||||
const timeLeft = Math.ceil((timeLength - (timeNow - timeUsed)) / 1000 / 60);
|
||||
private SendOnCooldown(context: ICommandContext, timeLength: number, timeNow: Date, timeUsed: Date) {
|
||||
const timeLeft = Math.ceil((timeLength - (timeNow.getTime() - timeUsed.getTime())) / 1000 / 60);
|
||||
|
||||
context.message.reply(`Requesting a lobby for this game is on cooldown! Please try again in **${timeLeft} minutes**.`);
|
||||
}
|
||||
|
||||
private SendDisabled(context: ICommandContext) {
|
||||
context.message.reply("This channel hasn't been setup for lobbies.");
|
||||
}
|
||||
}
|
45
src/entity/501231711271780357/Lobby.ts
Normal file
45
src/entity/501231711271780357/Lobby.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
import { Column, Entity, getConnection } from "typeorm";
|
||||
import BaseEntity from "../../contracts/BaseEntity";
|
||||
|
||||
@Entity()
|
||||
export default class Lobby extends BaseEntity {
|
||||
constructor(channelId: string, roleId: string, cooldown: number, name: string) {
|
||||
super();
|
||||
|
||||
this.ChannelId = channelId;
|
||||
this.RoleId = roleId;
|
||||
this.Cooldown = cooldown;
|
||||
this.Name = name;
|
||||
|
||||
this.LastUsed = new Date(0);
|
||||
}
|
||||
|
||||
@Column()
|
||||
public ChannelId: string;
|
||||
|
||||
@Column()
|
||||
public RoleId: string;
|
||||
|
||||
@Column()
|
||||
public Cooldown: number;
|
||||
|
||||
@Column()
|
||||
public LastUsed: Date;
|
||||
|
||||
@Column()
|
||||
public Name: string;
|
||||
|
||||
public MarkAsUsed() {
|
||||
this.LastUsed = new Date();
|
||||
}
|
||||
|
||||
public static async FetchOneByChannelId(channelId: string, relations?: string[]): Promise<Lobby | undefined> {
|
||||
const connection = getConnection();
|
||||
|
||||
const repository = connection.getRepository(Lobby);
|
||||
|
||||
const single = await repository.findOne({ ChannelId: channelId }, { relations: relations || [] });
|
||||
|
||||
return single;
|
||||
}
|
||||
}
|
|
@ -48,6 +48,10 @@ export default class Registry {
|
|||
// Exclusive Commands: MankBot
|
||||
client.RegisterCommand("lobby", new Lobby(), "501231711271780357");
|
||||
client.RegisterCommand("entry", new Entry(), "501231711271780357");
|
||||
|
||||
// Add Exclusive Commands to Test Server
|
||||
client.RegisterCommand("lobby", new Lobby(), "442730357897429002");
|
||||
client.RegisterCommand("entry", new Entry(), "442730357897429002");
|
||||
}
|
||||
|
||||
public static RegisterEvents(client: CoreClient) {
|
||||
|
|
Loading…
Reference in a new issue