From c17abbd7ef3bbd2426f846796340103304b2e461 Mon Sep 17 00:00:00 2001 From: "Alex S. Glomsaas" Date: Sun, 2 Apr 2023 03:22:48 +0200 Subject: [PATCH] Rework !whois command --- .gitignore | 1 + .../xcschemes/mechasqueak.xcscheme | 2 +- Sources/mechasqueak/FuelRatsAPI/Group.swift | 33 ++++++++- .../mechasqueak/FuelRatsAPI/Nickname.swift | 10 ++- .../mechasqueak/Modules/AccountCommands.swift | 73 +++++++------------ .../mechasqueak/Modules/HelpCommands.swift | 2 +- Sources/mechasqueak/Templating/Stencil.swift | 39 ++++++++++ commandref.html | 26 ++++--- localisation/en.json | 4 + templates/whois.stencil | 22 ++++++ 10 files changed, 147 insertions(+), 65 deletions(-) create mode 100644 templates/whois.stencil diff --git a/.gitignore b/.gitignore index d0fa7fc4..e06d83bf 100644 --- a/.gitignore +++ b/.gitignore @@ -138,3 +138,4 @@ xcuserdata **/xcshareddata/WorkspaceSettings.xcsettings # End of https://www.toptal.com/developers/gitignore/api/macos,xcode,swiftpackagemanager,swift +config.json diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/mechasqueak.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/mechasqueak.xcscheme index ef8f51da..fa45ff39 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/mechasqueak.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/mechasqueak.xcscheme @@ -103,7 +103,7 @@ diff --git a/Sources/mechasqueak/FuelRatsAPI/Group.swift b/Sources/mechasqueak/FuelRatsAPI/Group.swift index 7cf4466f..1120b47c 100644 --- a/Sources/mechasqueak/FuelRatsAPI/Group.swift +++ b/Sources/mechasqueak/FuelRatsAPI/Group.swift @@ -26,6 +26,7 @@ import Foundation import JSONAPI import NIO import AsyncHTTPClient +import IRCKit enum GroupDescription: ResourceObjectDescription { public static var jsonType: String { return "groups" } @@ -121,18 +122,39 @@ typealias GroupSearchDocument = Document, NoIncludes> extension Group { var groupNameMap: [String: String] { return [ - "verified": "Verified User", + "verified": "Verified", "developer": "Developer", "rat": "Drilled Rat", "dispatch": "Drilled Dispatch", "trainer": "Trainer", "traineradmin": "Training Manager", + "merch": "Quartermaster", "overseer": "Overseer", "techrat": "Tech rat", "moderator": "Moderator", "operations": "Operations team", "netadmin": "Network administrator", - "admin": "Network moderator" + "admin": "Network moderator", + "owner": "Special snowflake", + ] + } + + var groupColor: [String: IRCColor] { + return [ + "verified": .Grey, + "developer": .LightBlue, + "rat": .LightGreen, + "dispatch": .Green, + "trainer": .Yellow, + "traineradmin": .Purple, + "merch": .Grey, + "overseer": .Orange, + "techrat": .LightBlue, + "moderator": .LightRed, + "operations": .Purple, + "netadmin": .LightBlue, + "admin": .Purple, + "owner": .Purple ] } @@ -140,6 +162,13 @@ extension Group { return groupNameMap[self.name] ?? self.name } + var ircRepresentation: String { + if let color = groupColor[self.name] { + return IRCFormat.color(color, groupDescription) + } + return groupDescription + } + static func getList () async throws -> GroupSearchDocument { let request = try HTTPClient.Request(apiPath: "/groups", method: .GET) diff --git a/Sources/mechasqueak/FuelRatsAPI/Nickname.swift b/Sources/mechasqueak/FuelRatsAPI/Nickname.swift index 6376e322..fcc6d81e 100644 --- a/Sources/mechasqueak/FuelRatsAPI/Nickname.swift +++ b/Sources/mechasqueak/FuelRatsAPI/Nickname.swift @@ -77,13 +77,17 @@ extension NicknameSearchDocument { }) }) ?? [] } - - var permissions: [AccountPermission] { + + var groups: [Group] { let groupIds = self.user?.relationships.groups?.ids ?? [] return self.body.includes![Group.self].filter({ groupIds.contains($0.id) - }).flatMap({ + }) + } + + var permissions: [AccountPermission] { + self.groups.flatMap({ $0.attributes.permissions.value }) } diff --git a/Sources/mechasqueak/Modules/AccountCommands.swift b/Sources/mechasqueak/Modules/AccountCommands.swift index 8d4b59f7..b85a5e22 100644 --- a/Sources/mechasqueak/Modules/AccountCommands.swift +++ b/Sources/mechasqueak/Modules/AccountCommands.swift @@ -62,20 +62,6 @@ class AccountCommands: IRCBotModule { return } - let rats = associatedNickname.ratsBelongingTo(user: apiUser).map({ (rat: Rat) -> String in - var description = "\(rat.attributes.name.value)" - - if rat.platform == .PC { - description += " (\(rat.attributes.platform.value.ircRepresentable) \(rat.attributes.expansion.value.ircRepresentable))" - } else { - description += " (\(rat.attributes.platform.value.ircRepresentable))" - } - if rat.data.permits?.contains("Pilots' Federation District") == true { - description += " (\(IRCFormat.color(.Grey, "Starter Zone")))" - } - return description - }).joined(separator: ", ") - let joinedDate = associatedNickname.ratsBelongingTo(user: apiUser).reduce(nil, { (acc: Date?, rat: Rat) -> Date? in if acc == nil || rat.attributes.createdAt.value < acc! { return rat.attributes.createdAt.value @@ -83,22 +69,25 @@ class AccountCommands: IRCBotModule { return acc }) - let verifiedStatus = associatedNickname.permissions.contains(.UserVerified) ? - IRCFormat.color(.LightGreen, "Verified") : - IRCFormat.color(.Orange, "Unverified") - - command.message.reply(key: "whoami.response", fromCommand: command, map: [ + let group = associatedNickname.groups.sorted(by: { + $0.priority > $1.priority + + }).first?.ircRepresentation + + let output = (try? stencil.renderLine(name: "whois.stencil", context: [ + "nick": command.message.user.nickname, "account": account, - "userId": apiUser.id.rawValue.ircRepresentation, - "rats": rats, + "id": associatedNickname.user?.id.rawValue.ircRepresentation ?? "", + "group": group as Any, + "displayId": command.options.contains("@"), "joined": joinedDate?.eliteFormattedString ?? "unknown", - "verified": verifiedStatus - ]) + "rats": associatedNickname.ratsBelongingTo(user: apiUser) + ])) ?? "" } @BotCommand( ["whois", "ratid", "who", "id"], - [.param("nickname", "SpaceDawg")], + [.param("nickname", "SpaceDawg"), .options(["@"])], category: .account, description: "Check the Fuel Rats account information the bot is associating with someone's nick.", permission: .RatReadOwn, @@ -142,20 +131,6 @@ class AccountCommands: IRCBotModule { return } - let rats = associatedNickname.ratsBelongingTo(user: apiUser).map({ (rat: Rat) -> String in - var description = "\(rat.attributes.name.value)" - - if rat.platform == .PC { - description += " (\(rat.attributes.platform.value.ircRepresentable) \(rat.attributes.expansion.value.ircRepresentable))" - } else { - description += " (\(rat.attributes.platform.value.ircRepresentable))" - } - if rat.data.permits?.contains("Pilots' Federation District") == true { - description += " (\(IRCFormat.color(.Grey, "Starter Zone")))" - } - return description - }).joined(separator: ", ") - let joinedDate = associatedNickname.ratsBelongingTo(user: apiUser).reduce(nil, { (acc: Date?, rat: Rat) -> Date? in if acc == nil || rat.attributes.createdAt.value < acc! { return rat.attributes.createdAt.value @@ -163,18 +138,22 @@ class AccountCommands: IRCBotModule { return acc }) - let verifiedStatus = associatedNickname.permissions.contains(.UserVerified) ? - IRCFormat.color(.LightGreen, "Verified") : - IRCFormat.color(.Orange, "Unverified") - - command.message.reply(key: "whois.response", fromCommand: command, map: [ + let group = associatedNickname.groups.sorted(by: { + $0.priority > $1.priority + + }).first?.ircRepresentation + + let output = (try? stencil.renderLine(name: "whois.stencil", context: [ "nick": nick, "account": account, - "userId": apiUser.id.rawValue.ircRepresentation, - "rats": rats, + "id": associatedNickname.user?.id.rawValue.ircRepresentation ?? "", + "group": group as Any, + "displayId": command.options.contains("@"), "joined": joinedDate?.eliteFormattedString ?? "unknown", - "verified": verifiedStatus - ]) + "rats": associatedNickname.ratsBelongingTo(user: apiUser) + ])) ?? "" + + command.message.reply(message: output) } @BotCommand( diff --git a/Sources/mechasqueak/Modules/HelpCommands.swift b/Sources/mechasqueak/Modules/HelpCommands.swift index 43d83b1b..bfe9a431 100644 --- a/Sources/mechasqueak/Modules/HelpCommands.swift +++ b/Sources/mechasqueak/Modules/HelpCommands.swift @@ -204,7 +204,7 @@ class HelpCommands: IRCBotModule { ]) let permissionGroups = helpCommand.permission?.groups .sorted(by: { $0.priority < $1.priority }) - .map({ $0.groupDescription }) ?? [] + .map({ $0.ircRepresentation }) ?? [] var secondLine = "" if helpCommand.commands.count > 1 { diff --git a/Sources/mechasqueak/Templating/Stencil.swift b/Sources/mechasqueak/Templating/Stencil.swift index dfc2ca9b..4ef822fc 100644 --- a/Sources/mechasqueak/Templating/Stencil.swift +++ b/Sources/mechasqueak/Templating/Stencil.swift @@ -162,6 +162,45 @@ private func generateEnvironment () -> Environment { return nil } + ext.registerFilter("platform") { (value: Any?) in + if let rat = value as? Rat { + if rat.platform == .PC { + return "\(rat.attributes.platform.value.ircRepresentable) \(rat.attributes.expansion.value.ircRepresentable)" + } + return rat.attributes.platform.value.ircRepresentable + } + + if let rescue = value as? Rescue { + return rescue.platformExpansion + } + + return nil + } + + ext.registerFilter("isStarterRat") { (value: Any?) in + if let rat = value as? Rat { + return rat.data.permits?.contains("Pilots' Federation District") + } + + return false + } + + ext.registerFilter("name") { (value: Any?) in + if let rat = value as? Rat { + return rat.name + } + + return nil + } + + ext.registerFilter("id") { (value: Any?) in + if let rat = value as? Rat { + return rat.id.rawValue.ircRepresentation + } + + return nil + } + return environment } diff --git a/commandref.html b/commandref.html index b947fd9c..392e4e96 100644 --- a/commandref.html +++ b/commandref.html @@ -1165,7 +1165,7 @@


- !renameid <rescue uuid> <client name>

+ !renameid <rescue uuid> <client name...>

Permissions: Overseer, Tech rat, Moderator, Operations team, Network moderator, Network administrator @@ -1793,7 +1793,7 @@

- Permissions: Verified User + Permissions: Verified

@@ -2017,7 +2017,7 @@


- !whois <nickname>

+ !whois [-@] <nickname>

Aliases: !ratid !who !id @@ -2026,11 +2026,15 @@

- Permissions: Verified User + Permissions: Verified

Check the Fuel Rats account information the bot is associating with someone's nick. +

+ Options: +

+ -@ Display user and rat UUIDs

@@ -2056,7 +2060,7 @@

- Permissions: Verified User + Permissions: Verified

@@ -2086,7 +2090,7 @@

- Permissions: Verified User + Permissions: Verified

@@ -2110,7 +2114,7 @@

!permits

- Permissions: Verified User + Permissions: Verified

@@ -2140,7 +2144,7 @@

- Permissions: Verified User + Permissions: Verified

@@ -2170,7 +2174,7 @@

- Permissions: Verified User + Permissions: Verified

@@ -2200,7 +2204,7 @@

- Permissions: Verified User + Permissions: Verified

@@ -2241,7 +2245,7 @@

- Permissions: Verified User + Permissions: Verified

diff --git a/localisation/en.json b/localisation/en.json index 14c05ac8..79b0c4b4 100644 --- a/localisation/en.json +++ b/localisation/en.json @@ -452,6 +452,10 @@ "help.command.quiet.mode": "Shows last time for another PC game version (l / h / o)", "sendhelp.nouser": "Could not find %{nick} in the channel", + + "help.command.whoami.@": "Display user and rat UUIDs", + "help.command.whois.@": "Display user and rat UUIDs", + "help.command.addcase.pc": "Set platform to PC", "help.command.addcase.xb": "Set platform to Xbox", "help.command.addcase.ps": "Set platform to PS4", diff --git a/templates/whois.stencil b/templates/whois.stencil new file mode 100644 index 00000000..f927db48 --- /dev/null +++ b/templates/whois.stencil @@ -0,0 +1,22 @@ +{{ nick }} is logged in as user {{ account }} +{% if group %} + ({{ group }}). +{% else %} + ({{ "Unverified"|color:4 }}). +{% endif %} +{% if displayId %} + ({{ id }}) +{% endif %} + Joined: {{ joined }}. Rats: +{% for rat in rats %} + {{ rat|name }} ({{ rat|platform }}) +{% if rat|isStarterRat %} + ({{ "Starter Zone"|color:14 }}) +{% endif %} +{% if displayId %} + ({{ rat|id }}) +{% endif %} +{% if not forloop.last %} +, +{% endif %} +{% endfor %}