feat: hug command
This commit is contained in:
66
Sources/zundamon/Actions.swift
Normal file
66
Sources/zundamon/Actions.swift
Normal file
@@ -0,0 +1,66 @@
|
||||
import DiscordBM
|
||||
|
||||
struct Actions {
|
||||
static func getUserFromMention(_ mention: MentionUser) -> String {
|
||||
mention.member?.nick ?? mention.global_name ?? mention.username
|
||||
}
|
||||
|
||||
static let hugRes = (try? String(contentsOfFile: "choices/hug.txt", encoding: .utf8))?
|
||||
.split(separator: "\n")
|
||||
.map({ $0.trimmingCharacters(in: .whitespacesAndNewlines) })
|
||||
|
||||
static func hug(
|
||||
_ args: ArraySlice<String.SubSequence>,
|
||||
client: DiscordClient,
|
||||
ctx: Gateway.MessageCreate
|
||||
) async throws {
|
||||
guard let hugRes = hugRes else { print("hug.txt not loaded"); return }
|
||||
let author = "**\(ctx.member?.nick ?? ctx.author?.global_name ?? ctx.author?.username ?? "Zundamon")**"
|
||||
let dests = ctx.mentions.map(getUserFromMention).map({ "**\($0)**" })
|
||||
|
||||
let retMsg: String
|
||||
|
||||
if dests.count > 1 {
|
||||
let groupHugs = [
|
||||
"{subjects} all huddled together.",
|
||||
"{subjects} hugged each other pairwise, generating a total of **{total}** hugs.",
|
||||
"{subjects} hugged each other at the same time in the same place (although I'm not sure how that works with the current understanding of spacetime).",
|
||||
]
|
||||
|
||||
let group = [author] + dests
|
||||
let total = String(group.count)
|
||||
let subjects = String(group.joined(by: ", "))
|
||||
|
||||
guard let res = groupHugs.randomElement()?
|
||||
.replacingOccurrences(of: "{subjects}", with: subjects)
|
||||
.replacingOccurrences(of: "{total}", with: total)
|
||||
else { print("groupHugs.randomElement() returned null"); return }
|
||||
|
||||
retMsg = res
|
||||
} else {
|
||||
let orig: String
|
||||
let dest: String
|
||||
if let firstDest = dests.first {
|
||||
orig = "**\(author)**"
|
||||
dest = "**\(firstDest)**"
|
||||
} else {
|
||||
orig = "**Zundamon**"
|
||||
dest = "**\(author)**"
|
||||
}
|
||||
|
||||
guard let res = hugRes.randomElement()?
|
||||
.replacingOccurrences(of: "{orig}", with: orig)
|
||||
.replacingOccurrences(of: "{dest}", with: dest)
|
||||
else { print("hug.txt empty"); return }
|
||||
|
||||
retMsg = res
|
||||
}
|
||||
|
||||
try await client.createMessage(
|
||||
channelId: ctx.channel_id,
|
||||
payload: .init(
|
||||
embeds: [.init(description: retMsg)],
|
||||
)
|
||||
).guardSuccess()
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ struct MessageHandler {
|
||||
switch command {
|
||||
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)
|
||||
default: break
|
||||
}
|
||||
} else if ctx.mentions.contains(where: { $0.id == Zundamon.ownID }) {
|
||||
|
||||
Reference in New Issue
Block a user