hippietrail
hippietrail
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by hippietrail on 2/22/2024 in #djs-questions
How best to handle `unknown interaction` on `deferReply`
Nomadding in Southeast Asia. My bot was working fine in Thailand but gets intermittent unknown interaction errors a lot here in Laos. Chased it for a couple of days looking at the usual reasons it occurs but it seems I'm getting it at the beginning of my handler where I do deferReply which does not seem to be among the usual causes. Internet here is a bit laggy/bursty I suppose. Is there a way to check whether my interaction is still valid or can I only catch the exception when I try to use it? If exception is the only way, what's the elegant way? I can't return from .catch() since it's a closure. Putting it all in try/catch would lump this expected exception in with random exceptions which doesn't feel right. I don't think I can put the rest of the handler inside .then()
9 replies
DIAdiscord.js - Imagine an app
Created by hippietrail on 1/4/2024 in #djs-questions
How to add support for private slash commands
Not a big Discord user so lots of common stuff I don't know. But I've made a bot for use just in one channel with a couple of friends. I have slash commands working and I've experimented enough to send and receive DMs. But now I want to add support slash commands in DMs. I can't seem to find the next step via Google, the official docs, Stack Overflow, searching this discord, or asking AI chatbots. Do I have to modify some stuff on my bot's dev page? Pass some more settings in new Client() in my bot code? Or both?
13 replies
TSDThe Swift Den
Created by hippietrail on 9/9/2023 in #swift-development
Can progress reporting be added to this style of https transfer?
import Foundation

func fetchData() async throws -> Data {
let url = URL(string: "https://gist.github.com/khaykov/a6105154becce4c0530da38e723c2330/raw/41ab415ac41c93a198f7da5b47d604956157c5c3/gistfile1.txt")!

var request = URLRequest(url: url)

let (data, _) = try await URLSession.shared.data(for: request)
return data
}

@main
struct Main {
static func main() async throws {
do {
let foo = try await fetchData()
let bar = String(data: foo, encoding: .utf8)
dump(bar)
} catch {
print("** \(error.localizedDescription)")
}
}
}
import Foundation

func fetchData() async throws -> Data {
let url = URL(string: "https://gist.github.com/khaykov/a6105154becce4c0530da38e723c2330/raw/41ab415ac41c93a198f7da5b47d604956157c5c3/gistfile1.txt")!

var request = URLRequest(url: url)

let (data, _) = try await URLSession.shared.data(for: request)
return data
}

@main
struct Main {
static func main() async throws {
do {
let foo = try await fetchData()
let bar = String(data: foo, encoding: .utf8)
dump(bar)
} catch {
print("** \(error.localizedDescription)")
}
}
}
I've asked several AI chatbots and none of their attempts have worked. I'm guessing I should be using the old dataTask ways to do this and that trying to do it with async/await isn't really implemented/supported yet. But even though I'm an old programmer I'm very new to the Apple ecosystem.
5 replies
TSDThe Swift Den
Created by hippietrail on 8/30/2023 in #swift-development
Is it possible to replicate Mac's standard Help menu Search item?
No description
3 replies
TSDThe Swift Den
Created by hippietrail on 8/27/2023 in #swift-development
How to position vertical Slider
I'm an old programmer but new to Mac and SwiftUI. I'm trying to get a vertical Slider positioned right up against the right edge of the window. Apparently this requires rotation:
Slider(value: $vx).rotationEffect(.degrees(v1))
Slider(value: $vx).rotationEffect(.degrees(v1))
But this rotation is just an effect. All measurements, bounding boxes, alignment, etc that effect its size and position behave as though it's still horizontal, which makes it incredibly unintuitive and frustrating to reason about, especially when new and unfamiliar with how positioning etc is done even for normal UI elements. The very few Google hits for this have poor answers like just a slider in the middle, or a complete custom replacement that doesn't behave like a native one. (By the way Android has the exact some problem in both XML and Jetpack Compose. AppKit for one is intuitive though.)
25 replies
TSDThe Swift Den
Created by hippietrail on 9/15/2022 in #swift-development
How to pass parameter to custom iterator
I'm making a custom iterator that returns every slice of a given width through a collection. I got it working with a hardcoded width but can't figure out how to make it a parameter. I want the hard-coded 3 to be replaced with that param:
extension Array {

func windows() -> AnySequence<ArraySlice<Element>> {
return AnySequence({ () -> AnyIterator<ArraySlice<Element>> in
var index = self.startIndex

return AnyIterator({
if index > self.count - 3 {
return nil
}

let result = self[index..<index + 3]

self.formIndex(after: &index)

return result
})
})
}

}

let seq = [1,11,7,444]
for val in seq.windows() {
print(val)
}
extension Array {

func windows() -> AnySequence<ArraySlice<Element>> {
return AnySequence({ () -> AnyIterator<ArraySlice<Element>> in
var index = self.startIndex

return AnyIterator({
if index > self.count - 3 {
return nil
}

let result = self[index..<index + 3]

self.formIndex(after: &index)

return result
})
})
}

}

let seq = [1,11,7,444]
for val in seq.windows() {
print(val)
}
2 replies