Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaototi committed Nov 8, 2023
1 parent a5b96ba commit 99f2aa4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ require (
require (
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/sys v0.1.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzG
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
Expand Down
26 changes: 17 additions & 9 deletions lib/cmds/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"time"
"unicode/utf8"

_ "golang.org/x/image/webp"

"github.com/bwmarrin/discordgo"
"github.com/tpc3/Bocchi-Go/lib/chat"
"github.com/tpc3/Bocchi-Go/lib/config"
Expand Down Expand Up @@ -95,11 +97,6 @@ func ChatCmd(session *discordgo.Session, orgMsg *discordgo.MessageCreate, guild
return
}

if !strings.Contains(imgurl, ".png") && !strings.Contains(imgurl, ".jpg") && !strings.Contains(imgurl, ".jpeg") && !strings.Contains(imgurl, ".gif") && !strings.Contains(imgurl, ".webp") {
ErrorReply(session, orgMsg, config.Lang[config.CurrentConfig.Guild.Lang].Error.NoSupportimage)
return
}

resp, err := http.Get(imgurl)
if err != nil {
if strings.Contains(err.Error(), "no such host") {
Expand All @@ -113,6 +110,21 @@ func ChatCmd(session *discordgo.Session, orgMsg *discordgo.MessageCreate, guild
ErrorReply(session, orgMsg, config.Lang[config.CurrentConfig.Guild.Lang].Error.BrokenLink)
return
}
imageConfig, imageType, err := image.DecodeConfig(resp.Body)
if err != nil {
if strings.Contains(err.Error(), "unknown format") {
ErrorReply(session, orgMsg, config.Lang[config.CurrentConfig.Guild.Lang].Error.NoSupportimage)
return
}
log.Panic("Faild to decode: ", err)
}

if imageType != "png" && imageType != "jpeg" && imageType != "webp" && imageType != "gif" {
ErrorReply(session, orgMsg, config.Lang[config.CurrentConfig.Guild.Lang].Error.NoSupportimage)
return
}

log.Print(imageType)

model = "gpt-4-vision-preview"

Expand Down Expand Up @@ -142,10 +154,6 @@ func ChatCmd(session *discordgo.Session, orgMsg *discordgo.MessageCreate, guild
if detail == "low" {
detcost = 85
} else if detail == "high" {
imageConfig, _, err := image.DecodeConfig(resp.Body)
if err != nil {
log.Panic("Faild to decode: ", err)
}

width := imageConfig.Width
height := imageConfig.Height
Expand Down
2 changes: 1 addition & 1 deletion lib/config/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func loadLang() {
LongResponse: "AIの生成した文章が長すぎました。指示を変更してもう一度お試しください。",
TimeOut: "要求がタイムアウトしました。もう一度お試しください。",
CantReply: "エラーへの返信はできません。",
NoDetail: "highかlowのみ選択できます。",
NoDetail: "`-d`の値はhighかlowのみです。。",
NoImage: "画像が入力されていません。",
NoSupportimage: "その画像形式は対応していません。",
NoUrl: "URLを入力してください。",
Expand Down

0 comments on commit 99f2aa4

Please sign in to comment.