const ESMCommand = require("../esm_command"); module.exports = class ESMCommand_Add extends ESMCommand { constructor(bot) { super(bot); this.permissions = { requires_registration: true, requires_server: true }; this.commandParams = { serverID: { regex: this.util.regex.serverID.base }, territoryID: { regex: this.util.regex.territoryID.base }, targetUID: { regex: this.util.regex.targetAcceptDeny.base } }; this.information = { category: "territory", params: "<server_id> <territory_id> <target_uid | @user>", help: "Adds a player to the build rights for a territory" }; this.configuration = { allowedInTextChannels: { modifiable: true, default: false }, cooldown: { modifiable: true, default: [2, "seconds"] }, enabled: { modifiable: true, default: true } }; } async fromDiscord() { let targetUser = null; if (this.params.targetUID === "accept") { let requests = await this.db.getRequests(this.params.steamUID, { type: "addPlayerToTerritory", serverID: this.params.serverID, territoryID: this.params.territoryID }); if (this.util.isEmpty(requests)) { return this.ESMBot.send(this.message.channel, `${this.info.author.tag}, you do not have any pending requests for this territory`); } this.ESMBot.send(requests.channelID, `${(await this.ESMBot.getUser(requests.requestorID)).toString()}, ${this.message.author.toString()} has accepted your request to be added to \`${requests.territoryID}\` on \`${this.params.serverID}\``); this.ESMBot.send(this.message.channel, "Request accepted"); this.db.removeRequest(requests.id); let user = await this.ESMBot.getUser(requests.requestorID); return this.send({ server_id: this.params.serverID, channel: requests.channelID, author: { tag: user.toString(), id: user.id }, command: "add", parameters: { function_name: "addPlayerToTerritory", territory_id: requests.territoryID, target_uid: this.params.steamUID, uid: requests.requestorUID } }); } if (this.params.targetUID === "decline") { let requests = await this.db.getRequests(this.params.steamUID, { type: "addPlayerToTerritory", serverID: this.params.serverID, territoryID: this.params.territoryID }); if (this.util.isEmpty(requests)) { return this.ESMBot.send(this.message.channel, "You do not have any pending requests for this territory"); } this.db.removeRequest(requests.id); return this.ESMBot.send(this.message.channel, "Request declined"); } if (this.util.regex.discordTag.base.test(this.params.targetUID)) { let discordUser = await this.ESMBot.getUser(this.params.targetUID); if (discordUser == null) { return this.ESMBot.send(this.message.channel, this.ESMBot.errors.format(this.message.author, "USER_NOT_FOUND")); } let isRegistered = await this.db.isRegistered(discordUser.id); if (!isRegistered) { return this.ESMBot.send(this.message.channel, this.ESMBot.errors.format(this.message.author, "USER_NOT_REGISTERED")); } this.params.targetUID = await this.db.getSteamUIDFromID(discordUser.id); if (this.params.targetUID == null) return; targetUser = discordUser; } else { targetUser = await this.ESMBot.getUser((await this.db.getDiscordIDsFromSteamUID(this.params.targetUID))[0]); } if (targetUser == null) { return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, I cannot send the request. I was unable to find a user with that UID`); } let requests = await this.db.getRequests(this.params.targetUID, { type: "addPlayerToTerritory", serverID: this.params.serverID, territoryID: this.params.territoryID }); if (!this.util.isEmpty(requests)) { return this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, this user already has a pending request`); } this.db.addRequest(this.params.targetUID, { type: "addPlayerToTerritory", serverID: this.params.serverID, territoryID: this.params.territoryID, requestorUID: this.params.steamUID, channelID: this.message.channel.id, requestorID: this.message.author.id }); this.ESMBot.send(targetUser, { author: { name: this.message.author.tag, icon_url: this.message.author.avatarURL() }, title: `${this.message.author.username} wants to add you to their territory \`${this.params.territoryID}\` on \`${this.params.serverID}\``, fields: [{ name: "To Accept", value: `Reply with \`!add ${this.params.serverID} ${this.params.territoryID} accept\`` }, { name: "To Decline", value: `Reply with \`!add ${this.params.serverID} ${this.params.territoryID} decline\`` } ], footer: { text: "This request will decline after 1 day" } }); this.ESMBot.send(this.message.channel, `${this.message.author.toString()}, I've sent this user a request. They have 1 day to accept this request`); } async fromServer() { this.ESMBot.send(this.info.channel, `${this.info.author.tag}, \`${this.info.parameters.target_uid}\` has been added to \`${this.info.parameters.territory_id}\``); } }