feat: pat command

This commit is contained in:
Andrew Glaze
2026-03-01 14:32:18 -05:00
parent 251b3b8109
commit cc489fccb6
7 changed files with 69 additions and 3 deletions

View File

@@ -5,7 +5,35 @@ struct Actions {
mention.member?.nick ?? mention.global_name ?? mention.username
}
static let hugRes = (try? String(contentsOfFile: "choices/hug.txt", encoding: .utf8))?
static func performAction(ctx: Gateway.MessageCreate, client: DiscordClient, resOpts: [String]) async throws {
let author = "**\(ctx.member?.nick ?? ctx.author?.global_name ?? ctx.author?.username ?? "Zundamon")**"
let dests = ctx.mentions.map(getUserFromMention).map({ "**\($0)**" })
let orig: String
let dest: String
if let firstDest = dests.first {
orig = "\(author)"
dest = "\(firstDest)"
} else {
orig = "**Zundamon**"
dest = "\(author)"
}
guard let res = resOpts.randomElement()?
.replacingOccurrences(of: "{orig}", with: orig)
.replacingOccurrences(of: "{dest}", with: dest)
else { print("retOptions empty"); return }
let retMsg = res
try await client.createMessage(
channelId: ctx.channel_id,
payload: .init(
embeds: [.init(description: retMsg)],
)
).guardSuccess()
}
static let hugRes = (try? String(contentsOfFile: "resources/choices/hug.txt", encoding: .utf8))?
.split(separator: "\n")
.map({ $0.trimmingCharacters(in: .whitespacesAndNewlines) })
@@ -44,7 +72,7 @@ struct Actions {
orig = "\(author)"
dest = "\(firstDest)"
} else {
orig = "Zundamon"
orig = "**Zundamon**"
dest = "\(author)"
}
@@ -63,4 +91,17 @@ struct Actions {
)
).guardSuccess()
}
static let patRes = (try? String(contentsOfFile: "resources/choices/pat.txt", encoding: .utf8))?
.split(separator: "\n")
.map({ $0.trimmingCharacters(in: .whitespacesAndNewlines) })
static func pat(
_ args: ArraySlice<String.SubSequence>,
client: DiscordClient,
ctx: Gateway.MessageCreate
) async throws {
guard let patRes = patRes else { print("pat.txt not loaded"); return }
try await performAction(ctx: ctx, client: client, resOpts: patRes)
}
}

View File

@@ -7,7 +7,7 @@ struct MessageHandler {
let client: any DiscordClient
static let prefix = ":"
static let zundaGifData = try? Data(contentsOf: URL(filePath: "media/zundamone.gif"))
static let zundaGifData = try? Data(contentsOf: URL(filePath: "resources/media/zundamone.gif"))
func handle() async throws {
guard !(ctx.author?.bot ?? false) else { return }
@@ -20,6 +20,8 @@ struct MessageHandler {
case "wow": try await handleWow(args)
case "domath": try await Wolfram.handleMath(args, client: client, ctx: ctx)
case "hug": try await Actions.hug(args, client: client, ctx: ctx)
case "pat": try await Actions.pat(args, client: client, ctx: ctx)
case "pet": try await Actions.pat(args, client: client, ctx: ctx)
default: break
}
} else if ctx.mentions.contains(where: { $0.id == Zundamon.ownID }) {