Feature/66 add different commands per server #122

Merged
Vylpes merged 4 commits from feature/66-add-different-commands-per-server into develop 2022-04-09 14:11:06 +01:00
3 changed files with 67 additions and 39 deletions
Showing only changes of commit 5de2b4bd45 - Show all commits

View file

@ -1,9 +1,7 @@
import { TextChannel } from "discord.js"; import { TextChannel } from "discord.js";
import { SOURCE_MAPPING_PREFIX } from "ts-jest";
import { ICommandContext } from "../../contracts/ICommandContext"; import { ICommandContext } from "../../contracts/ICommandContext";
import ErrorEmbed from "../../helpers/embeds/ErrorEmbed";
import SettingsHelper from "../../helpers/SettingsHelper";
import { Command } from "../../type/command"; import { Command } from "../../type/command";
import { default as eLobby } from "../../entity/501231711271780357/Lobby";
export default class Lobby extends Command { export default class Lobby extends Command {
constructor() { constructor() {
@ -16,61 +14,42 @@ export default class Lobby extends Command {
if (!context.message.guild) return; if (!context.message.guild) return;
const channel = context.message.channel as TextChannel; const channel = context.message.channel as TextChannel;
const channelName = channel.name;
const channelId = channel.id; 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 (!lobby) {
this.SendDisabled(context);
if (!timeUsed) {
await this.RequestLobby(context, channelId, context.message.guild.id);
return; return;
} }
const timeNow = Date.now(); const timeNow = Date.now();
const timeCooldown = await SettingsHelper.GetSetting('commands.lobby.cooldown', context.message.guild.id) || "30"; const timeLength = lobby.Cooldown * 60 * 1000; // x minutes in ms
const timeLength = parseInt(timeCooldown) * 60 * 1000; // x minutes in ms
const timeAgo = timeNow - timeLength; const timeAgo = timeNow - timeLength;
// If it was less than x minutes ago // If it was less than x minutes ago
if (parseInt(timeUsed) > timeAgo) { if (lobby.LastUsed.getTime() > timeAgo) {
this.SendOnCooldown(context, timeLength, timeNow, parseInt(timeUsed)); this.SendOnCooldown(context, timeLength, new Date(timeNow), lobby.LastUsed);
return; return;
} }
await this.RequestLobby(context, channelId, context.message.guild.id); await this.RequestLobby(context, lobby);
} }
private async RequestLobby(context: ICommandContext, channelId: string, serverId: string) { private async RequestLobby(context: ICommandContext, lobby: eLobby) {
const roleId = await SettingsHelper.GetSetting(`commands.lobby.channel.${channelId}.role`, serverId); lobby.MarkAsUsed();
await lobby.Save(eLobby, lobby);
if (!roleId) { context.message.channel.send(`${context.message.author} would like to organise a lobby of **${lobby.Name}**! <@&${lobby.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}>`);
} }
private SendOnCooldown(context: ICommandContext, timeLength: number, timeNow: number, timeUsed: number) { private SendOnCooldown(context: ICommandContext, timeLength: number, timeNow: Date, timeUsed: Date) {
const timeLeft = Math.ceil((timeLength - (timeNow - timeUsed)) / 1000 / 60); 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**.`); 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.");
}
} }

View 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;
}
}

View file

@ -48,6 +48,10 @@ export default class Registry {
// Exclusive Commands: MankBot // Exclusive Commands: MankBot
client.RegisterCommand("lobby", new Lobby(), "501231711271780357"); client.RegisterCommand("lobby", new Lobby(), "501231711271780357");
client.RegisterCommand("entry", new Entry(), "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) { public static RegisterEvents(client: CoreClient) {