From ff22e5570725101b4380cd6303f41a446af70ce8 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Thu, 16 Apr 2020 15:34:34 +0900 Subject: [PATCH 01/83] =?UTF-8?q?!name=E3=81=A8!hello=E3=81=AE=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/message/command.go | 7 +++++++ command/message/message.go | 38 +++++++++++++++++++++++++++++++++++++ command/message/sentense.go | 6 ++++++ 3 files changed, 51 insertions(+) create mode 100644 command/message/command.go create mode 100644 command/message/message.go create mode 100644 command/message/sentense.go diff --git a/command/message/command.go b/command/message/command.go new file mode 100644 index 0000000..d061db9 --- /dev/null +++ b/command/message/command.go @@ -0,0 +1,7 @@ +package message + +var( + hello = "!hello" + usage = "!usage" + callName = "!name" +) diff --git a/command/message/message.go b/command/message/message.go new file mode 100644 index 0000000..1e4add9 --- /dev/null +++ b/command/message/message.go @@ -0,0 +1,38 @@ +package message + +import ( + "fmt" + "github.com/bwmarrin/discordgo" + "log" + "strings" +) + +func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + c, err := s.State.Channel(m.ChannelID) + if err != nil { + log.Println("Error getting channel: ", err) + return + } + + switch { + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", hello)): { + sendMessage(s, c, helloWorld) + } + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", usage)): { + sendMessage(s, c, usageSentence) + } + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", callName)): { + sendMessage(s, c, m.Member.Nick) + } + } +} + + +func sendMessage(s *discordgo.Session, c *discordgo.Channel, msg string) { + _, err := s.ChannelMessageSend(c.ID, msg) + + log.Println(">>> " + msg) + if err != nil { + log.Println("Error sending message: ", err) + } +} diff --git a/command/message/sentense.go b/command/message/sentense.go new file mode 100644 index 0000000..9799e76 --- /dev/null +++ b/command/message/sentense.go @@ -0,0 +1,6 @@ +package message + +var ( + helloWorld = "Hello, world!" + usageSentence = "試作段階のBotです。現在実装されているコマンドは、\n!hello: 挨拶をします。\n!name: 発言者の名前を表示します。" +) From 6edd35a0a048c5cb85ad8195aa047f810e890b97 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Thu, 16 Apr 2020 22:22:14 +0900 Subject: [PATCH 02/83] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E6=95=B4=E7=90=86=E3=80=81(WIP)!unko=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=E8=A9=A6=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/{message => }/command.go | 3 ++- command/{message => }/sentense.go | 2 +- command/{message/message.go => tools.go} | 6 ++++- main.go | 32 +++--------------------- 4 files changed, 12 insertions(+), 31 deletions(-) rename command/{message => }/command.go (66%) rename command/{message => }/sentense.go (92%) rename command/{message/message.go => tools.go} (81%) diff --git a/command/message/command.go b/command/command.go similarity index 66% rename from command/message/command.go rename to command/command.go index d061db9..7149de9 100644 --- a/command/message/command.go +++ b/command/command.go @@ -1,7 +1,8 @@ -package message +package command var( hello = "!hello" usage = "!usage" callName = "!name" + unko = "!unko" ) diff --git a/command/message/sentense.go b/command/sentense.go similarity index 92% rename from command/message/sentense.go rename to command/sentense.go index 9799e76..a578806 100644 --- a/command/message/sentense.go +++ b/command/sentense.go @@ -1,4 +1,4 @@ -package message +package command var ( helloWorld = "Hello, world!" diff --git a/command/message/message.go b/command/tools.go similarity index 81% rename from command/message/message.go rename to command/tools.go index 1e4add9..4b1e738 100644 --- a/command/message/message.go +++ b/command/tools.go @@ -1,9 +1,10 @@ -package message +package command import ( "fmt" "github.com/bwmarrin/discordgo" "log" + "change-status-go/secret" "strings" ) @@ -24,6 +25,9 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { case strings.HasPrefix(m.Content, fmt.Sprintf("%s", callName)): { sendMessage(s, c, m.Member.Nick) } + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", unko)): { + s.GuildMemberNickname(secret.GuildID,m.Message.Member.User.ID,":poop:") + } } } diff --git a/main.go b/main.go index 2d460cd..f3dc325 100644 --- a/main.go +++ b/main.go @@ -1,27 +1,24 @@ package main import ( + "change-status-go/command" + "change-status-go/secret" "fmt" "github.com/bwmarrin/discordgo" - "log" - "strings" ) var ( - Token = "Bot Njk5OTM0MTQ5NzI0NzMzNTIw.XpcOmQ.4laodM1AugWukek0aPpl-glBfEU" - BotName = "699934149724733520" StopBot = make(chan bool) - Hello = "!hello" ) func main() { var discord, err = discordgo.New() - discord.Token = Token + discord.Token = secret.Token if err != nil { fmt.Println("Error logged in") fmt.Println(err) } - discord.AddHandler(onMessageCreate) + discord.AddHandler(command.OnMessageCreate) err = discord.Open() if err != nil { @@ -32,24 +29,3 @@ func main() { <-StopBot return } - -func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { - c, err := s.State.Channel(m.ChannelID) - if err != nil { - log.Println("Error getting channel: ", err) - return - } - - if strings.HasPrefix(m.Content, fmt.Sprintf("%s", Hello)) { - sendMessage(s, c, "Hello world!") - } -} - -func sendMessage(s *discordgo.Session, c *discordgo.Channel, msg string) { - _, err := s.ChannelMessageSend(c.ID, msg) - - log.Println(">>> " + msg) - if err != nil { - log.Println("Error sending message: ", err) - } -} From 3e22941917ccd5899232a312809810a29d1bdfb5 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Fri, 17 Apr 2020 18:27:15 +0900 Subject: [PATCH 03/83] =?UTF-8?q?!id=E3=81=AE=E8=BF=BD=E5=8A=A0=E3=81=A8?= =?UTF-8?q?=E8=8B=A5=E5=B9=B2=E3=81=AE=E3=82=B3=E3=83=BC=E3=83=89=E6=88=90?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 多分あってるはず…… --- command/command.go | 1 + command/sentense.go | 2 +- command/tools.go | 15 +++++++++------ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/command/command.go b/command/command.go index 7149de9..b12fe1b 100644 --- a/command/command.go +++ b/command/command.go @@ -5,4 +5,5 @@ var( usage = "!usage" callName = "!name" unko = "!unko" + uid = "!id" ) diff --git a/command/sentense.go b/command/sentense.go index a578806..07084ae 100644 --- a/command/sentense.go +++ b/command/sentense.go @@ -2,5 +2,5 @@ package command var ( helloWorld = "Hello, world!" - usageSentence = "試作段階のBotです。現在実装されているコマンドは、\n!hello: 挨拶をします。\n!name: 発言者の名前を表示します。" + usageSentence = "試作段階のBotです。現在実装されているコマンドは、\n!hello: 挨拶をします。\n!name: 発言者の名前を表示します。\n!id: 発言者のIDを表示します。\n!unko: (WIP)発言者の表示名を:poop:にします。現在開発中なので正常に動作しません。" ) diff --git a/command/tools.go b/command/tools.go index 4b1e738..ef889f4 100644 --- a/command/tools.go +++ b/command/tools.go @@ -16,15 +16,18 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { } switch { - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", hello)): { + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", hello)): sendMessage(s, c, helloWorld) - } - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", usage)): { + + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", usage)): sendMessage(s, c, usageSentence) - } - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", callName)): { + + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", callName)): sendMessage(s, c, m.Member.Nick) - } + + case strings.HasPrefix(m.Content, fmt.Sprintf(uid)): + sendMessage(s, c, m.Author.ID) + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", unko)): { s.GuildMemberNickname(secret.GuildID,m.Message.Member.User.ID,":poop:") } From d2fa4c83fb28389b7cd8893aa11d287138e0033c Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Fri, 17 Apr 2020 22:47:53 +0900 Subject: [PATCH 04/83] =?UTF-8?q?!unko=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/command/tools.go b/command/tools.go index ef889f4..74e36f1 100644 --- a/command/tools.go +++ b/command/tools.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/bwmarrin/discordgo" "log" - "change-status-go/secret" "strings" ) @@ -29,7 +28,15 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { sendMessage(s, c, m.Author.ID) case strings.HasPrefix(m.Content, fmt.Sprintf("%s", unko)): { - s.GuildMemberNickname(secret.GuildID,m.Message.Member.User.ID,":poop:") + Err := s.GuildMemberNickname(m.GuildID, m.Author.ID,"💩") + if fmt.Sprint(Err) == "403 Forbidden" { + sendMessage(s, c, "権限がないので変更できません。落ちぶれましょう。") + } + if Err != nil { + fmt.Println(Err) + sendMessage(s, c, "あなたのコードが間違っています") + } + sendMessage(s, c, "今から私は" + m.Member.Nick + "です。") } } } From e36227359799790b230f7a5551504776b713a9c2 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 00:41:21 +0900 Subject: [PATCH 05/83] =?UTF-8?q?!status=20&=20!reset=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/command.go | 3 ++- command/tools.go | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/command/command.go b/command/command.go index b12fe1b..b3a2ad1 100644 --- a/command/command.go +++ b/command/command.go @@ -4,6 +4,7 @@ var( hello = "!hello" usage = "!usage" callName = "!name" - unko = "!unko" + status = "!status" + reset = "!reset" uid = "!id" ) diff --git a/command/tools.go b/command/tools.go index 74e36f1..5fd0c9c 100644 --- a/command/tools.go +++ b/command/tools.go @@ -21,22 +21,42 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { case strings.HasPrefix(m.Content, fmt.Sprintf("%s", usage)): sendMessage(s, c, usageSentence) - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", callName)): - sendMessage(s, c, m.Member.Nick) + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", callName)): { + if m.Member.Nick != "" { + sendMessage(s, c, m.Member.Nick) + } else { + sendMessage(s, c, m.Author.Username) + } + + } case strings.HasPrefix(m.Content, fmt.Sprintf(uid)): sendMessage(s, c, m.Author.ID) - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", unko)): { - Err := s.GuildMemberNickname(m.GuildID, m.Author.ID,"💩") - if fmt.Sprint(Err) == "403 Forbidden" { - sendMessage(s, c, "権限がないので変更できません。落ちぶれましょう。") + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", status)): { + if strings.Split(m.Content, " ")[0] == "!status" { + Emoji := m.Content[len("!status"):] + Err := s.GuildMemberNickname(m.GuildID, m.Author.ID,m.Author.Username + Emoji) + if fmt.Sprint(Err) == `HTTP 403 Forbidden, {"message": "Missing Permissions", "code": 50013}` { + sendMessage(s, c, forbidden) + } + if Err != nil { + fmt.Println(Err) + sendMessage(s, c, wrong) + } + sendMessage(s, c, Emoji + "ですね。" + m.Author.Username + "、行ってらっしゃい。") + } else { + sendMessage(s, c, advise) } + + } + case strings.HasPrefix(m.Content, fmt.Sprintf("%s", reset)):{ + Err := s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username) if Err != nil { fmt.Println(Err) - sendMessage(s, c, "あなたのコードが間違っています") + sendMessage(s, c, wrong) } - sendMessage(s, c, "今から私は" + m.Member.Nick + "です。") + sendMessage(s, c, notify + "、"+ m.Author.Username) } } } From c23b2e0471c7857f25666f2fa016db902bcfc1fc Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 00:41:37 +0900 Subject: [PATCH 06/83] =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/sentense.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/command/sentense.go b/command/sentense.go index 07084ae..0681659 100644 --- a/command/sentense.go +++ b/command/sentense.go @@ -1,6 +1,10 @@ package command var ( + advise = "usage: !status [emoji]" + forbidden = "権限がないので変更できません。落ちぶれましょう。" helloWorld = "Hello, world!" - usageSentence = "試作段階のBotです。現在実装されているコマンドは、\n!hello: 挨拶をします。\n!name: 発言者の名前を表示します。\n!id: 発言者のIDを表示します。\n!unko: (WIP)発言者の表示名を:poop:にします。現在開発中なので正常に動作しません。" + notify = "名前を元に戻しました" + usageSentence = "試作段階のBotです。現在実装されているコマンドは、\n!hello: 挨拶をします。\n!name: 発言者の名前を表示します。\n!id: 発言者のIDを表示します。\n!status [Emoji]: 表示名を絵文字にします。\n!reset: 名前をユーザーネームに戻します。" + wrong = "あなたのコードが間違っています" ) From 3f05a5369f94b09ab9a2149218a0db1d586d447a Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 05:44:46 +0900 Subject: [PATCH 07/83] =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92?= =?UTF-8?q?=E3=81=8D=E3=82=8C=E3=81=84=E3=81=AB=E3=81=97=E3=81=BE=E3=81=97?= =?UTF-8?q?=E3=82=87=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Siwtch-caseの正しい使用、ガード節の実装など --- command/tools.go | 50 +++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/command/tools.go b/command/tools.go index 5fd0c9c..52898c8 100644 --- a/command/tools.go +++ b/command/tools.go @@ -13,50 +13,44 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { log.Println("Error getting channel: ", err) return } - - switch { - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", hello)): + commandName := strings.Split(m.Content, " ")[0][1:] + switch commandName { + case hello: sendMessage(s, c, helloWorld) - - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", usage)): + case usage: sendMessage(s, c, usageSentence) - - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", callName)): { + case callName: { if m.Member.Nick != "" { sendMessage(s, c, m.Member.Nick) } else { sendMessage(s, c, m.Author.Username) } - } - - case strings.HasPrefix(m.Content, fmt.Sprintf(uid)): + case uid: sendMessage(s, c, m.Author.ID) - - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", status)): { - if strings.Split(m.Content, " ")[0] == "!status" { - Emoji := m.Content[len("!status"):] - Err := s.GuildMemberNickname(m.GuildID, m.Author.ID,m.Author.Username + Emoji) - if fmt.Sprint(Err) == `HTTP 403 Forbidden, {"message": "Missing Permissions", "code": 50013}` { - sendMessage(s, c, forbidden) - } - if Err != nil { - fmt.Println(Err) - sendMessage(s, c, wrong) - } - sendMessage(s, c, Emoji + "ですね。" + m.Author.Username + "、行ってらっしゃい。") - } else { - sendMessage(s, c, advise) + case status: { + Emoji := m.Content[len(status):] + Err :=s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username + Emoji) + if fmt.Sprint(Err) == `HTTP 403 Forbidden, {"message": "Missing Permissions", "code": 50013}` { + sendMessage(s, c, forbidden) + return + } + if Err != nil { + fmt.Println(Err) + sendMessage(s, c, wrong) + return } + sendMessage(s, c, Emoji + "ですね。" + m.Author.Username + "、行ってらっしゃい。") } - case strings.HasPrefix(m.Content, fmt.Sprintf("%s", reset)):{ - Err := s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username) + case reset: { + Err := s.GuildNickname(m.guildID, m.Author.ID, m.Author.Username) if Err != nil { fmt.Println(Err) sendMessage(s, c, wrong) + return } - sendMessage(s, c, notify + "、"+ m.Author.Username) + sendMessage(s, c, notify + "、" + m.Author.Username) } } } From 17e8c02f51ebb437a116eb7de2f082793def78f5 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 05:47:32 +0900 Subject: [PATCH 08/83] fix typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Typoすんな --- command/tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/tools.go b/command/tools.go index 52898c8..cfba223 100644 --- a/command/tools.go +++ b/command/tools.go @@ -44,7 +44,7 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { } case reset: { - Err := s.GuildNickname(m.guildID, m.Author.ID, m.Author.Username) + Err := s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username) if Err != nil { fmt.Println(Err) sendMessage(s, c, wrong) From 41b8691e31d9a7641a637ca7fb10ad7ea118dbcd Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 05:53:01 +0900 Subject: [PATCH 09/83] =?UTF-8?q?!=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=81=84=E3=81=8C=E3=81=84=E7=84=A1=E8=A6=96=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/command.go | 12 ++++++------ command/tools.go | 3 +++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/command/command.go b/command/command.go index b3a2ad1..8bcc296 100644 --- a/command/command.go +++ b/command/command.go @@ -1,10 +1,10 @@ package command var( - hello = "!hello" - usage = "!usage" - callName = "!name" - status = "!status" - reset = "!reset" - uid = "!id" + hello = "hello" + usage = "usage" + callName = "name" + status = "status" + reset = "reset" + uid = "id" ) diff --git a/command/tools.go b/command/tools.go index cfba223..d320496 100644 --- a/command/tools.go +++ b/command/tools.go @@ -13,6 +13,9 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { log.Println("Error getting channel: ", err) return } + if !strings.HasPrefix(m.Content, "!") { + return + } commandName := strings.Split(m.Content, " ")[0][1:] switch commandName { case hello: From 77ca117e128d3ec4e74d348ea96157b3509b15cc Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 05:56:03 +0900 Subject: [PATCH 10/83] =?UTF-8?q?=E3=83=8F=E3=83=BC=E3=83=89=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=87=E3=82=A3=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/tools.go b/command/tools.go index d320496..6e7e08f 100644 --- a/command/tools.go +++ b/command/tools.go @@ -32,7 +32,7 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { case uid: sendMessage(s, c, m.Author.ID) case status: { - Emoji := m.Content[len(status):] + Emoji := m.Content[8:] Err :=s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username + Emoji) if fmt.Sprint(Err) == `HTTP 403 Forbidden, {"message": "Missing Permissions", "code": 50013}` { sendMessage(s, c, forbidden) From 9e4493c8acb2331ae1a9371aa6d5f7868a2ed67d Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 13:01:20 +0900 Subject: [PATCH 11/83] var -> const --- command/command.go | 2 +- command/sentense.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/command/command.go b/command/command.go index 8bcc296..4fbdc41 100644 --- a/command/command.go +++ b/command/command.go @@ -1,6 +1,6 @@ package command -var( +const ( hello = "hello" usage = "usage" callName = "name" diff --git a/command/sentense.go b/command/sentense.go index 0681659..caf12b7 100644 --- a/command/sentense.go +++ b/command/sentense.go @@ -1,6 +1,6 @@ package command -var ( +const ( advise = "usage: !status [emoji]" forbidden = "権限がないので変更できません。落ちぶれましょう。" helloWorld = "Hello, world!" From 49d134367159ad16d95d51186058b0ffe60137e5 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 14:05:46 +0900 Subject: [PATCH 12/83] =?UTF-8?q?sentence=E3=81=A8command=E3=81=AE?= =?UTF-8?q?=E5=88=86=E9=9B=A2=E3=80=81=E6=96=87=E7=AB=A0=E3=81=AE=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/sentense.go | 10 ---------- command/tools.go | 13 +++++++------ sentence/sentence.go | 10 ++++++++++ 3 files changed, 17 insertions(+), 16 deletions(-) delete mode 100644 command/sentense.go create mode 100644 sentence/sentence.go diff --git a/command/sentense.go b/command/sentense.go deleted file mode 100644 index caf12b7..0000000 --- a/command/sentense.go +++ /dev/null @@ -1,10 +0,0 @@ -package command - -const ( - advise = "usage: !status [emoji]" - forbidden = "権限がないので変更できません。落ちぶれましょう。" - helloWorld = "Hello, world!" - notify = "名前を元に戻しました" - usageSentence = "試作段階のBotです。現在実装されているコマンドは、\n!hello: 挨拶をします。\n!name: 発言者の名前を表示します。\n!id: 発言者のIDを表示します。\n!status [Emoji]: 表示名を絵文字にします。\n!reset: 名前をユーザーネームに戻します。" - wrong = "あなたのコードが間違っています" -) diff --git a/command/tools.go b/command/tools.go index 6e7e08f..8a2daf3 100644 --- a/command/tools.go +++ b/command/tools.go @@ -1,6 +1,7 @@ package command import ( + "change-status-go/sentence" "fmt" "github.com/bwmarrin/discordgo" "log" @@ -19,9 +20,9 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { commandName := strings.Split(m.Content, " ")[0][1:] switch commandName { case hello: - sendMessage(s, c, helloWorld) + sendMessage(s, c, sentence.Hello) case usage: - sendMessage(s, c, usageSentence) + sendMessage(s, c, sentence.Usage) case callName: { if m.Member.Nick != "" { sendMessage(s, c, m.Member.Nick) @@ -35,12 +36,12 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { Emoji := m.Content[8:] Err :=s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username + Emoji) if fmt.Sprint(Err) == `HTTP 403 Forbidden, {"message": "Missing Permissions", "code": 50013}` { - sendMessage(s, c, forbidden) + sendMessage(s, c, sentence.Forbidden) return } if Err != nil { fmt.Println(Err) - sendMessage(s, c, wrong) + sendMessage(s, c, sentence.Wrong) return } sendMessage(s, c, Emoji + "ですね。" + m.Author.Username + "、行ってらっしゃい。") @@ -50,10 +51,10 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { Err := s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username) if Err != nil { fmt.Println(Err) - sendMessage(s, c, wrong) + sendMessage(s, c, sentence.Wrong) return } - sendMessage(s, c, notify + "、" + m.Author.Username) + sendMessage(s, c, sentence.Notify + "、" + m.Author.Username) } } } diff --git a/sentence/sentence.go b/sentence/sentence.go new file mode 100644 index 0000000..7cfbfa8 --- /dev/null +++ b/sentence/sentence.go @@ -0,0 +1,10 @@ +package sentence + +const ( + Advise = "usage: !status [emoji]" + Forbidden = "権限がないので変更できません。落ちぶれましょう。" + Hello = "Hello, world!" + Notify = "名前を元に戻しました" + Usage = "試作段階のBotです。現在実装されているコマンドは、\n!hello: 挨拶をします。\n!name: 発言者の名前を表示します。\n!id: 発言者のIDを表示します。\n!status [Emoji]: 表示名を絵文字にします。\n!reset: 名前をユーザーネームに戻します。" + Wrong = "コードが間違っている可能性があります。開発者に問い合わせてください。" +) From 1dd51e4056c0bfd11e088e152184d8686d5799ea Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 14:11:50 +0900 Subject: [PATCH 13/83] =?UTF-8?q?=E6=8E=A5=E5=B0=BE=E8=BE=9E=E3=82=92?= =?UTF-8?q?=E5=AE=9A=E6=95=B0=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/command/tools.go b/command/tools.go index 8a2daf3..2968124 100644 --- a/command/tools.go +++ b/command/tools.go @@ -8,13 +8,14 @@ import ( "strings" ) +const prefix = "%" func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { c, err := s.State.Channel(m.ChannelID) if err != nil { log.Println("Error getting channel: ", err) return } - if !strings.HasPrefix(m.Content, "!") { + if !strings.HasPrefix(m.Content, prefix) { return } commandName := strings.Split(m.Content, " ")[0][1:] From 59153460386cc2ac56c73abf97a915d8434a86d4 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 14:12:16 +0900 Subject: [PATCH 14/83] =?UTF-8?q?"err"=E3=82=92"Err"=E3=81=AB=E7=B5=B1?= =?UTF-8?q?=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/command/tools.go b/command/tools.go index 2968124..9c19e63 100644 --- a/command/tools.go +++ b/command/tools.go @@ -9,10 +9,11 @@ import ( ) const prefix = "%" + func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { - c, err := s.State.Channel(m.ChannelID) - if err != nil { - log.Println("Error getting channel: ", err) + c, Err := s.State.Channel(m.ChannelID) + if Err != nil { + log.Println("Error getting channel: ", Err) return } if !strings.HasPrefix(m.Content, prefix) { @@ -62,10 +63,10 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func sendMessage(s *discordgo.Session, c *discordgo.Channel, msg string) { - _, err := s.ChannelMessageSend(c.ID, msg) + _, Err := s.ChannelMessageSend(c.ID, msg) log.Println(">>> " + msg) - if err != nil { - log.Println("Error sending message: ", err) + if Err != nil { + log.Println("Error sending message: ", Err) } } From 969139740c81f57717610a2663a726b1f3b528f4 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 14:17:13 +0900 Subject: [PATCH 15/83] =?UTF-8?q?Sprintf=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/tools.go b/command/tools.go index 9c19e63..8c9a013 100644 --- a/command/tools.go +++ b/command/tools.go @@ -46,7 +46,7 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { sendMessage(s, c, sentence.Wrong) return } - sendMessage(s, c, Emoji + "ですね。" + m.Author.Username + "、行ってらっしゃい。") + sendMessage(s, c, fmt.Sprintf("%sですね。%s、行ってらっしゃい。", Emoji, m.Member.Nick)) } case reset: { From 4fbb3ef8c731273f0b3caddc4fb48fb5dcfa7846 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 15:13:21 +0900 Subject: [PATCH 16/83] =?UTF-8?q?=E8=AA=AC=E6=98=8E=E6=96=87=E3=81=AE?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sentence/sentence.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentence/sentence.go b/sentence/sentence.go index 7cfbfa8..36f510b 100644 --- a/sentence/sentence.go +++ b/sentence/sentence.go @@ -5,6 +5,6 @@ const ( Forbidden = "権限がないので変更できません。落ちぶれましょう。" Hello = "Hello, world!" Notify = "名前を元に戻しました" - Usage = "試作段階のBotです。現在実装されているコマンドは、\n!hello: 挨拶をします。\n!name: 発言者の名前を表示します。\n!id: 発言者のIDを表示します。\n!status [Emoji]: 表示名を絵文字にします。\n!reset: 名前をユーザーネームに戻します。" + Usage = "試作段階のBotです。現在実装されているコマンドは、\n%hello: 挨拶をします。\n%name: 発言者の名前を表示します。\n%id: 発言者のIDを表示します。\n%status [Emoji]: 表示名を絵文字にします。\n%reset: 名前をユーザーネームに戻します。" Wrong = "コードが間違っている可能性があります。開発者に問い合わせてください。" ) From da7eb8e4447cb063af8c3e19be2f2f00053e8237 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 18:10:41 +0900 Subject: [PATCH 17/83] =?UTF-8?q?Error=E8=A9=95=E4=BE=A1=E3=81=AE=E6=A7=8B?= =?UTF-8?q?=E6=96=87=E3=82=92=E5=A4=89=E6=9B=B4=E3=80=81=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8=E3=81=AE?= =?UTF-8?q?=E5=AE=9A=E6=95=B0=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/command/tools.go b/command/tools.go index 8c9a013..d94009a 100644 --- a/command/tools.go +++ b/command/tools.go @@ -8,7 +8,9 @@ import ( "strings" ) -const prefix = "%" +const ( + errorForbidden = `HTTP 403 Forbidden, {"message": "Missing Permissions", "code": 50013}` +) func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { c, Err := s.State.Channel(m.ChannelID) @@ -37,16 +39,17 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { case status: { Emoji := m.Content[8:] Err :=s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username + Emoji) - if fmt.Sprint(Err) == `HTTP 403 Forbidden, {"message": "Missing Permissions", "code": 50013}` { - sendMessage(s, c, sentence.Forbidden) - return - } + if Err != nil { + if Err.Error() == errorForbidden { + sendMessage(s, c, sentence.Forbidden) + return + } if Err != nil { fmt.Println(Err) sendMessage(s, c, sentence.Wrong) return } - sendMessage(s, c, fmt.Sprintf("%sですね。%s、行ってらっしゃい。", Emoji, m.Member.Nick)) + sendMessage(s, c, fmt.Sprintf("%sですね。%s、行ってらっしゃい。", Emoji, m.Author.Username + Emoji)) } case reset: { From 5d00c81375e481f058c7a32500e8da5c5bb461c9 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 18:11:16 +0900 Subject: [PATCH 18/83] =?UTF-8?q?32=E6=96=87=E5=AD=97=E4=BB=A5=E4=B8=8A?= =?UTF-8?q?=E3=81=AE=E3=82=B9=E3=83=86=E3=83=BC=E3=82=BF=E3=82=B9=E3=82=92?= =?UTF-8?q?=E8=AD=A6=E5=91=8A=E3=81=99=E3=82=8B=E6=9D=A1=E4=BB=B6=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 7 ++++++- sentence/sentence.go | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/command/tools.go b/command/tools.go index d94009a..36d41c9 100644 --- a/command/tools.go +++ b/command/tools.go @@ -9,7 +9,9 @@ import ( ) const ( + errorBadRequest = `HTTP 400 Bad Request, {"nick": ["Must be 32 or fewer in length."]}` errorForbidden = `HTTP 403 Forbidden, {"message": "Missing Permissions", "code": 50013}` + prefix = "%" ) func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { @@ -44,7 +46,10 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { sendMessage(s, c, sentence.Forbidden) return } - if Err != nil { + if Err.Error() == errorBadRequest { + sendMessage(s, c, sentence.BadRequest) + return + } fmt.Println(Err) sendMessage(s, c, sentence.Wrong) return diff --git a/sentence/sentence.go b/sentence/sentence.go index 36f510b..1d39ca8 100644 --- a/sentence/sentence.go +++ b/sentence/sentence.go @@ -1,7 +1,7 @@ package sentence const ( - Advise = "usage: !status [emoji]" + BadRequest = "付けられるステータスは32文字以下である必要があります。" Forbidden = "権限がないので変更できません。落ちぶれましょう。" Hello = "Hello, world!" Notify = "名前を元に戻しました" From 35b95aa34450d5d8e449d38e3bf8a4fbf4a3d678 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 19:57:25 +0900 Subject: [PATCH 19/83] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA{}=E3=82=92?= =?UTF-8?q?=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/command/tools.go b/command/tools.go index 36d41c9..a42501a 100644 --- a/command/tools.go +++ b/command/tools.go @@ -29,16 +29,15 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { sendMessage(s, c, sentence.Hello) case usage: sendMessage(s, c, sentence.Usage) - case callName: { + case callName: if m.Member.Nick != "" { sendMessage(s, c, m.Member.Nick) } else { sendMessage(s, c, m.Author.Username) } - } case uid: sendMessage(s, c, m.Author.ID) - case status: { + case status: Emoji := m.Content[8:] Err :=s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username + Emoji) if Err != nil { @@ -56,8 +55,7 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { } sendMessage(s, c, fmt.Sprintf("%sですね。%s、行ってらっしゃい。", Emoji, m.Author.Username + Emoji)) - } - case reset: { + case reset: Err := s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username) if Err != nil { fmt.Println(Err) @@ -65,7 +63,6 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } sendMessage(s, c, sentence.Notify + "、" + m.Author.Username) - } } } From b129cc0457bb73af3c4810181b3845a8b9139946 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 18 Apr 2020 19:58:39 +0900 Subject: [PATCH 20/83] =?UTF-8?q?=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= =?UTF-8?q?=E8=AD=98=E5=88=A5=E3=81=AE=E6=8A=BD=E5=87=BA=E6=B3=95=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/tools.go b/command/tools.go index a42501a..b4722d9 100644 --- a/command/tools.go +++ b/command/tools.go @@ -23,7 +23,7 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if !strings.HasPrefix(m.Content, prefix) { return } - commandName := strings.Split(m.Content, " ")[0][1:] + commandName := strings.Split(m.Content, " ")[0][len(prefix):] switch commandName { case hello: sendMessage(s, c, sentence.Hello) From 2e7bb3ffeb2e2d3d8c0ce8eabecefb5de8acde91 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sun, 26 Apr 2020 18:04:27 +0900 Subject: [PATCH 21/83] =?UTF-8?q?.gitignore=E3=81=AE=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit vendorの非追跡の対応 --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2ca5a1f..12a6757 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.exe .idea secret +vendor From d9974ed88af7d794cd73764228fe072ae3831ba6 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sun, 26 Apr 2020 18:08:45 +0900 Subject: [PATCH 22/83] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 5 +++++ go.sum | 6 ++++++ 2 files changed, 11 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c5e2460 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module change-status-go + +go 1.14 + +require github.com/bwmarrin/discordgo v0.20.3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7123e3e --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/bwmarrin/discordgo v0.20.3 h1:AxjcHGbyBFSC0a3Zx5nDQwbOjU7xai5dXjRnZ0YB7nU= +github.com/bwmarrin/discordgo v0.20.3/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q= +github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= From 988aca2c222346fbaeb38ac3d53395bdd26da699 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sun, 26 Apr 2020 18:09:14 +0900 Subject: [PATCH 23/83] =?UTF-8?q?usage=E3=81=AE=E6=96=87=E7=AB=A0=E3=82=92?= =?UTF-8?q?=E8=A6=8B=E3=82=84=E3=81=99=E3=81=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sentence/sentence.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentence/sentence.go b/sentence/sentence.go index 1d39ca8..7f20dc2 100644 --- a/sentence/sentence.go +++ b/sentence/sentence.go @@ -5,6 +5,6 @@ const ( Forbidden = "権限がないので変更できません。落ちぶれましょう。" Hello = "Hello, world!" Notify = "名前を元に戻しました" - Usage = "試作段階のBotです。現在実装されているコマンドは、\n%hello: 挨拶をします。\n%name: 発言者の名前を表示します。\n%id: 発言者のIDを表示します。\n%status [Emoji]: 表示名を絵文字にします。\n%reset: 名前をユーザーネームに戻します。" + Usage = "```試作段階のBotです。現在実装されているコマンドは、\n%hello: 挨拶をします。\n%name: 発言者の名前を表示します。\n%id: 発言者のIDを表示します。\n%status [Emoji]: 表示名を絵文字にします。\n%reset: 名前をユーザーネームに戻します。```" Wrong = "コードが間違っている可能性があります。開発者に問い合わせてください。" ) From 40bb60f387a1cc59ffb7cbbfc62fbe639f711124 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sun, 26 Apr 2020 18:18:29 +0900 Subject: [PATCH 24/83] =?UTF-8?q?reset=E3=81=AE=E5=89=8A=E9=99=A4(?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E6=A9=9F=E8=83=BD=E3=81=B8=E3=81=AE=E9=81=A9?= =?UTF-8?q?=E7=94=A8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/tools.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/command/tools.go b/command/tools.go index b4722d9..9e042d0 100644 --- a/command/tools.go +++ b/command/tools.go @@ -54,15 +54,6 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } sendMessage(s, c, fmt.Sprintf("%sですね。%s、行ってらっしゃい。", Emoji, m.Author.Username + Emoji)) - - case reset: - Err := s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username) - if Err != nil { - fmt.Println(Err) - sendMessage(s, c, sentence.Wrong) - return - } - sendMessage(s, c, sentence.Notify + "、" + m.Author.Username) } } From 743b8a362a5a4741e9afaee447c39885878fedcb Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Tue, 28 Apr 2020 07:52:37 +0900 Subject: [PATCH 25/83] =?UTF-8?q?rm=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ただしバグをはらんでいる --- Users/user.go | 1 + command/tools.go | 37 +++++++++++++++++++++++-------------- sentence/sentence.go | 11 +++++++++-- 3 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 Users/user.go diff --git a/Users/user.go b/Users/user.go new file mode 100644 index 0000000..846915c --- /dev/null +++ b/Users/user.go @@ -0,0 +1 @@ +package Users diff --git a/command/tools.go b/command/tools.go index 9e042d0..41c2ad3 100644 --- a/command/tools.go +++ b/command/tools.go @@ -25,21 +25,18 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { } commandName := strings.Split(m.Content, " ")[0][len(prefix):] switch commandName { - case hello: - sendMessage(s, c, sentence.Hello) - case usage: - sendMessage(s, c, sentence.Usage) - case callName: - if m.Member.Nick != "" { - sendMessage(s, c, m.Member.Nick) - } else { - sendMessage(s, c, m.Author.Username) - } - case uid: - sendMessage(s, c, m.Author.ID) case status: - Emoji := m.Content[8:] - Err :=s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Author.Username + Emoji) + operator := strings.Split(m.Content, " ")[1] + Emoji := strings.Split(m.Content, " ")[2] + + if operator == "add" { + Err = s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Member.Nick + Emoji) + } else if operator == "rm" { + if !strings.Contains(m.Member.Nick, Emoji) { + return + } + Err = s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Member.Nick[:(len(m.Member.Nick) - len(Emoji))]) + } if Err != nil { if Err.Error() == errorForbidden { sendMessage(s, c, sentence.Forbidden) @@ -54,6 +51,18 @@ func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } sendMessage(s, c, fmt.Sprintf("%sですね。%s、行ってらっしゃい。", Emoji, m.Author.Username + Emoji)) + case hello: + sendMessage(s, c, sentence.Hello) + case usage: + sendMessage(s, c, sentence.Usage) + case callName: + if m.Member.Nick != "" { + sendMessage(s, c, m.Member.Nick) + } else { + sendMessage(s, c, m.Author.Username) + } + case uid: + sendMessage(s, c, m.Author.ID) } } diff --git a/sentence/sentence.go b/sentence/sentence.go index 7f20dc2..74dd967 100644 --- a/sentence/sentence.go +++ b/sentence/sentence.go @@ -2,9 +2,16 @@ package sentence const ( BadRequest = "付けられるステータスは32文字以下である必要があります。" - Forbidden = "権限がないので変更できません。落ちぶれましょう。" + Forbidden = "Botに権限がないので変更できません。落ちぶれましょう。" Hello = "Hello, world!" Notify = "名前を元に戻しました" - Usage = "```試作段階のBotです。現在実装されているコマンドは、\n%hello: 挨拶をします。\n%name: 発言者の名前を表示します。\n%id: 発言者のIDを表示します。\n%status [Emoji]: 表示名を絵文字にします。\n%reset: 名前をユーザーネームに戻します。```" + Usage = ` +試作段階のBotです。現在実装されているコマンドは、 +%hello: 挨拶をします。 +%name: 発言者の名前を表示します。 +%id: 発言者のIDを表示します。 +%status [Emoji]: 表示名を絵文字にします。 +%reset: 名前をユーザーネームに戻します。 + ` Wrong = "コードが間違っている可能性があります。開発者に問い合わせてください。" ) From 0e81731c71e8f04de83d1a0f52981d51ddf45267 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Tue, 28 Apr 2020 08:48:24 +0900 Subject: [PATCH 26/83] =?UTF-8?q?=E5=86=8D=E8=A8=AD=E8=A8=88=E3=81=AE?= =?UTF-8?q?=E3=81=9F=E3=82=81=E7=8F=BE=E8=A1=8C=E3=83=95=E3=82=A1=E3=82=A4?= =?UTF-8?q?=E3=83=AB=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Users/user.go | 1 - command/command.go | 10 ------ command/tools.go | 77 -------------------------------------------- main.go | 31 ------------------ sentence/sentence.go | 17 ---------- 5 files changed, 136 deletions(-) delete mode 100644 Users/user.go delete mode 100644 command/command.go delete mode 100644 command/tools.go delete mode 100644 main.go delete mode 100644 sentence/sentence.go diff --git a/Users/user.go b/Users/user.go deleted file mode 100644 index 846915c..0000000 --- a/Users/user.go +++ /dev/null @@ -1 +0,0 @@ -package Users diff --git a/command/command.go b/command/command.go deleted file mode 100644 index 4fbdc41..0000000 --- a/command/command.go +++ /dev/null @@ -1,10 +0,0 @@ -package command - -const ( - hello = "hello" - usage = "usage" - callName = "name" - status = "status" - reset = "reset" - uid = "id" -) diff --git a/command/tools.go b/command/tools.go deleted file mode 100644 index 41c2ad3..0000000 --- a/command/tools.go +++ /dev/null @@ -1,77 +0,0 @@ -package command - -import ( - "change-status-go/sentence" - "fmt" - "github.com/bwmarrin/discordgo" - "log" - "strings" -) - -const ( - errorBadRequest = `HTTP 400 Bad Request, {"nick": ["Must be 32 or fewer in length."]}` - errorForbidden = `HTTP 403 Forbidden, {"message": "Missing Permissions", "code": 50013}` - prefix = "%" -) - -func OnMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { - c, Err := s.State.Channel(m.ChannelID) - if Err != nil { - log.Println("Error getting channel: ", Err) - return - } - if !strings.HasPrefix(m.Content, prefix) { - return - } - commandName := strings.Split(m.Content, " ")[0][len(prefix):] - switch commandName { - case status: - operator := strings.Split(m.Content, " ")[1] - Emoji := strings.Split(m.Content, " ")[2] - - if operator == "add" { - Err = s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Member.Nick + Emoji) - } else if operator == "rm" { - if !strings.Contains(m.Member.Nick, Emoji) { - return - } - Err = s.GuildMemberNickname(m.GuildID, m.Author.ID, m.Member.Nick[:(len(m.Member.Nick) - len(Emoji))]) - } - if Err != nil { - if Err.Error() == errorForbidden { - sendMessage(s, c, sentence.Forbidden) - return - } - if Err.Error() == errorBadRequest { - sendMessage(s, c, sentence.BadRequest) - return - } - fmt.Println(Err) - sendMessage(s, c, sentence.Wrong) - return - } - sendMessage(s, c, fmt.Sprintf("%sですね。%s、行ってらっしゃい。", Emoji, m.Author.Username + Emoji)) - case hello: - sendMessage(s, c, sentence.Hello) - case usage: - sendMessage(s, c, sentence.Usage) - case callName: - if m.Member.Nick != "" { - sendMessage(s, c, m.Member.Nick) - } else { - sendMessage(s, c, m.Author.Username) - } - case uid: - sendMessage(s, c, m.Author.ID) - } -} - - -func sendMessage(s *discordgo.Session, c *discordgo.Channel, msg string) { - _, Err := s.ChannelMessageSend(c.ID, msg) - - log.Println(">>> " + msg) - if Err != nil { - log.Println("Error sending message: ", Err) - } -} diff --git a/main.go b/main.go deleted file mode 100644 index f3dc325..0000000 --- a/main.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "change-status-go/command" - "change-status-go/secret" - "fmt" - "github.com/bwmarrin/discordgo" -) - -var ( - StopBot = make(chan bool) -) -func main() { - var discord, err = discordgo.New() - discord.Token = secret.Token - if err != nil { - fmt.Println("Error logged in") - fmt.Println(err) - } - - discord.AddHandler(command.OnMessageCreate) - - err = discord.Open() - if err != nil { - fmt.Println(err) - } - - fmt.Println("Listening...") - <-StopBot - return -} diff --git a/sentence/sentence.go b/sentence/sentence.go deleted file mode 100644 index 74dd967..0000000 --- a/sentence/sentence.go +++ /dev/null @@ -1,17 +0,0 @@ -package sentence - -const ( - BadRequest = "付けられるステータスは32文字以下である必要があります。" - Forbidden = "Botに権限がないので変更できません。落ちぶれましょう。" - Hello = "Hello, world!" - Notify = "名前を元に戻しました" - Usage = ` -試作段階のBotです。現在実装されているコマンドは、 -%hello: 挨拶をします。 -%name: 発言者の名前を表示します。 -%id: 発言者のIDを表示します。 -%status [Emoji]: 表示名を絵文字にします。 -%reset: 名前をユーザーネームに戻します。 - ` - Wrong = "コードが間違っている可能性があります。開発者に問い合わせてください。" -) From d413495dd7386ee79115c05efd467dd0fc3a4610 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Tue, 28 Apr 2020 09:15:47 +0900 Subject: [PATCH 27/83] =?UTF-8?q?main.go=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 変数名の変更、終了処理を最新の処理に置き換え --- main.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..0045a09 --- /dev/null +++ b/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "change-status-go/secret" + "fmt" + "github.com/bwmarrin/discordgo" + "os" + "os/signal" + "syscall" +) + +func main() { + discordBrain, err := discordgo.New() + discordBrain.Token = secret.Token + if err != nil { + fmt.Println("Error logging in") + fmt.Println(err) + } + + discordBrain.AddHandler(messageCreate) + + err = discordBrain.Open() + if err != nil { + fmt.Println(err) + } + + fmt.Println("Bot起動完了、命令待機中") + sc := make(chan os.Signal,1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <- sc + return +} From 14d5e8f2cc633e5da005b176cba0a47fcfa4586c Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Tue, 28 Apr 2020 09:22:27 +0900 Subject: [PATCH 28/83] ping->Pong! --- main.go | 3 ++- src/tools.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/tools.go diff --git a/main.go b/main.go index 0045a09..f7ea3a0 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "change-status-go/secret" + command "change-status-go/src" "fmt" "github.com/bwmarrin/discordgo" "os" @@ -17,7 +18,7 @@ func main() { fmt.Println(err) } - discordBrain.AddHandler(messageCreate) + discordBrain.AddHandler(command.MessageCreate) err = discordBrain.Open() if err != nil { diff --git a/src/tools.go b/src/tools.go new file mode 100644 index 0000000..549c214 --- /dev/null +++ b/src/tools.go @@ -0,0 +1,15 @@ +package src + +import "github.com/bwmarrin/discordgo" + +func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + //Ping -> Pong + if m.Author.ID == s.State.User.ID { + return + } + + if m.Content == "ping" { + s.ChannelMessageSend(m.ChannelID, "Pong!") + } + +} From a5305d05e084f59c4d786d4f8a1afe39cdd002c8 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Tue, 28 Apr 2020 14:05:42 +0900 Subject: [PATCH 29/83] =?UTF-8?q?[refactor]=20=E3=83=A1=E3=83=83=E3=82=BB?= =?UTF-8?q?=E3=83=BC=E3=82=B8=E9=80=81=E4=BF=A1=E9=83=A8=E5=88=86=E3=81=AE?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit できるだけ同一関数を記述するのを回避して、可読性を上げてみた(ただし上がっているかは不明) 例外処理などはまだ未実装 追記:該当するコマンドがないと誘導するようになりました。 --- src/fecthMessage.go | 13 +++++++++++++ src/message.go | 17 +++++++++++++++++ src/tools.go | 22 +++++++++++++++++++--- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/fecthMessage.go create mode 100644 src/message.go diff --git a/src/fecthMessage.go b/src/fecthMessage.go new file mode 100644 index 0000000..db857c0 --- /dev/null +++ b/src/fecthMessage.go @@ -0,0 +1,13 @@ +package src + +func fetchMessage (cmd string) string{ + switch cmd { + case "help": + return helpMessage + + case "ping": + return "Pong!" + default: + return "該当するコマンドがありません`%help`を参照してください。" + } +} diff --git a/src/message.go b/src/message.go new file mode 100644 index 0000000..f41d1eb --- /dev/null +++ b/src/message.go @@ -0,0 +1,17 @@ +package src + +var ( + helpMessage = "```" + +`=Title= + something +=Description= + 表示名に絵文字を付けることでステータスを表示するBotの予定 +=Command= + (Prefix: "%") + - help: このBotの概要を説明します。 + - ping: Pong!と返します。かわいいですね。 +=Source= + github.com/brokenManager/change-status-go +    ` + "```" +) + diff --git a/src/tools.go b/src/tools.go index 549c214..ec66b0c 100644 --- a/src/tools.go +++ b/src/tools.go @@ -1,15 +1,31 @@ package src -import "github.com/bwmarrin/discordgo" +import ( + "fmt" + "github.com/bwmarrin/discordgo" + "strings" +) + +var ( + prefix = "%" +) func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //Ping -> Pong if m.Author.ID == s.State.User.ID { return } + if !strings.HasPrefix(m.Content, prefix) { + return + } - if m.Content == "ping" { - s.ChannelMessageSend(m.ChannelID, "Pong!") + var cmd = strings.Split(m.Content, " ")[0][len(prefix):] + //temp := strings.Split(m.Content, " ")[1] + Err, _ := s.ChannelMessageSend(m.ChannelID, fetchMessage(cmd)) + + if Err != nil { + fmt.Println("something wrong:", Err) + return } } From d772a8849ac26ca9757013f3e332a5e64d8b2055 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Tue, 28 Apr 2020 14:45:16 +0900 Subject: [PATCH 30/83] =?UTF-8?q?[refactor]=20=E7=8A=B6=E6=85=8B=E5=A4=89?= =?UTF-8?q?=E5=8C=96=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=E6=BA=96=E5=82=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fecthMessage.go | 3 ++- src/message.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/fecthMessage.go b/src/fecthMessage.go index db857c0..d7bcf24 100644 --- a/src/fecthMessage.go +++ b/src/fecthMessage.go @@ -2,9 +2,10 @@ package src func fetchMessage (cmd string) string{ switch cmd { + case "status": + return updateMessage case "help": return helpMessage - case "ping": return "Pong!" default: diff --git a/src/message.go b/src/message.go index f41d1eb..8f6c86a 100644 --- a/src/message.go +++ b/src/message.go @@ -12,6 +12,7 @@ var ( - ping: Pong!と返します。かわいいですね。 =Source= github.com/brokenManager/change-status-go -    ` + "```" +    ` + "```\n" + updateMessage = "ステータスを変更しました。" ) From cd86d58f9433f2ec220f1c8745d2d57afbdf776d Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Fri, 1 May 2020 23:06:50 +0900 Subject: [PATCH 31/83] =?UTF-8?q?[refactor]=E3=81=A8=E3=82=8A=E3=81=82?= =?UTF-8?q?=E3=81=88=E3=81=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 関数が多すぎるのでこれからまとめるためにいったんコミットで整理 --- main.go | 2 ++ src/BootNotify.go | 14 ++++++++++++++ src/fecthMessage.go | 2 -- src/nilCheck.go | 13 +++++++++++++ src/tools.go | 16 ++++------------ 5 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 src/BootNotify.go create mode 100644 src/nilCheck.go diff --git a/main.go b/main.go index f7ea3a0..13d0a93 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ func main() { fmt.Println(err) } + discordBrain.AddHandler(command.MessageCreate) err = discordBrain.Open() @@ -26,6 +27,7 @@ func main() { } fmt.Println("Bot起動完了、命令待機中") + discordBrain.AddHandlerOnce(command.BootNotify) sc := make(chan os.Signal,1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) <- sc diff --git a/src/BootNotify.go b/src/BootNotify.go new file mode 100644 index 0000000..a9df2f4 --- /dev/null +++ b/src/BootNotify.go @@ -0,0 +1,14 @@ +package src + +import ( + "fmt" + "github.com/bwmarrin/discordgo" +) + +func BootNotify(s *discordgo.Session, m *discordgo.Ready) { + Err, _ := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") + if Err == nil { + fmt.Println("Bot did not Booted.") + return + } +} diff --git a/src/fecthMessage.go b/src/fecthMessage.go index d7bcf24..b4b9e7a 100644 --- a/src/fecthMessage.go +++ b/src/fecthMessage.go @@ -2,8 +2,6 @@ package src func fetchMessage (cmd string) string{ switch cmd { - case "status": - return updateMessage case "help": return helpMessage case "ping": diff --git a/src/nilCheck.go b/src/nilCheck.go new file mode 100644 index 0000000..f511eef --- /dev/null +++ b/src/nilCheck.go @@ -0,0 +1,13 @@ +package src + +import ( + "github.com/bwmarrin/discordgo" + "log" +) + +func nilCheck(Err *discordgo.Message) { + if Err != nil { + log.Println("Error: ", Err) + return + } +} diff --git a/src/tools.go b/src/tools.go index ec66b0c..d333374 100644 --- a/src/tools.go +++ b/src/tools.go @@ -1,17 +1,13 @@ package src import ( - "fmt" "github.com/bwmarrin/discordgo" "strings" ) -var ( - prefix = "%" -) +var prefix = "%" func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { - //Ping -> Pong if m.Author.ID == s.State.User.ID { return } @@ -19,13 +15,9 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - var cmd = strings.Split(m.Content, " ")[0][len(prefix):] - //temp := strings.Split(m.Content, " ")[1] + cmd := strings.Split(m.Content, " ")[0][:len(prefix)] Err, _ := s.ChannelMessageSend(m.ChannelID, fetchMessage(cmd)) + nilCheck(Err) +} - if Err != nil { - fmt.Println("something wrong:", Err) - return - } -} From d11c737ba0eb84754418ef7db20dee0bb97afb80 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Fri, 1 May 2020 23:14:01 +0900 Subject: [PATCH 32/83] =?UTF-8?q?=E9=96=A2=E6=95=B0=E3=81=BE=E3=81=A8?= =?UTF-8?q?=E3=82=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit とりあえずまとめた --- src/BootNotify.go | 14 -------------- src/message.go | 1 - src/nilCheck.go | 13 ------------- src/tools.go | 11 +++++++++++ 4 files changed, 11 insertions(+), 28 deletions(-) delete mode 100644 src/BootNotify.go delete mode 100644 src/nilCheck.go diff --git a/src/BootNotify.go b/src/BootNotify.go deleted file mode 100644 index a9df2f4..0000000 --- a/src/BootNotify.go +++ /dev/null @@ -1,14 +0,0 @@ -package src - -import ( - "fmt" - "github.com/bwmarrin/discordgo" -) - -func BootNotify(s *discordgo.Session, m *discordgo.Ready) { - Err, _ := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") - if Err == nil { - fmt.Println("Bot did not Booted.") - return - } -} diff --git a/src/message.go b/src/message.go index 8f6c86a..7d46000 100644 --- a/src/message.go +++ b/src/message.go @@ -13,6 +13,5 @@ var ( =Source= github.com/brokenManager/change-status-go     ` + "```\n" - updateMessage = "ステータスを変更しました。" ) diff --git a/src/nilCheck.go b/src/nilCheck.go deleted file mode 100644 index f511eef..0000000 --- a/src/nilCheck.go +++ /dev/null @@ -1,13 +0,0 @@ -package src - -import ( - "github.com/bwmarrin/discordgo" - "log" -) - -func nilCheck(Err *discordgo.Message) { - if Err != nil { - log.Println("Error: ", Err) - return - } -} diff --git a/src/tools.go b/src/tools.go index d333374..5757b38 100644 --- a/src/tools.go +++ b/src/tools.go @@ -2,6 +2,7 @@ package src import ( "github.com/bwmarrin/discordgo" + "log" "strings" ) @@ -20,4 +21,14 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { nilCheck(Err) } +func BootNotify(s *discordgo.Session, m *discordgo.Ready) { + Err, _ := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") + nilCheck(Err) +} +func nilCheck(Err *discordgo.Message) { + if Err != nil { + log.Println("Error: ", Err) + return + } +} From 585b311e891fc0ef8c0410ec8496409be20dba9c Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Tue, 12 May 2020 08:16:22 +0900 Subject: [PATCH 33/83] =?UTF-8?q?[dev]=E3=82=AB=E3=83=A9=E3=83=BC=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=82=92=E5=8F=97=E3=81=91=E5=8F=96=E3=81=A3?= =?UTF-8?q?=E3=81=A6=E7=94=BB=E5=83=8F=E3=82=92=E8=BF=94=E3=81=99=E6=A9=9F?= =?UTF-8?q?=E8=83=BD=E3=81=AE=E5=AE=9F=E8=A3=85-1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/genImage.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ src/sendImage.go | 1 + 2 files changed, 63 insertions(+) create mode 100644 src/genImage.go create mode 100644 src/sendImage.go diff --git a/src/genImage.go b/src/genImage.go new file mode 100644 index 0000000..f21963d --- /dev/null +++ b/src/genImage.go @@ -0,0 +1,62 @@ +package src + +import ( +"fmt" +"image" +color "image/color" +"image/jpeg" +"log" +"os" +"strconv" +) + +var ( + x = 0 + y = 0 + width = 400 + height = 300 + red uint8 + blue uint8 + green uint8 +) + +func intToHexRGB(code uint32) (uint8, uint8, uint8) { + var rgb [3]uint8 + for i := 0;i < 3;i++ { + rgb[i] = uint8(code >> (3 - i) * 8 & 0xFF) + } + + return rgb[0], rgb[1], rgb[2] +} + +func generateImage(colorCode string) { + if len(colorCode) != 6 { + os.Exit(0) + } + num, _ := strconv.ParseInt(colorCode,16 ,32) + + img := image.NewRGBA(image.Rect(x, y, width, height)) + + red, green, blue := intToHexRGB(uint32(num)) + for i := img.Rect.Min.Y; i < img.Rect.Max.Y; i++ { + for j := img.Rect.Min.X; j < img.Rect.Max.X; j++ { + img.Set(j, i, color.RGBA{R: red, G: green, B: blue}) + } + } + + file, _ := os.Create("sample.jpeg") + defer file.Close() + + err := jpeg.Encode(file, img, &jpeg.Options{Quality: 100}) + if err != nil { + log.Println(err) + } + + log.Println("end") +} + +func GenImage(code string) { + fmt.Println(code[1:]) + generateImage(code[len("#"):]) +} + diff --git a/src/sendImage.go b/src/sendImage.go new file mode 100644 index 0000000..c6d5f54 --- /dev/null +++ b/src/sendImage.go @@ -0,0 +1 @@ +package src From 9afcaf76ca50bfcebe4dfda260d08b629098d7bd Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Fri, 15 May 2020 15:00:06 +0900 Subject: [PATCH 34/83] =?UTF-8?q?[dev]=E7=94=BB=E5=83=8F=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=A9=9F=E8=83=BD=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ただしバグありIssueを立てます --- go.mod | 7 ++++++- go.sum | 28 ++++++++++++++++++++++++++++ main.go | 1 + src/genImage.go | 42 +++++++++++++++++++++--------------------- src/message.go | 14 +++++++------- src/sendImage.go | 27 +++++++++++++++++++++++++++ src/tools.go | 3 ++- 7 files changed, 92 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index c5e2460..7f093c1 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module change-status-go go 1.14 -require github.com/bwmarrin/discordgo v0.20.3 +require ( + github.com/bwmarrin/discordgo v0.20.3 + github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect + golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect + golang.org/x/tools v0.0.0-20200501205727-542909fd9944 // indirect +) diff --git a/go.sum b/go.sum index 7123e3e..8be8df3 100644 --- a/go.sum +++ b/go.sum @@ -2,5 +2,33 @@ github.com/bwmarrin/discordgo v0.20.3 h1:AxjcHGbyBFSC0a3Zx5nDQwbOjU7xai5dXjRnZ0Y github.com/bwmarrin/discordgo v0.20.3/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/mattn/go-sqlite3 v1.13.0 h1:LnJI81JidiW9r7pS/hXe6cFeO5EXNq7KbfvoJLRI69c= +github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U= +github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k= +golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200501205727-542909fd9944 h1:H2fcUfqnOlhuExePgcxfMRf98XwAWOF2pqkJTFTc2z0= +golang.org/x/tools v0.0.0-20200501205727-542909fd9944/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 13d0a93..ea4d4b3 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ func main() { discordBrain.AddHandler(command.MessageCreate) + discordBrain.AddHandler(command.SendImage) err = discordBrain.Open() if err != nil { diff --git a/src/genImage.go b/src/genImage.go index f21963d..95ff8a0 100644 --- a/src/genImage.go +++ b/src/genImage.go @@ -1,29 +1,27 @@ package src import ( -"fmt" -"image" -color "image/color" -"image/jpeg" -"log" -"os" -"strconv" + "fmt" + "image" + color "image/color" + "image/jpeg" + "log" + "os" + "strconv" ) -var ( - x = 0 - y = 0 - width = 400 - height = 300 - red uint8 - blue uint8 - green uint8 -) +var x = 0 +var y = 0 +var width = 400 +var height = 300 +var red uint8 +var blue uint8 +var green uint8 func intToHexRGB(code uint32) (uint8, uint8, uint8) { var rgb [3]uint8 for i := 0;i < 3;i++ { - rgb[i] = uint8(code >> (3 - i) * 8 & 0xFF) + rgb[i] = uint8(code >> ((3 - i) * 8) & 0xFF) } return rgb[0], rgb[1], rgb[2] @@ -31,23 +29,25 @@ func intToHexRGB(code uint32) (uint8, uint8, uint8) { func generateImage(colorCode string) { if len(colorCode) != 6 { - os.Exit(0) + log.Println("Error") + return } num, _ := strconv.ParseInt(colorCode,16 ,32) img := image.NewRGBA(image.Rect(x, y, width, height)) red, green, blue := intToHexRGB(uint32(num)) + log.Println(red,blue,green) for i := img.Rect.Min.Y; i < img.Rect.Max.Y; i++ { for j := img.Rect.Min.X; j < img.Rect.Max.X; j++ { img.Set(j, i, color.RGBA{R: red, G: green, B: blue}) } } - file, _ := os.Create("sample.jpeg") - defer file.Close() + File, _ := os.Create("sample.jpeg") + defer File.Close() - err := jpeg.Encode(file, img, &jpeg.Options{Quality: 100}) + err := jpeg.Encode(File, img, &jpeg.Options{Quality: 100}) if err != nil { log.Println(err) } diff --git a/src/message.go b/src/message.go index 7d46000..1689454 100644 --- a/src/message.go +++ b/src/message.go @@ -1,16 +1,16 @@ package src var ( - helpMessage = "```" + -`=Title= + helpMessage = "```asciidoc\n" + +`= Title = something -=Description= += Description = 表示名に絵文字を付けることでステータスを表示するBotの予定 -=Command= += Command = (Prefix: "%") - - help: このBotの概要を説明します。 - - ping: Pong!と返します。かわいいですね。 -=Source= + help :: このBotの概要を説明します。 + ping :: Pong!と返します。かわいいですね。 += Source = github.com/brokenManager/change-status-go     ` + "```\n" ) diff --git a/src/sendImage.go b/src/sendImage.go index c6d5f54..8da464f 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -1 +1,28 @@ package src + +import ( + "github.com/bwmarrin/discordgo" + "log" + "os" + "strings" +) + +func SendImage(session *discordgo.Session, message *discordgo.MessageCreate) { + if message.Author.ID == session.State.User.ID { + return + } + if!strings.HasPrefix(message.Content, "#") { + return + } + GenImage(message.Content) + + file, Err := os.Open("sample.jpeg") + if Err != nil { + log.Println(Err) + } + + _, Err = session.ChannelFileSend(message.ChannelID, "sample.jpeg", file) + if Err != nil { + log.Println(Err) + } +} \ No newline at end of file diff --git a/src/tools.go b/src/tools.go index 5757b38..2aac46f 100644 --- a/src/tools.go +++ b/src/tools.go @@ -16,12 +16,13 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { return } - cmd := strings.Split(m.Content, " ")[0][:len(prefix)] + cmd := strings.Split(m.Content, " ")[0][len(prefix):] Err, _ := s.ChannelMessageSend(m.ChannelID, fetchMessage(cmd)) nilCheck(Err) } func BootNotify(s *discordgo.Session, m *discordgo.Ready) { + // BootNotify is sending message when this bot is booted. Err, _ := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") nilCheck(Err) } From e0caf38ddecfc303328f13023b117e4501083050 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 00:23:47 +0900 Subject: [PATCH 35/83] =?UTF-8?q?[fix]=E6=8C=87=E6=91=98=E7=82=B9=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=9B=B4(1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit いろいろかえました --- main.go | 2 +- src/genImage.go | 62 --------------------------------------------- src/sendImage.go | 66 ++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 62 insertions(+), 68 deletions(-) delete mode 100644 src/genImage.go diff --git a/main.go b/main.go index ea4d4b3..b84227d 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ func main() { discordBrain.AddHandler(command.MessageCreate) - discordBrain.AddHandler(command.SendImage) + discordBrain.AddHandler(command.GenerateImage) err = discordBrain.Open() if err != nil { diff --git a/src/genImage.go b/src/genImage.go deleted file mode 100644 index 95ff8a0..0000000 --- a/src/genImage.go +++ /dev/null @@ -1,62 +0,0 @@ -package src - -import ( - "fmt" - "image" - color "image/color" - "image/jpeg" - "log" - "os" - "strconv" -) - -var x = 0 -var y = 0 -var width = 400 -var height = 300 -var red uint8 -var blue uint8 -var green uint8 - -func intToHexRGB(code uint32) (uint8, uint8, uint8) { - var rgb [3]uint8 - for i := 0;i < 3;i++ { - rgb[i] = uint8(code >> ((3 - i) * 8) & 0xFF) - } - - return rgb[0], rgb[1], rgb[2] -} - -func generateImage(colorCode string) { - if len(colorCode) != 6 { - log.Println("Error") - return - } - num, _ := strconv.ParseInt(colorCode,16 ,32) - - img := image.NewRGBA(image.Rect(x, y, width, height)) - - red, green, blue := intToHexRGB(uint32(num)) - log.Println(red,blue,green) - for i := img.Rect.Min.Y; i < img.Rect.Max.Y; i++ { - for j := img.Rect.Min.X; j < img.Rect.Max.X; j++ { - img.Set(j, i, color.RGBA{R: red, G: green, B: blue}) - } - } - - File, _ := os.Create("sample.jpeg") - defer File.Close() - - err := jpeg.Encode(File, img, &jpeg.Options{Quality: 100}) - if err != nil { - log.Println(err) - } - - log.Println("end") -} - -func GenImage(code string) { - fmt.Println(code[1:]) - generateImage(code[len("#"):]) -} - diff --git a/src/sendImage.go b/src/sendImage.go index 8da464f..7dce16b 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -1,20 +1,76 @@ package src import ( + "fmt" "github.com/bwmarrin/discordgo" + "image" + "image/color" + "image/jpeg" "log" "os" + "strconv" "strings" ) -func SendImage(session *discordgo.Session, message *discordgo.MessageCreate) { - if message.Author.ID == session.State.User.ID { +func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) { + const ( + x = 0 + y = 0 + width = 40 + height = 30 + ) + content := strings.TrimSpace(message.Content) + if !strings.HasPrefix(content, "#") { return } - if!strings.HasPrefix(message.Content, "#") { + + if v, Err := strconv.ParseInt(content[1:], 16, 32); Err != nil || v <= 0 { + log.Println("strconv: invalid value not Hex") return } - GenImage(message.Content) + + colorCode := content[len("#"):] + if len(content) != 6 { + log.Println("generateImage: len(colorCode) != 6") + _, Err := session.ChannelMessageSend(message.ChannelID, "不正な値です。形式は16進のカラーコードである必要があります。") + if Err != nil { + log.Println(Err) + } + return + } + + img := image.NewRGBA(image.Rect(x, y, width, height)) + + red, Err := strconv.ParseInt(colorCode[0:2],16,32) + if Err != nil { + log.Println(fmt.Sprintf("strconv.ParseInt: %s",Err)) + } + green, Err := strconv.ParseInt(colorCode[2:4],16,32) + if Err != nil { + log.Println(fmt.Sprintf("strconv.ParseInt: %s",Err)) + } + blue, Err := strconv.ParseInt(colorCode[4:6],16,32) + if Err != nil { + log.Println(fmt.Sprintf("strconv.ParseInt: %s",Err)) + } + + log.Printf("\"%s\" parsed as %d, %d, %d,", colorCode, red, green, blue) + for i := img.Rect.Min.Y; i < img.Rect.Max.Y; i++ { + for j := img.Rect.Min.X; j < img.Rect.Max.X; j++ { + img.Set(j, i, color.RGBA{R: byte(red), G: byte(green), B: byte(blue)}) + } + } + + File, _ := os.Create("sample.jpeg") + defer File.Close() + + Err = jpeg.Encode(File, img, &jpeg.Options{Quality: 60}) + if Err != nil { + text := fmt.Sprintf("Error at encoding jpeg: %s",Err) + log.Println(text) + } + + log.Println("generatedImage: process ended") file, Err := os.Open("sample.jpeg") if Err != nil { @@ -25,4 +81,4 @@ func SendImage(session *discordgo.Session, message *discordgo.MessageCreate) { if Err != nil { log.Println(Err) } -} \ No newline at end of file +} From 9f417178fdde61836916f5aba768774f0c8868a5 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 00:38:13 +0900 Subject: [PATCH 36/83] =?UTF-8?q?[fix]=E3=81=8C=E3=81=B0=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sendImage.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sendImage.go b/src/sendImage.go index 7dce16b..353ac32 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -24,13 +24,13 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) return } - if v, Err := strconv.ParseInt(content[1:], 16, 32); Err != nil || v <= 0 { - log.Println("strconv: invalid value not Hex") + if v, Err := strconv.ParseInt(content[1:], 16, 32); Err != nil || v < 0 { + log.Println("strconv: invalid value; not Hex") return } colorCode := content[len("#"):] - if len(content) != 6 { + if len(colorCode) != 6 { log.Println("generateImage: len(colorCode) != 6") _, Err := session.ChannelMessageSend(message.ChannelID, "不正な値です。形式は16進のカラーコードである必要があります。") if Err != nil { From e2f20cdc84cc3cc9fa0e4585b6f800d944866c28 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 01:41:14 +0900 Subject: [PATCH 37/83] =?UTF-8?q?[fix]=E4=BF=AE=E6=AD=A3(=EF=BC=92)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 何をしているかわからなくなったので聖火を上げます --- src/sendImage.go | 53 ++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/src/sendImage.go b/src/sendImage.go index 353ac32..29f0254 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -12,10 +12,34 @@ import ( "strings" ) +func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { + red, Err := strconv.ParseInt(colorCode[0:2],16,32) + if Err != nil { + log.Println(fmt.Sprintf("Red --- strconv.ParseInt: %s", Err)) + return + } + green, Err := strconv.ParseInt(colorCode[2:4],16,32) + if Err != nil { + log.Println(fmt.Sprintf("Green --- strconv.ParseInt: %s", Err)) + return + } + blue, Err := strconv.ParseInt(colorCode[4:6],16,32) + if Err != nil { + log.Println(fmt.Sprintf("Blue --- strconv.ParseInt: %s", Err)) + return + } + log.Printf("\"%s\" parsed as %d, %d, %d,", colorCode, red, green, blue) + Result.R = byte(red) + Result.G = byte(green) + Result.B = byte(blue) + return + +} + func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) { const ( - x = 0 - y = 0 + statrX = 0 + startY = 0 width = 40 height = 30 ) @@ -31,7 +55,7 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) colorCode := content[len("#"):] if len(colorCode) != 6 { - log.Println("generateImage: len(colorCode) != 6") + log.Println("generateImage: len(colorCode) must be just 6") _, Err := session.ChannelMessageSend(message.ChannelID, "不正な値です。形式は16進のカラーコードである必要があります。") if Err != nil { log.Println(Err) @@ -39,25 +63,14 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) return } - img := image.NewRGBA(image.Rect(x, y, width, height)) - red, Err := strconv.ParseInt(colorCode[0:2],16,32) - if Err != nil { - log.Println(fmt.Sprintf("strconv.ParseInt: %s",Err)) - } - green, Err := strconv.ParseInt(colorCode[2:4],16,32) - if Err != nil { - log.Println(fmt.Sprintf("strconv.ParseInt: %s",Err)) - } - blue, Err := strconv.ParseInt(colorCode[4:6],16,32) - if Err != nil { - log.Println(fmt.Sprintf("strconv.ParseInt: %s",Err)) - } - log.Printf("\"%s\" parsed as %d, %d, %d,", colorCode, red, green, blue) - for i := img.Rect.Min.Y; i < img.Rect.Max.Y; i++ { - for j := img.Rect.Min.X; j < img.Rect.Max.X; j++ { - img.Set(j, i, color.RGBA{R: byte(red), G: byte(green), B: byte(blue)}) + img := image.NewRGBA(image.Rect(statrX, startY, width, height)) + + colorInfo, Err := ParseColorCode(colorCode) + for x := img.Rect.Min.Y; x < img.Rect.Max.Y; x++ { + for y := img.Rect.Min.X; y < img.Rect.Max.X; y++ { + img.Set(x, y, colorInfo) } } From ab19dd15680809e7f6efc8a12d552de29941d488 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 01:43:03 +0900 Subject: [PATCH 38/83] =?UTF-8?q?[fix]=E3=81=8C=E3=81=B0=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sendImage.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/sendImage.go b/src/sendImage.go index 29f0254..6a7b8ba 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -68,6 +68,9 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) img := image.NewRGBA(image.Rect(statrX, startY, width, height)) colorInfo, Err := ParseColorCode(colorCode) + if Err != nil { + log.Println(Err) + } for x := img.Rect.Min.Y; x < img.Rect.Max.Y; x++ { for y := img.Rect.Min.X; y < img.Rect.Max.X; y++ { img.Set(x, y, colorInfo) From d806e8e1686f35c915a1fd7bc79d1a8bf7154754 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 01:56:11 +0900 Subject: [PATCH 39/83] =?UTF-8?q?[fix]=E4=BF=AE=E6=AD=A3(=EF=BC=93)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit がばがば --- src/sendImage.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/sendImage.go b/src/sendImage.go index 6a7b8ba..0d30e60 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -36,13 +36,26 @@ func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { } -func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) { +func genImage(colorInfo color.RGBA) *image.RGBA { const ( statrX = 0 startY = 0 width = 40 height = 30 ) + + img := image.NewRGBA(image.Rect(statrX, startY, width, height)) + + for x := img.Rect.Min.Y; x < img.Rect.Max.Y; x++ { + for y := img.Rect.Min.X; y < img.Rect.Max.X; y++ { + img.Set(x, y, colorInfo) + } + } + return img +} + +func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) { + content := strings.TrimSpace(message.Content) if !strings.HasPrefix(content, "#") { return @@ -65,22 +78,22 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) - img := image.NewRGBA(image.Rect(statrX, startY, width, height)) + colorInfo, Err := ParseColorCode(colorCode) + colorImage := genImage(colorInfo) if Err != nil { log.Println(Err) - } - for x := img.Rect.Min.Y; x < img.Rect.Max.Y; x++ { - for y := img.Rect.Min.X; y < img.Rect.Max.X; y++ { - img.Set(x, y, colorInfo) - } + return } - File, _ := os.Create("sample.jpeg") + File, Err := os.Create("sample.jpeg") + if Err != nil { + + } defer File.Close() - Err = jpeg.Encode(File, img, &jpeg.Options{Quality: 60}) + Err = jpeg.Encode(File, colorImage, &jpeg.Options{Quality: 60}) if Err != nil { text := fmt.Sprintf("Error at encoding jpeg: %s",Err) log.Println(text) From 3ad266fb44d445391e35656996cbaddc6cd83005 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 02:01:20 +0900 Subject: [PATCH 40/83] =?UTF-8?q?[fix]=E3=81=8C=E3=81=B0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sendImage.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/sendImage.go b/src/sendImage.go index 0d30e60..a4051fd 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -13,17 +13,17 @@ import ( ) func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { - red, Err := strconv.ParseInt(colorCode[0:2],16,32) + red, Err := strconv.ParseInt(colorCode[0:2], 16, 32) if Err != nil { log.Println(fmt.Sprintf("Red --- strconv.ParseInt: %s", Err)) return } - green, Err := strconv.ParseInt(colorCode[2:4],16,32) + green, Err := strconv.ParseInt(colorCode[2:4], 16, 32) if Err != nil { log.Println(fmt.Sprintf("Green --- strconv.ParseInt: %s", Err)) return } - blue, Err := strconv.ParseInt(colorCode[4:6],16,32) + blue, Err := strconv.ParseInt(colorCode[4:6], 16, 32) if Err != nil { log.Println(fmt.Sprintf("Blue --- strconv.ParseInt: %s", Err)) return @@ -76,10 +76,6 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) return } - - - - colorInfo, Err := ParseColorCode(colorCode) colorImage := genImage(colorInfo) if Err != nil { @@ -87,21 +83,21 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) return } - File, Err := os.Create("sample.jpeg") + file, Err := os.Create("sample.jpeg") if Err != nil { } - defer File.Close() + defer file.Close() - Err = jpeg.Encode(File, colorImage, &jpeg.Options{Quality: 60}) + Err = jpeg.Encode(file, colorImage, &jpeg.Options{Quality: 60}) if Err != nil { - text := fmt.Sprintf("Error at encoding jpeg: %s",Err) + text := fmt.Sprintf("Error at encoding jpeg: %s", Err) log.Println(text) } log.Println("generatedImage: process ended") - file, Err := os.Open("sample.jpeg") + file, Err = os.Open("sample.jpeg") if Err != nil { log.Println(Err) } From 362e4f8bce1bdb6911f083c7707c2feff6501bfa Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 02:06:33 +0900 Subject: [PATCH 41/83] =?UTF-8?q?[fix]=E4=BF=AE=E6=AD=A3(=EF=BC=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sendImage.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/sendImage.go b/src/sendImage.go index a4051fd..a61a321 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -85,7 +85,7 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) file, Err := os.Create("sample.jpeg") if Err != nil { - + return } defer file.Close() @@ -93,6 +93,7 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) if Err != nil { text := fmt.Sprintf("Error at encoding jpeg: %s", Err) log.Println(text) + return } log.Println("generatedImage: process ended") @@ -100,10 +101,12 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) file, Err = os.Open("sample.jpeg") if Err != nil { log.Println(Err) + return } _, Err = session.ChannelFileSend(message.ChannelID, "sample.jpeg", file) if Err != nil { log.Println(Err) + return } } From cb7edf24dd199a48c0a3ad962b572c8bae86f82c Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 02:10:51 +0900 Subject: [PATCH 42/83] =?UTF-8?q?[fix]=E9=A0=86=E7=95=AA=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sendImage.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sendImage.go b/src/sendImage.go index a61a321..4bdf36c 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -77,12 +77,13 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) } colorInfo, Err := ParseColorCode(colorCode) - colorImage := genImage(colorInfo) if Err != nil { log.Println(Err) return } + colorImage := genImage(colorInfo) + file, Err := os.Create("sample.jpeg") if Err != nil { return From 4f2ad93ee46416fc7d087e8bb25b6aac0ed7d40b Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 02:44:25 +0900 Subject: [PATCH 43/83] =?UTF-8?q?[fix]=E3=81=8C=E3=81=B0=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?(=EF=BC=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit これが最後と信じたい --- src/sendImage.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/sendImage.go b/src/sendImage.go index 4bdf36c..c3c5298 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -1,13 +1,14 @@ package src import ( + "bufio" + "bytes" "fmt" "github.com/bwmarrin/discordgo" "image" "image/color" "image/jpeg" "log" - "os" "strconv" "strings" ) @@ -83,29 +84,28 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) } colorImage := genImage(colorInfo) + var ( + buffer bytes.Buffer + fileWriter = bufio.NewWriter(&buffer) + fileReader = bufio.NewReader(&buffer) + ) - file, Err := os.Create("sample.jpeg") + Err = jpeg.Encode(fileWriter, colorImage, &jpeg.Options{Quality: 60}) if Err != nil { + text := fmt.Sprintf("Error at encoding jpeg: %s", Err) + log.Println(text) return } - defer file.Close() - - Err = jpeg.Encode(file, colorImage, &jpeg.Options{Quality: 60}) + Err = fileWriter.Flush() if Err != nil { - text := fmt.Sprintf("Error at encoding jpeg: %s", Err) + text := fmt.Sprintf("Error at io.Writer flush: %s", Err) log.Println(text) return } log.Println("generatedImage: process ended") - file, Err = os.Open("sample.jpeg") - if Err != nil { - log.Println(Err) - return - } - - _, Err = session.ChannelFileSend(message.ChannelID, "sample.jpeg", file) + _, Err = session.ChannelFileSend(message.ChannelID, "sample.jpeg", fileReader) if Err != nil { log.Println(Err) return From 6616b87b539495b9944a2d9da7a1a7086d696e22 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 15:19:31 +0900 Subject: [PATCH 44/83] =?UTF-8?q?[fix]=E8=AA=AC=E6=98=8E=E6=96=87=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/message.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/message.go b/src/message.go index 7d46000..feb7b86 100644 --- a/src/message.go +++ b/src/message.go @@ -1,17 +1,18 @@ package src var ( - helpMessage = "```" + -`=Title= + helpMessage = "```asciidoc\n" + + `= Title = something =Description= 表示名に絵文字を付けることでステータスを表示するBotの予定 =Command= (Prefix: "%") - - help: このBotの概要を説明します。 - - ping: Pong!と返します。かわいいですね。 -=Source= + help :: このBotの概要を説明します。 + ping :: Pong!と返します。かわいいですね。 + (prefix: "#") + [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 += Source = github.com/brokenManager/change-status-go     ` + "```\n" ) - From ae546e0965c4a2ed54ef6caca8dfd35aaaece490 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sat, 16 May 2020 15:20:38 +0900 Subject: [PATCH 45/83] =?UTF-8?q?[fix]=E8=A1=9D=E7=AA=81=E5=9B=9E=E9=81=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sendImage.go | 113 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/sendImage.go diff --git a/src/sendImage.go b/src/sendImage.go new file mode 100644 index 0000000..6a17722 --- /dev/null +++ b/src/sendImage.go @@ -0,0 +1,113 @@ +package src + +import ( + "bufio" + "bytes" + "fmt" + "github.com/bwmarrin/discordgo" + "image" + "image/color" + "image/jpeg" + "log" + "strconv" + "strings" +) + +func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { + red, Err := strconv.ParseInt(colorCode[0:2], 16, 32) + if Err != nil { + log.Println(fmt.Sprintf("Red --- strconv.ParseInt: %s", Err)) + return + } + green, Err := strconv.ParseInt(colorCode[2:4], 16, 32) + if Err != nil { + log.Println(fmt.Sprintf("Green --- strconv.ParseInt: %s", Err)) + return + } + blue, Err := strconv.ParseInt(colorCode[4:6], 16, 32) + if Err != nil { + log.Println(fmt.Sprintf("Blue --- strconv.ParseInt: %s", Err)) + return + } + log.Printf("\"%s\" parsed as %d, %d, %d,", colorCode, red, green, blue) + Result.R = byte(red) + Result.G = byte(green) + Result.B = byte(blue) + return + +} + +func genImage(colorInfo color.RGBA) *image.RGBA { + const ( + statrX = 0 + startY = 0 + width = 40 + height = 30 + ) + + img := image.NewRGBA(image.Rect(statrX, startY, width, height)) + + for y := img.Rect.Min.Y; y < img.Rect.Max.Y; y++ { + for x := img.Rect.Min.X; x < img.Rect.Max.X; x++ { + img.Set(x, y, colorInfo) + } + } + return img +} + +func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) { + + content := strings.TrimSpace(message.Content) + if !strings.HasPrefix(content, "#") { + return + } + + if v, Err := strconv.ParseInt(content[1:], 16, 32); Err != nil || v < 0 { + log.Println("strconv: invalid value; not Hex") + return + } + + colorCode := content[len("#"):] + if len(colorCode) != 6 { + log.Println("generateImage: len(colorCode) must be just 6") + _, Err := session.ChannelMessageSend(message.ChannelID, "不正な値です。形式は16進のカラーコードである必要があります。") + if Err != nil { + log.Println(Err) + } + return + } + + colorInfo, Err := ParseColorCode(colorCode) + if Err != nil { + log.Println(Err) + return + } + + colorImage := genImage(colorInfo) + var ( + buffer bytes.Buffer + fileWriter = bufio.NewWriter(&buffer) + fileReader = bufio.NewReader(&buffer) + ) + + Err = jpeg.Encode(fileWriter, colorImage, &jpeg.Options{Quality: 60}) + if Err != nil { + text := fmt.Sprintf("Error at encoding jpeg: %s", Err) + log.Println(text) + return + } + Err = fileWriter.Flush() + if Err != nil { + text := fmt.Sprintf("Error at io.Writer flush: %s", Err) + log.Println(text) + return + } + + log.Println("generatedImage: process ended") + + _, Err = session.ChannelFileSend(message.ChannelID, "sample.jpeg", fileReader) + if Err != nil { + log.Println(Err) + return + } +} From 8314343dd134054567cab65d2f3755dc35181c63 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sun, 17 May 2020 21:46:17 +0900 Subject: [PATCH 46/83] =?UTF-8?q?[dev]Morse=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit バグがありますがとりあえず --- main.go | 1 + src/decodeMorse.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/decodeMorse.go diff --git a/main.go b/main.go index b84227d..3811e35 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ func main() { discordBrain.AddHandler(command.MessageCreate) discordBrain.AddHandler(command.GenerateImage) + discordBrain.AddHandler(command.DecodeMorse) err = discordBrain.Open() if err != nil { diff --git a/src/decodeMorse.go b/src/decodeMorse.go new file mode 100644 index 0000000..ce9d5a7 --- /dev/null +++ b/src/decodeMorse.go @@ -0,0 +1,91 @@ +package src + +import ( + "fmt" + "github.com/bwmarrin/discordgo" + "log" + "strings" +) + +const prefixMorseDecode = "DM" +const prefixMorseEncode = "EM" + +func decode(sentence string, wordSep string) (string, error) { + var response string + for _, s := range strings.Split(sentence, " ") { + if s ==wordSep { + response += " " + } + for key, value := range alphabetTable { + if s != value { + return "[]", fmt.Errorf("Not such a code: %s", s) + } + response += key + + } + } + return sentence, nil +} + +func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { + if message.Author.ID == session.State.User.ID { + return + } + if !strings.HasPrefix(message.Content, prefixMorseDecode) { + return + } + sentence := message.Content[2:] + content, Err := decode(sentence, " ") + if Err != nil { + log.Println("Failed to decode:", Err) + return + } + _, Err = session.ChannelMessageSend(message.ChannelID, content) + if Err != nil { + log.Println("Error at ChannelMessageSend:", Err) + return + } +} + +var alphabetTable = map[string]string{ + "a" : ".-", + "b" : "-...", + "c" : "-.-.", + "d" : "-..", + "e" : ".", + "f" : "..-.", + "g" : "--.", + "h" : "....", + "i" : "..", + "j" : ".---", + "k" : "-.-", + "l" : ".-..", + "m" : "--", + "n" : "-.", + "o" : "---", + "p" : ".--.", + "q" : "--.-", + "r" : ".-.", + "s" : "...", + "t" : "-", + "u" : "..-", + "v" : "...-", + "w" : "-..-", + "x" : "-..-", + "z" : "--..", + "." : ".--.-.", + "," : "--..--", + ":" : "---...", + "?" : "..--..", + "'" : ".----.", + "-" : "-....-", + "(" : "-.--.", + ")" : "-.--.-", + "/" : "-..-.", + "=" : "-...-.", + "+" : ".-.-.-", + "\"" : ".-..-.", + "*" : "-..-", + "@" : ".--.-.", + "amend" : "........", +} From 34b004fbbff2291eaea2946cd85bce1d267dc16e Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sun, 17 May 2020 23:25:53 +0900 Subject: [PATCH 47/83] =?UTF-8?q?[fix]=E6=8C=87=E6=91=98=E7=82=B9=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit まだバグは残っている --- main.go | 5 +- src/decodeMorse.go | 121 ++++++++++++++++++++++++--------------------- 2 files changed, 66 insertions(+), 60 deletions(-) diff --git a/main.go b/main.go index 3811e35..3d22d0a 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,6 @@ func main() { fmt.Println(err) } - discordBrain.AddHandler(command.MessageCreate) discordBrain.AddHandler(command.GenerateImage) discordBrain.AddHandler(command.DecodeMorse) @@ -30,8 +29,8 @@ func main() { fmt.Println("Bot起動完了、命令待機中") discordBrain.AddHandlerOnce(command.BootNotify) - sc := make(chan os.Signal,1) + sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) - <- sc + <-sc return } diff --git a/src/decodeMorse.go b/src/decodeMorse.go index ce9d5a7..7cbac3f 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -4,88 +4,95 @@ import ( "fmt" "github.com/bwmarrin/discordgo" "log" + "regexp" "strings" ) -const prefixMorseDecode = "DM" -const prefixMorseEncode = "EM" +const ( + prefixMorseDecode = "DM" + prefixMorseEncode = "EM" + space = " " +) + +var alphabetTable = map[string]string{ + "a": ".-", + "b": "-...", + "c": "-.-.", + "d": "-..", + "e": ".", + "f": "..-.", + "g": "--.", + "h": "....", + "i": "..", + "j": ".---", + "k": "-.-", + "l": ".-..", + "m": "--", + "n": "-.", + "o": "---", + "p": ".--.", + "q": "--.-", + "r": ".-.", + "s": "...", + "t": "-", + "u": "..-", + "v": "...-", + "w": "-..-", + "x": "-..-", + "z": "--..", + ".": ".--.-.", + ",": "--..--", + ":": "---...", + "?": "..--..", + "'": ".----.", + "-": "-....-", + "(": "-.--.", + ")": "-.--.-", + "/": "-..-.", + "=": "-...-.", + "+": ".-.-.-", + "\"": ".-..-.", + "*": "-..-", + "@": ".--.-.", + "amend": "........", +} -func decode(sentence string, wordSep string) (string, error) { +func decode(sentence string) (string, error) { var response string - for _, s := range strings.Split(sentence, " ") { - if s ==wordSep { + reg := regexp.MustCompile(`[ \t+]`) + sentence = reg.ReplaceAllString(sentence, " ") + for _, part := range strings.Split(sentence, " ") { + if part == space { response += " " + continue } - for key, value := range alphabetTable { - if s != value { - return "[]", fmt.Errorf("Not such a code: %s", s) + for alphabet, morse := range alphabetTable { + if part == morse { + response += alphabet + break } - response += key - + return "[]", fmt.Errorf("Not such a code: %s", part) } } - return sentence, nil + return response, nil } func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { - if message.Author.ID == session.State.User.ID { + if (message.Author.ID == session.State.User.ID) || message.Author.Bot { return } if !strings.HasPrefix(message.Content, prefixMorseDecode) { return } sentence := message.Content[2:] - content, Err := decode(sentence, " ") + decodeResult, Err := decode(sentence) if Err != nil { log.Println("Failed to decode:", Err) return } - _, Err = session.ChannelMessageSend(message.ChannelID, content) + _, Err = session.ChannelMessageSend(message.ChannelID, decodeResult) if Err != nil { log.Println("Error at ChannelMessageSend:", Err) return } } - -var alphabetTable = map[string]string{ - "a" : ".-", - "b" : "-...", - "c" : "-.-.", - "d" : "-..", - "e" : ".", - "f" : "..-.", - "g" : "--.", - "h" : "....", - "i" : "..", - "j" : ".---", - "k" : "-.-", - "l" : ".-..", - "m" : "--", - "n" : "-.", - "o" : "---", - "p" : ".--.", - "q" : "--.-", - "r" : ".-.", - "s" : "...", - "t" : "-", - "u" : "..-", - "v" : "...-", - "w" : "-..-", - "x" : "-..-", - "z" : "--..", - "." : ".--.-.", - "," : "--..--", - ":" : "---...", - "?" : "..--..", - "'" : ".----.", - "-" : "-....-", - "(" : "-.--.", - ")" : "-.--.-", - "/" : "-..-.", - "=" : "-...-.", - "+" : ".-.-.-", - "\"" : ".-..-.", - "*" : "-..-", - "@" : ".--.-.", - "amend" : "........", -} From aad772fafdf187675174183a772063c7e51b6f3d Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 00:02:53 +0900 Subject: [PATCH 48/83] =?UTF-8?q?[fix]=E6=8C=87=E6=91=98=E7=82=B9=E3=81=AE?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index 7cbac3f..58eaa27 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -59,11 +59,10 @@ var alphabetTable = map[string]string{ func decode(sentence string) (string, error) { var response string - reg := regexp.MustCompile(`[ \t+]`) + reg := regexp.MustCompile(`[ \t]+`) sentence = reg.ReplaceAllString(sentence, " ") for _, part := range strings.Split(sentence, " ") { if part == space { - response += " " continue } for alphabet, morse := range alphabetTable { @@ -78,7 +77,7 @@ func decode(sentence string) (string, error) { } func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { - if (message.Author.ID == session.State.User.ID) || message.Author.Bot { + if message.Author.Bot { return } if !strings.HasPrefix(message.Content, prefixMorseDecode) { @@ -93,6 +92,5 @@ func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { _, Err = session.ChannelMessageSend(message.ChannelID, decodeResult) if Err != nil { log.Println("Error at ChannelMessageSend:", Err) - return } } From 7c482519378aac5cb5f281f4c46e89353652d38c Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 01:19:00 +0900 Subject: [PATCH 49/83] =?UTF-8?q?[fix]=E3=83=90=E3=82=B0=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index 58eaa27..6827d94 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -1,6 +1,7 @@ package src import ( + "errors" "fmt" "github.com/bwmarrin/discordgo" "log" @@ -9,9 +10,7 @@ import ( ) const ( - prefixMorseDecode = "DM" - prefixMorseEncode = "EM" - space = " " + prefixMorseDecode = "%decode" ) var alphabetTable = map[string]string{ @@ -57,23 +56,31 @@ var alphabetTable = map[string]string{ "amend": "........", } -func decode(sentence string) (string, error) { - var response string +func searchTable(morseInput string) (string, bool) { + for alphabet, morse := range alphabetTable { + if morseInput == morse { + return alphabet, true + } + } + + return "", false +} + +func decode(sentence string) (response string, Err error) { reg := regexp.MustCompile(`[ \t]+`) sentence = reg.ReplaceAllString(sentence, " ") + log.Printf(sentence) for _, part := range strings.Split(sentence, " ") { - if part == space { - continue - } - for alphabet, morse := range alphabetTable { - if part == morse { - response += alphabet - break - } - return "[]", fmt.Errorf("Not such a code: %s", part) + + alphabet, found := searchTable(part) + + if !found { + return "", errors.New(fmt.Sprintf("Not found such code: %s", part)) } + + response += alphabet } - return response, nil + return } func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { @@ -83,7 +90,7 @@ func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { if !strings.HasPrefix(message.Content, prefixMorseDecode) { return } - sentence := message.Content[2:] + sentence := message.Content[len(prefixMorseDecode)+1:] decodeResult, Err := decode(sentence) if Err != nil { log.Println("Failed to decode:", Err) From 821bb784c34e7026a826ce8c4063a39310c8e355 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 01:29:56 +0900 Subject: [PATCH 50/83] =?UTF-8?q?[fix]=E6=8C=87=E6=91=98=E7=82=B9=E3=81=AE?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index 6827d94..22d0dd6 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -75,7 +75,7 @@ func decode(sentence string) (response string, Err error) { alphabet, found := searchTable(part) if !found { - return "", errors.New(fmt.Sprintf("Not found such code: %s", part)) + Err = errors.New(fmt.Sprintf("Not found such code: %s", part)) } response += alphabet @@ -90,7 +90,7 @@ func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { if !strings.HasPrefix(message.Content, prefixMorseDecode) { return } - sentence := message.Content[len(prefixMorseDecode)+1:] + sentence := strings.TrimSpace(message.Content[len(prefixMorseDecode):]) decodeResult, Err := decode(sentence) if Err != nil { log.Println("Failed to decode:", Err) From 11e713c44212bea7b6fefa8b2f4efc4d09c767c1 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 01:32:29 +0900 Subject: [PATCH 51/83] =?UTF-8?q?[fix]=E3=81=8C=E3=81=B0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index 22d0dd6..202919d 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -76,6 +76,7 @@ func decode(sentence string) (response string, Err error) { if !found { Err = errors.New(fmt.Sprintf("Not found such code: %s", part)) + return } response += alphabet From 027958bfbbe5af166b1d13820217bb79cd39a66c Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 01:40:52 +0900 Subject: [PATCH 52/83] =?UTF-8?q?[dev]=E5=BE=A9=E5=8F=B7=E5=A4=B1=E6=95=97?= =?UTF-8?q?=E6=99=82=E3=81=AE=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E9=80=81=E4=BF=A1=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index 202919d..c736a33 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -95,6 +95,10 @@ func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { decodeResult, Err := decode(sentence) if Err != nil { log.Println("Failed to decode:", Err) + _, Err = session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("復号に失敗しました。: %s", Err)) + if Err != nil { + log.Println("Error at ChannelMessageSend: ", Err) + } return } _, Err = session.ChannelMessageSend(message.ChannelID, decodeResult) From ad0a7a6e694898c355d3dc23b7fb684bb8f8f41d Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 01:56:58 +0900 Subject: [PATCH 53/83] =?UTF-8?q?[refactor]nilCheck=E3=81=AE=E5=89=8A?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/tools.go b/src/tools.go index 2aac46f..c41a5b2 100644 --- a/src/tools.go +++ b/src/tools.go @@ -6,7 +6,7 @@ import ( "strings" ) -var prefix = "%" +const prefix = "%" func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.ID == s.State.User.ID { @@ -18,18 +18,15 @@ func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { cmd := strings.Split(m.Content, " ")[0][len(prefix):] Err, _ := s.ChannelMessageSend(m.ChannelID, fetchMessage(cmd)) - nilCheck(Err) + if Err != nil { + log.Println("failed send message: ", Err) + } } func BootNotify(s *discordgo.Session, m *discordgo.Ready) { // BootNotify is sending message when this bot is booted. - Err, _ := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") - nilCheck(Err) -} - -func nilCheck(Err *discordgo.Message) { + _, Err := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") if Err != nil { - log.Println("Error: ", Err) - return + log.Println("Boot failed: ", Err) } } From 3a2792abd56246b4d016f810db319948c29aaa7b Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 04:51:42 +0900 Subject: [PATCH 54/83] =?UTF-8?q?[refactor]help,ping,color=E3=81=AE?= =?UTF-8?q?=E5=86=8D=E8=A8=AD=E8=A8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 16 ++++-------- src/sendImage.go | 38 ++++++++------------------- src/tools.go | 64 +++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 72 insertions(+), 46 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index c736a33..c42e66c 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -9,10 +9,6 @@ import ( "strings" ) -const ( - prefixMorseDecode = "%decode" -) - var alphabetTable = map[string]string{ "a": ".-", "b": "-...", @@ -36,10 +32,11 @@ var alphabetTable = map[string]string{ "t": "-", "u": "..-", "v": "...-", - "w": "-..-", + "w": ".--", "x": "-..-", + "y": "-.--", "z": "--..", - ".": ".--.-.", + ".": ".-.-.-", ",": "--..--", ":": "---...", "?": "..--..", @@ -49,7 +46,7 @@ var alphabetTable = map[string]string{ ")": "-.--.-", "/": "-..-.", "=": "-...-.", - "+": ".-.-.-", + "+": ".-.-.", "\"": ".-..-.", "*": "-..-", "@": ".--.-.", @@ -88,10 +85,7 @@ func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { if message.Author.Bot { return } - if !strings.HasPrefix(message.Content, prefixMorseDecode) { - return - } - sentence := strings.TrimSpace(message.Content[len(prefixMorseDecode):]) + sentence := strings.TrimSpace(message.Content) decodeResult, Err := decode(sentence) if Err != nil { log.Println("Failed to decode:", Err) diff --git a/src/sendImage.go b/src/sendImage.go index 6a17722..45914b8 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -8,6 +8,7 @@ import ( "image" "image/color" "image/jpeg" + "io" "log" "strconv" "strings" @@ -55,28 +56,17 @@ func genImage(colorInfo color.RGBA) *image.RGBA { return img } -func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) { - - content := strings.TrimSpace(message.Content) - if !strings.HasPrefix(content, "#") { - return - } +func GenerateImage(colorCode string) (fileReader io.Reader, Err error) { + var ( + buffer bytes.Buffer + fileWriter = bufio.NewWriter(&buffer) + ) - if v, Err := strconv.ParseInt(content[1:], 16, 32); Err != nil || v < 0 { + if v, Err := strconv.ParseInt(colorCode[len("#"):], 16, 32); Err != nil || v < 0 { log.Println("strconv: invalid value; not Hex") return } - colorCode := content[len("#"):] - if len(colorCode) != 6 { - log.Println("generateImage: len(colorCode) must be just 6") - _, Err := session.ChannelMessageSend(message.ChannelID, "不正な値です。形式は16進のカラーコードである必要があります。") - if Err != nil { - log.Println(Err) - } - return - } - colorInfo, Err := ParseColorCode(colorCode) if Err != nil { log.Println(Err) @@ -84,11 +74,8 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) } colorImage := genImage(colorInfo) - var ( - buffer bytes.Buffer - fileWriter = bufio.NewWriter(&buffer) - fileReader = bufio.NewReader(&buffer) - ) + + fileReader = bufio.NewReader(&buffer) Err = jpeg.Encode(fileWriter, colorImage, &jpeg.Options{Quality: 60}) if Err != nil { @@ -104,10 +91,5 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) } log.Println("generatedImage: process ended") - - _, Err = session.ChannelFileSend(message.ChannelID, "sample.jpeg", fileReader) - if Err != nil { - log.Println(Err) - return - } + return } diff --git a/src/tools.go b/src/tools.go index c41a5b2..0f58e26 100644 --- a/src/tools.go +++ b/src/tools.go @@ -2,25 +2,75 @@ package src import ( "github.com/bwmarrin/discordgo" + "io" "log" "strings" ) const prefix = "%" -func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.State.User.ID { +type messageContext struct { + s *discordgo.Session + m *discordgo.MessageCreate +} + +func (cxt *messageContext) messageSend(message string) (Err error) { + _, Err = cxt.s.ChannelMessageSend(cxt.m.ChannelID, message) + if Err != nil { + log.Println("failed send message: ", Err) return } - if !strings.HasPrefix(m.Content, prefix) { + return +} + +func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) { + _, Err = cxt.s.ChannelFileSend(cxt.m.ChannelID, fileName, data) + if Err != nil { + log.Println("failed send file: ", Err) return } + return +} - cmd := strings.Split(m.Content, " ")[0][len(prefix):] - Err, _ := s.ChannelMessageSend(m.ChannelID, fetchMessage(cmd)) - if Err != nil { - log.Println("failed send message: ", Err) +func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { + context := messageContext{ + session, + message, + } + const colorCodePrefix = "#" + + if message.Author.ID == session.State.User.ID { + return } + if !strings.HasPrefix(message.Content, prefix) { + return + } + commandBody := strings.Split(message.Content, " ") + switch commandBody[0][len(prefix):] { + case "color": + { + fileData, Err := GenerateImage(commandBody[1]) + if Err != nil { + log.Println("failed to genarateImage: ", Err) + return + } + Err = context.fileSend("unkonow.jpeg", fileData) + if Err != nil { + log.Println("failed file send: ", Err) + return + } + } + case "ping", "help": + { + contentText := fetchMessage(commandBody[0]) + Err := context.messageSend(contentText) + if Err != nil { + log.Println("failed send message: ", Err) + return + } + } + } + } func BootNotify(s *discordgo.Session, m *discordgo.Ready) { From 0c719cbe4b3e6b85fe67fc2a307b521a8deb2d82 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 04:54:40 +0900 Subject: [PATCH 55/83] =?UTF-8?q?[fix]=E3=83=93=E3=83=AB=E3=83=89=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=81=AE=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 先にビルドしてね --- src/sendImage.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/sendImage.go b/src/sendImage.go index 45914b8..c97ae9a 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -4,14 +4,12 @@ import ( "bufio" "bytes" "fmt" - "github.com/bwmarrin/discordgo" "image" "image/color" "image/jpeg" "io" "log" "strconv" - "strings" ) func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { @@ -64,13 +62,13 @@ func GenerateImage(colorCode string) (fileReader io.Reader, Err error) { if v, Err := strconv.ParseInt(colorCode[len("#"):], 16, 32); Err != nil || v < 0 { log.Println("strconv: invalid value; not Hex") - return + return nil, Err } colorInfo, Err := ParseColorCode(colorCode) if Err != nil { log.Println(Err) - return + return nil, Err } colorImage := genImage(colorInfo) From 86169a5116130d2066f1b186b23d53df7ee684ed Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 12:16:08 +0900 Subject: [PATCH 56/83] =?UTF-8?q?[fix,refactor]=E3=83=8F=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=83=A9=E7=B5=B1=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.go b/main.go index 3d22d0a..b51e8ca 100644 --- a/main.go +++ b/main.go @@ -19,8 +19,6 @@ func main() { } discordBrain.AddHandler(command.MessageCreate) - discordBrain.AddHandler(command.GenerateImage) - discordBrain.AddHandler(command.DecodeMorse) err = discordBrain.Open() if err != nil { From 014035dcea4358a30820b2579d27a64950e33923 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 12:17:17 +0900 Subject: [PATCH 57/83] =?UTF-8?q?[refactor]=E5=8D=98=E7=B4=94=E3=81=AA?= =?UTF-8?q?=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8=E8=BF=94=E7=AD=94?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E5=86=8D=E8=A8=AD=E8=A8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/tools.go b/src/tools.go index 0f58e26..7aa059e 100644 --- a/src/tools.go +++ b/src/tools.go @@ -7,7 +7,10 @@ import ( "strings" ) -const prefix = "%" +const ( + prefix = "%" + colorPrefix = "#" +) type messageContext struct { s *discordgo.Session @@ -60,14 +63,13 @@ func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) return } } - case "ping", "help": - { + default: + contentText := fetchMessage(commandBody[0][len(prefix):]) contentText := fetchMessage(commandBody[0]) - Err := context.messageSend(contentText) - if Err != nil { - log.Println("failed send message: ", Err) - return - } + Err := context.messageSend(contentText) + if Err != nil { + log.Println("failed send message: ", Err) + return } } From 118b8ca31be1d883684f0551dcf2a192ef6ed6bd Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 12:17:56 +0900 Subject: [PATCH 58/83] =?UTF-8?q?[fix]BootNotify=E3=81=AE=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E3=81=AE=E5=BC=95=E6=95=B0=E3=81=AE=E7=A0=B4?= =?UTF-8?q?=E6=A3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools.go b/src/tools.go index 7aa059e..b72c3a4 100644 --- a/src/tools.go +++ b/src/tools.go @@ -75,7 +75,7 @@ func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) } -func BootNotify(s *discordgo.Session, m *discordgo.Ready) { +func BootNotify(s *discordgo.Session, _ *discordgo.Ready) { // BootNotify is sending message when this bot is booted. _, Err := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") if Err != nil { From 6eed3f2f85adf05e490a5bdb1244a1c0a34736ce Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 12:19:43 +0900 Subject: [PATCH 59/83] =?UTF-8?q?[refactor]=E7=94=BB=E5=83=8F=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=87=A6=E7=90=86=E3=81=AE=E5=86=8D=E8=A8=AD=E8=A8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sendImage.go | 11 +++++++---- src/tools.go | 24 +++++++++++++++--------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/sendImage.go b/src/sendImage.go index c97ae9a..b93360f 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -38,13 +38,13 @@ func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { func genImage(colorInfo color.RGBA) *image.RGBA { const ( - statrX = 0 + startX = 0 startY = 0 width = 40 height = 30 ) - img := image.NewRGBA(image.Rect(statrX, startY, width, height)) + img := image.NewRGBA(image.Rect(startX, startY, width, height)) for y := img.Rect.Min.Y; y < img.Rect.Max.Y; y++ { for x := img.Rect.Min.X; x < img.Rect.Max.X; x++ { @@ -60,12 +60,15 @@ func GenerateImage(colorCode string) (fileReader io.Reader, Err error) { fileWriter = bufio.NewWriter(&buffer) ) - if v, Err := strconv.ParseInt(colorCode[len("#"):], 16, 32); Err != nil || v < 0 { + colorCodeHex := colorCode[len(colorPrefix):] + log.Println(colorCodeHex) + + if v, Err := strconv.ParseInt(colorCodeHex, 16, 32); Err != nil || v < 0 { log.Println("strconv: invalid value; not Hex") return nil, Err } - colorInfo, Err := ParseColorCode(colorCode) + colorInfo, Err := ParseColorCode(colorCodeHex) if Err != nil { log.Println(Err) return nil, Err diff --git a/src/tools.go b/src/tools.go index b72c3a4..04c2a53 100644 --- a/src/tools.go +++ b/src/tools.go @@ -51,21 +51,27 @@ func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) commandBody := strings.Split(message.Content, " ") switch commandBody[0][len(prefix):] { case "color": - { - fileData, Err := GenerateImage(commandBody[1]) + if len(commandBody) != 2 { + Err := context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") if Err != nil { - log.Println("failed to genarateImage: ", Err) - return - } - Err = context.fileSend("unkonow.jpeg", fileData) - if Err != nil { - log.Println("failed file send: ", Err) + log.Println("failed send message: ", Err) return } + return + } + fileData, Err := GenerateImage(commandBody[1]) + if Err != nil { + log.Println("failed to genarateImage: ", Err) + return + } + Err = context.fileSend("unkonow.jpeg", fileData) + if Err != nil { + log.Println("failed file send: ", Err) + return + } } default: contentText := fetchMessage(commandBody[0][len(prefix):]) - contentText := fetchMessage(commandBody[0]) Err := context.messageSend(contentText) if Err != nil { log.Println("failed send message: ", Err) From 6f16abdec68e9bf841d87c69884ea9b3e582c498 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 12:20:16 +0900 Subject: [PATCH 60/83] =?UTF-8?q?[fix]=E5=AE=9A=E5=9E=8B=E6=96=87=E3=81=AE?= =?UTF-8?q?=E7=B7=A8=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fecthMessage.go | 4 ++-- src/message.go | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/fecthMessage.go b/src/fecthMessage.go index b4b9e7a..d0ba8ea 100644 --- a/src/fecthMessage.go +++ b/src/fecthMessage.go @@ -1,12 +1,12 @@ package src -func fetchMessage (cmd string) string{ +func fetchMessage(cmd string) string { switch cmd { case "help": return helpMessage case "ping": return "Pong!" default: - return "該当するコマンドがありません`%help`を参照してください。" + return "該当するコマンドがありません。`%help`を参照してください。" } } diff --git a/src/message.go b/src/message.go index 1a1af83..f184cbf 100644 --- a/src/message.go +++ b/src/message.go @@ -2,7 +2,7 @@ package src var ( helpMessage = "```asciidoc\n" + -`= Title = + `= Title = something = Description = 表示名に絵文字を付けることでステータスを表示するBotの予定 @@ -10,8 +10,9 @@ var ( (Prefix: "%") help :: このBotの概要を説明します。 ping :: Pong!と返します。かわいいですね。 - (prefix: "#") - [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 + color :: 第二引数に以下の形式で入力: + (prefix: "#") + [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 = Source = github.com/brokenManager/change-status-go ` + "```\n" From ddc2b8e9e61f1aaf90b37da8b4f6a3410be6876c Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 12:21:09 +0900 Subject: [PATCH 61/83] =?UTF-8?q?[fix]=E4=BD=99=E5=88=86=E3=81=AA=E5=AE=9A?= =?UTF-8?q?=E6=95=B0=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tools.go b/src/tools.go index 04c2a53..277bbde 100644 --- a/src/tools.go +++ b/src/tools.go @@ -40,7 +40,6 @@ func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) session, message, } - const colorCodePrefix = "#" if message.Author.ID == session.State.User.ID { return From 3a575d735e35b6b5c73cb468c84d41fecfd753d9 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 13:50:59 +0900 Subject: [PATCH 62/83] =?UTF-8?q?[refactor]=E3=83=A2=E3=83=BC=E3=83=AB?= =?UTF-8?q?=E3=82=B9=E4=BF=A1=E5=8F=B7=E5=BE=A9=E5=8F=B7=E6=98=A8=E6=97=A5?= =?UTF-8?q?=E3=81=AE=E5=86=8D=E8=A8=AD=E8=A8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 102 ++++++++++++++++++++------------------------- src/message.go | 5 +++ src/tools.go | 29 +++++++++++++ 3 files changed, 80 insertions(+), 56 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index c42e66c..a6b391c 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -3,54 +3,54 @@ package src import ( "errors" "fmt" - "github.com/bwmarrin/discordgo" "log" "regexp" "strings" ) var alphabetTable = map[string]string{ - "a": ".-", - "b": "-...", - "c": "-.-.", - "d": "-..", - "e": ".", - "f": "..-.", - "g": "--.", - "h": "....", - "i": "..", - "j": ".---", - "k": "-.-", - "l": ".-..", - "m": "--", - "n": "-.", - "o": "---", - "p": ".--.", - "q": "--.-", - "r": ".-.", - "s": "...", - "t": "-", - "u": "..-", - "v": "...-", - "w": ".--", - "x": "-..-", - "y": "-.--", - "z": "--..", - ".": ".-.-.-", - ",": "--..--", - ":": "---...", - "?": "..--..", - "'": ".----.", - "-": "-....-", - "(": "-.--.", - ")": "-.--.-", - "/": "-..-.", - "=": "-...-.", - "+": ".-.-.", - "\"": ".-..-.", - "*": "-..-", - "@": ".--.-.", - "amend": "........", + "a": ".-", + "b": "-...", + "c": "-.-.", + "d": "-..", + "e": ".", + "f": "..-.", + "g": "--.", + "h": "....", + "i": "..", + "j": ".---", + "k": "-.-", + "l": ".-..", + "m": "--", + "n": "-.", + "o": "---", + "p": ".--.", + "q": "--.-", + "r": ".-.", + "s": "...", + "t": "-", + "u": "..-", + "v": "...-", + "w": ".--", + "x": "-..-", + "y": "-.--", + "z": "--..", + ".": ".-.-.-", + ",": "--..--", + ":": "---...", + "?": "..--..", + "'": ".----.", + "-": "-....-", + "(": "-.--.", + ")": "-.--.-", + "/": "-..-.", + "=": "-...-.", + "+": ".-.-.", + "\"": ".-..-.", + "*": "-..-", + "@": ".--.-.", + " ": "*", + " ***amend*** ": "........", } func searchTable(morseInput string) (string, bool) { @@ -81,22 +81,12 @@ func decode(sentence string) (response string, Err error) { return } -func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { - if message.Author.Bot { - return - } - sentence := strings.TrimSpace(message.Content) - decodeResult, Err := decode(sentence) +func DecodeMorse(messageContent string) (decodeResult string, Err error) { + sentence := strings.TrimSpace(messageContent) + decodeResult, Err = decode(sentence) if Err != nil { log.Println("Failed to decode:", Err) - _, Err = session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("復号に失敗しました。: %s", Err)) - if Err != nil { - log.Println("Error at ChannelMessageSend: ", Err) - } return } - _, Err = session.ChannelMessageSend(message.ChannelID, decodeResult) - if Err != nil { - log.Println("Error at ChannelMessageSend:", Err) - } + return } diff --git a/src/message.go b/src/message.go index f184cbf..e4d620c 100644 --- a/src/message.go +++ b/src/message.go @@ -13,6 +13,11 @@ var ( color :: 第二引数に以下の形式で入力: (prefix: "#") [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 + morse :: モールス暗号に関する変換をします。第2引数で動作を指定します。 + decode :: 暗号文 → 平文 + [Tips] + 現在任意の長さの空白を暗号変換に際して圧縮しています。これにより本来長い空白を入れることによって + 平文での空白を表現していたところを、*を代替しています。 = Source = github.com/brokenManager/change-status-go ` + "```\n" diff --git a/src/tools.go b/src/tools.go index 277bbde..e8349e5 100644 --- a/src/tools.go +++ b/src/tools.go @@ -1,6 +1,7 @@ package src import ( + "fmt" "github.com/bwmarrin/discordgo" "io" "log" @@ -35,6 +36,16 @@ func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) return } +func morseCodeOperation(optionalArgument string, codeType string) (answerSentence string, Err error) { + switch optionalArgument { + case "decode": + answerSentence, Err = DecodeMorse(codeType) + return + default: + return "", fmt.Errorf("Error at morseCodeOperation: No such operation.") + } +} + func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { context := messageContext{ session, @@ -68,6 +79,24 @@ func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) log.Println("failed file send: ", Err) return } + case "morse": + if len(commandBody) < 2 { + Err := context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") + if Err != nil { + log.Println("failed send message: ", Err) + return + } + return + } + contentText, Err := morseCodeOperation(commandBody[1], context.m.Content[len("%morse decode"):]) + if Err != nil { + log.Println("failed decode morse: ", Err) + return + } + Err = context.messageSend(contentText) + if Err != nil { + log.Println("failed send message: ", Err) + return } default: contentText := fetchMessage(commandBody[0][len(prefix):]) From 51d1e636494090b1a007a3bd88539269e57216c3 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 13:50:59 +0900 Subject: [PATCH 63/83] =?UTF-8?q?[refactor]=E3=83=A2=E3=83=BC=E3=83=AB?= =?UTF-8?q?=E3=82=B9=E4=BF=A1=E5=8F=B7=E5=BE=A9=E5=8F=B7=E6=A9=9F=E8=83=BD?= =?UTF-8?q?=E3=81=AE=E5=86=8D=E8=A8=AD=E8=A8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 102 ++++++++++++++++++++------------------------- src/message.go | 5 +++ src/tools.go | 29 +++++++++++++ 3 files changed, 80 insertions(+), 56 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index c42e66c..a6b391c 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -3,54 +3,54 @@ package src import ( "errors" "fmt" - "github.com/bwmarrin/discordgo" "log" "regexp" "strings" ) var alphabetTable = map[string]string{ - "a": ".-", - "b": "-...", - "c": "-.-.", - "d": "-..", - "e": ".", - "f": "..-.", - "g": "--.", - "h": "....", - "i": "..", - "j": ".---", - "k": "-.-", - "l": ".-..", - "m": "--", - "n": "-.", - "o": "---", - "p": ".--.", - "q": "--.-", - "r": ".-.", - "s": "...", - "t": "-", - "u": "..-", - "v": "...-", - "w": ".--", - "x": "-..-", - "y": "-.--", - "z": "--..", - ".": ".-.-.-", - ",": "--..--", - ":": "---...", - "?": "..--..", - "'": ".----.", - "-": "-....-", - "(": "-.--.", - ")": "-.--.-", - "/": "-..-.", - "=": "-...-.", - "+": ".-.-.", - "\"": ".-..-.", - "*": "-..-", - "@": ".--.-.", - "amend": "........", + "a": ".-", + "b": "-...", + "c": "-.-.", + "d": "-..", + "e": ".", + "f": "..-.", + "g": "--.", + "h": "....", + "i": "..", + "j": ".---", + "k": "-.-", + "l": ".-..", + "m": "--", + "n": "-.", + "o": "---", + "p": ".--.", + "q": "--.-", + "r": ".-.", + "s": "...", + "t": "-", + "u": "..-", + "v": "...-", + "w": ".--", + "x": "-..-", + "y": "-.--", + "z": "--..", + ".": ".-.-.-", + ",": "--..--", + ":": "---...", + "?": "..--..", + "'": ".----.", + "-": "-....-", + "(": "-.--.", + ")": "-.--.-", + "/": "-..-.", + "=": "-...-.", + "+": ".-.-.", + "\"": ".-..-.", + "*": "-..-", + "@": ".--.-.", + " ": "*", + " ***amend*** ": "........", } func searchTable(morseInput string) (string, bool) { @@ -81,22 +81,12 @@ func decode(sentence string) (response string, Err error) { return } -func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { - if message.Author.Bot { - return - } - sentence := strings.TrimSpace(message.Content) - decodeResult, Err := decode(sentence) +func DecodeMorse(messageContent string) (decodeResult string, Err error) { + sentence := strings.TrimSpace(messageContent) + decodeResult, Err = decode(sentence) if Err != nil { log.Println("Failed to decode:", Err) - _, Err = session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("復号に失敗しました。: %s", Err)) - if Err != nil { - log.Println("Error at ChannelMessageSend: ", Err) - } return } - _, Err = session.ChannelMessageSend(message.ChannelID, decodeResult) - if Err != nil { - log.Println("Error at ChannelMessageSend:", Err) - } + return } diff --git a/src/message.go b/src/message.go index f184cbf..e4d620c 100644 --- a/src/message.go +++ b/src/message.go @@ -13,6 +13,11 @@ var ( color :: 第二引数に以下の形式で入力: (prefix: "#") [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 + morse :: モールス暗号に関する変換をします。第2引数で動作を指定します。 + decode :: 暗号文 → 平文 + [Tips] + 現在任意の長さの空白を暗号変換に際して圧縮しています。これにより本来長い空白を入れることによって + 平文での空白を表現していたところを、*を代替しています。 = Source = github.com/brokenManager/change-status-go ` + "```\n" diff --git a/src/tools.go b/src/tools.go index 277bbde..e8349e5 100644 --- a/src/tools.go +++ b/src/tools.go @@ -1,6 +1,7 @@ package src import ( + "fmt" "github.com/bwmarrin/discordgo" "io" "log" @@ -35,6 +36,16 @@ func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) return } +func morseCodeOperation(optionalArgument string, codeType string) (answerSentence string, Err error) { + switch optionalArgument { + case "decode": + answerSentence, Err = DecodeMorse(codeType) + return + default: + return "", fmt.Errorf("Error at morseCodeOperation: No such operation.") + } +} + func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { context := messageContext{ session, @@ -68,6 +79,24 @@ func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) log.Println("failed file send: ", Err) return } + case "morse": + if len(commandBody) < 2 { + Err := context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") + if Err != nil { + log.Println("failed send message: ", Err) + return + } + return + } + contentText, Err := morseCodeOperation(commandBody[1], context.m.Content[len("%morse decode"):]) + if Err != nil { + log.Println("failed decode morse: ", Err) + return + } + Err = context.messageSend(contentText) + if Err != nil { + log.Println("failed send message: ", Err) + return } default: contentText := fetchMessage(commandBody[0][len(prefix):]) From aeb0aff6c0db40ed56e285983e9fa9b75e79c3cc Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 13:50:59 +0900 Subject: [PATCH 64/83] =?UTF-8?q?Revert=20"[refactor]=E3=83=A2=E3=83=BC?= =?UTF-8?q?=E3=83=AB=E3=82=B9=E4=BF=A1=E5=8F=B7=E5=BE=A9=E5=8F=B7=E6=98=A8?= =?UTF-8?q?=E6=97=A5=E3=81=AE=E5=86=8D=E8=A8=AD=E8=A8=88"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 3a575d735e35b6b5c73cb468c84d41fecfd753d9. --- src/decodeMorse.go | 102 +++++++++++++++++++++++++-------------------- src/message.go | 5 --- src/tools.go | 29 ------------- 3 files changed, 56 insertions(+), 80 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index a6b391c..c42e66c 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -3,54 +3,54 @@ package src import ( "errors" "fmt" + "github.com/bwmarrin/discordgo" "log" "regexp" "strings" ) var alphabetTable = map[string]string{ - "a": ".-", - "b": "-...", - "c": "-.-.", - "d": "-..", - "e": ".", - "f": "..-.", - "g": "--.", - "h": "....", - "i": "..", - "j": ".---", - "k": "-.-", - "l": ".-..", - "m": "--", - "n": "-.", - "o": "---", - "p": ".--.", - "q": "--.-", - "r": ".-.", - "s": "...", - "t": "-", - "u": "..-", - "v": "...-", - "w": ".--", - "x": "-..-", - "y": "-.--", - "z": "--..", - ".": ".-.-.-", - ",": "--..--", - ":": "---...", - "?": "..--..", - "'": ".----.", - "-": "-....-", - "(": "-.--.", - ")": "-.--.-", - "/": "-..-.", - "=": "-...-.", - "+": ".-.-.", - "\"": ".-..-.", - "*": "-..-", - "@": ".--.-.", - " ": "*", - " ***amend*** ": "........", + "a": ".-", + "b": "-...", + "c": "-.-.", + "d": "-..", + "e": ".", + "f": "..-.", + "g": "--.", + "h": "....", + "i": "..", + "j": ".---", + "k": "-.-", + "l": ".-..", + "m": "--", + "n": "-.", + "o": "---", + "p": ".--.", + "q": "--.-", + "r": ".-.", + "s": "...", + "t": "-", + "u": "..-", + "v": "...-", + "w": ".--", + "x": "-..-", + "y": "-.--", + "z": "--..", + ".": ".-.-.-", + ",": "--..--", + ":": "---...", + "?": "..--..", + "'": ".----.", + "-": "-....-", + "(": "-.--.", + ")": "-.--.-", + "/": "-..-.", + "=": "-...-.", + "+": ".-.-.", + "\"": ".-..-.", + "*": "-..-", + "@": ".--.-.", + "amend": "........", } func searchTable(morseInput string) (string, bool) { @@ -81,12 +81,22 @@ func decode(sentence string) (response string, Err error) { return } -func DecodeMorse(messageContent string) (decodeResult string, Err error) { - sentence := strings.TrimSpace(messageContent) - decodeResult, Err = decode(sentence) +func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { + if message.Author.Bot { + return + } + sentence := strings.TrimSpace(message.Content) + decodeResult, Err := decode(sentence) if Err != nil { log.Println("Failed to decode:", Err) + _, Err = session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("復号に失敗しました。: %s", Err)) + if Err != nil { + log.Println("Error at ChannelMessageSend: ", Err) + } return } - return + _, Err = session.ChannelMessageSend(message.ChannelID, decodeResult) + if Err != nil { + log.Println("Error at ChannelMessageSend:", Err) + } } diff --git a/src/message.go b/src/message.go index e4d620c..f184cbf 100644 --- a/src/message.go +++ b/src/message.go @@ -13,11 +13,6 @@ var ( color :: 第二引数に以下の形式で入力: (prefix: "#") [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 - morse :: モールス暗号に関する変換をします。第2引数で動作を指定します。 - decode :: 暗号文 → 平文 - [Tips] - 現在任意の長さの空白を暗号変換に際して圧縮しています。これにより本来長い空白を入れることによって - 平文での空白を表現していたところを、*を代替しています。 = Source = github.com/brokenManager/change-status-go ` + "```\n" diff --git a/src/tools.go b/src/tools.go index e8349e5..277bbde 100644 --- a/src/tools.go +++ b/src/tools.go @@ -1,7 +1,6 @@ package src import ( - "fmt" "github.com/bwmarrin/discordgo" "io" "log" @@ -36,16 +35,6 @@ func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) return } -func morseCodeOperation(optionalArgument string, codeType string) (answerSentence string, Err error) { - switch optionalArgument { - case "decode": - answerSentence, Err = DecodeMorse(codeType) - return - default: - return "", fmt.Errorf("Error at morseCodeOperation: No such operation.") - } -} - func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { context := messageContext{ session, @@ -79,24 +68,6 @@ func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) log.Println("failed file send: ", Err) return } - case "morse": - if len(commandBody) < 2 { - Err := context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") - if Err != nil { - log.Println("failed send message: ", Err) - return - } - return - } - contentText, Err := morseCodeOperation(commandBody[1], context.m.Content[len("%morse decode"):]) - if Err != nil { - log.Println("failed decode morse: ", Err) - return - } - Err = context.messageSend(contentText) - if Err != nil { - log.Println("failed send message: ", Err) - return } default: contentText := fetchMessage(commandBody[0][len(prefix):]) From 17298f2cbe2576aea1767493ae90b8fd2e7062b6 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 13:50:59 +0900 Subject: [PATCH 65/83] =?UTF-8?q?[refactor]=E3=83=A2=E3=83=BC=E3=83=AB?= =?UTF-8?q?=E3=82=B9=E4=BF=A1=E5=8F=B7=E5=BE=A9=E5=8F=B7=E6=A9=9F=E8=83=BD?= =?UTF-8?q?=E3=81=AE=E5=86=8D=E8=A8=AD=E8=A8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 102 ++++++++++++++++++++------------------------- src/message.go | 5 +++ src/tools.go | 29 +++++++++++++ 3 files changed, 80 insertions(+), 56 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index c42e66c..a6b391c 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -3,54 +3,54 @@ package src import ( "errors" "fmt" - "github.com/bwmarrin/discordgo" "log" "regexp" "strings" ) var alphabetTable = map[string]string{ - "a": ".-", - "b": "-...", - "c": "-.-.", - "d": "-..", - "e": ".", - "f": "..-.", - "g": "--.", - "h": "....", - "i": "..", - "j": ".---", - "k": "-.-", - "l": ".-..", - "m": "--", - "n": "-.", - "o": "---", - "p": ".--.", - "q": "--.-", - "r": ".-.", - "s": "...", - "t": "-", - "u": "..-", - "v": "...-", - "w": ".--", - "x": "-..-", - "y": "-.--", - "z": "--..", - ".": ".-.-.-", - ",": "--..--", - ":": "---...", - "?": "..--..", - "'": ".----.", - "-": "-....-", - "(": "-.--.", - ")": "-.--.-", - "/": "-..-.", - "=": "-...-.", - "+": ".-.-.", - "\"": ".-..-.", - "*": "-..-", - "@": ".--.-.", - "amend": "........", + "a": ".-", + "b": "-...", + "c": "-.-.", + "d": "-..", + "e": ".", + "f": "..-.", + "g": "--.", + "h": "....", + "i": "..", + "j": ".---", + "k": "-.-", + "l": ".-..", + "m": "--", + "n": "-.", + "o": "---", + "p": ".--.", + "q": "--.-", + "r": ".-.", + "s": "...", + "t": "-", + "u": "..-", + "v": "...-", + "w": ".--", + "x": "-..-", + "y": "-.--", + "z": "--..", + ".": ".-.-.-", + ",": "--..--", + ":": "---...", + "?": "..--..", + "'": ".----.", + "-": "-....-", + "(": "-.--.", + ")": "-.--.-", + "/": "-..-.", + "=": "-...-.", + "+": ".-.-.", + "\"": ".-..-.", + "*": "-..-", + "@": ".--.-.", + " ": "*", + " ***amend*** ": "........", } func searchTable(morseInput string) (string, bool) { @@ -81,22 +81,12 @@ func decode(sentence string) (response string, Err error) { return } -func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { - if message.Author.Bot { - return - } - sentence := strings.TrimSpace(message.Content) - decodeResult, Err := decode(sentence) +func DecodeMorse(messageContent string) (decodeResult string, Err error) { + sentence := strings.TrimSpace(messageContent) + decodeResult, Err = decode(sentence) if Err != nil { log.Println("Failed to decode:", Err) - _, Err = session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("復号に失敗しました。: %s", Err)) - if Err != nil { - log.Println("Error at ChannelMessageSend: ", Err) - } return } - _, Err = session.ChannelMessageSend(message.ChannelID, decodeResult) - if Err != nil { - log.Println("Error at ChannelMessageSend:", Err) - } + return } diff --git a/src/message.go b/src/message.go index f184cbf..e4d620c 100644 --- a/src/message.go +++ b/src/message.go @@ -13,6 +13,11 @@ var ( color :: 第二引数に以下の形式で入力: (prefix: "#") [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 + morse :: モールス暗号に関する変換をします。第2引数で動作を指定します。 + decode :: 暗号文 → 平文 + [Tips] + 現在任意の長さの空白を暗号変換に際して圧縮しています。これにより本来長い空白を入れることによって + 平文での空白を表現していたところを、*を代替しています。 = Source = github.com/brokenManager/change-status-go ` + "```\n" diff --git a/src/tools.go b/src/tools.go index 277bbde..e8349e5 100644 --- a/src/tools.go +++ b/src/tools.go @@ -1,6 +1,7 @@ package src import ( + "fmt" "github.com/bwmarrin/discordgo" "io" "log" @@ -35,6 +36,16 @@ func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) return } +func morseCodeOperation(optionalArgument string, codeType string) (answerSentence string, Err error) { + switch optionalArgument { + case "decode": + answerSentence, Err = DecodeMorse(codeType) + return + default: + return "", fmt.Errorf("Error at morseCodeOperation: No such operation.") + } +} + func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { context := messageContext{ session, @@ -68,6 +79,24 @@ func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) log.Println("failed file send: ", Err) return } + case "morse": + if len(commandBody) < 2 { + Err := context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") + if Err != nil { + log.Println("failed send message: ", Err) + return + } + return + } + contentText, Err := morseCodeOperation(commandBody[1], context.m.Content[len("%morse decode"):]) + if Err != nil { + log.Println("failed decode morse: ", Err) + return + } + Err = context.messageSend(contentText) + if Err != nil { + log.Println("failed send message: ", Err) + return } default: contentText := fetchMessage(commandBody[0][len(prefix):]) From f000793b7ec5a2a20a1690338daf79a2deb086ea Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 18 May 2020 14:22:49 +0900 Subject: [PATCH 66/83] =?UTF-8?q?[fix]=E3=81=8B=E3=81=B6=E3=81=A3=E3=81=A6?= =?UTF-8?q?=E3=81=84=E3=82=8B=E7=AC=A6=E5=8F=B7=E3=81=AE=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=83=BB=E6=BA=96=E6=8B=A0=E3=81=99=E3=82=8B=E3=82=B5=E3=82=A4?= =?UTF-8?q?=E3=83=88=E3=81=AE=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 参考サイトの信ぴょう性が取れないので修正をします。符号の対応表は、英語版Wikipediaを参照します --- src/decodeMorse.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index a6b391c..9f16cf1 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -35,21 +35,32 @@ var alphabetTable = map[string]string{ "x": "-..-", "y": "-.--", "z": "--..", + "0": "-----", + "1": ".----", + "2": "..---", + "3": "...--", + "4": "....-", + "5": ".....", + "6": "-....", + "7": "--...", + "8": "---..", + "9": "----.", ".": ".-.-.-", ",": "--..--", - ":": "---...", "?": "..--..", "'": ".----.", - "-": "-....-", - "(": "-.--.", - ")": "-.--.-", + "!": "-.-.--", "/": "-..-.", - "=": "-...-.", + "&": ".-...", + ":": "---...", + ";": "-.-.-.", + "=": "-...-", "+": ".-.-.", + "-": "-....-", + "_": "..--.-", "\"": ".-..-.", - "*": "-..-", + "$": "...-..-", "@": ".--.-.", - " ": "*", " ***amend*** ": "........", } From e3e74d4be469f9dcb5941a9aaf67fb28e34b3891 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Tue, 19 May 2020 01:01:09 +0900 Subject: [PATCH 67/83] =?UTF-8?q?[refactor]=E6=8C=87=E6=91=98=E7=82=B9?= =?UTF-8?q?=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ファイル大きい気もするので分けるのも検討中 --- src/decodeMorse.go | 3 ++- src/tools.go | 60 ++++++++++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index 9f16cf1..bc13981 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -62,6 +62,7 @@ var alphabetTable = map[string]string{ "$": "...-..-", "@": ".--.-.", " ***amend*** ": "........", + " ": "*", } func searchTable(morseInput string) (string, bool) { @@ -75,7 +76,7 @@ func searchTable(morseInput string) (string, bool) { } func decode(sentence string) (response string, Err error) { - reg := regexp.MustCompile(`[ \t]+`) + reg := regexp.MustCompile(`\s+`) sentence = reg.ReplaceAllString(sentence, " ") log.Printf(sentence) for _, part := range strings.Split(sentence, " ") { diff --git a/src/tools.go b/src/tools.go index e8349e5..9e44ad9 100644 --- a/src/tools.go +++ b/src/tools.go @@ -36,8 +36,8 @@ func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) return } -func morseCodeOperation(optionalArgument string, codeType string) (answerSentence string, Err error) { - switch optionalArgument { +func morseCodeOperation(mode string, codeType string) (answerSentence string, Err error) { + switch mode { case "decode": answerSentence, Err = DecodeMorse(codeType) return @@ -46,8 +46,34 @@ func morseCodeOperation(optionalArgument string, codeType string) (answerSentenc } } +func morseAction(command string, codeSentence string, context messageContext) { + contentText, Err := morseCodeOperation(command, codeSentence) + if Err != nil { + log.Println("failed decode morse: ", Err) + return + } + Err = context.messageSend(contentText) + if Err != nil { + log.Println("failed send message: ", Err) + return + } +} + +func colorAction(command string, context messageContext) { + fileData, Err := GenerateImage(command) + if Err != nil { + log.Println("failed to genarateImage: ", Err) + return + } + Err = context.fileSend("unkonow.jpeg", fileData) + if Err != nil { + log.Println("failed file send: ", Err) + return + } +} + func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { - context := messageContext{ + Context := messageContext{ session, message, } @@ -62,45 +88,27 @@ func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) switch commandBody[0][len(prefix):] { case "color": if len(commandBody) != 2 { - Err := context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") + Err := Context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") if Err != nil { log.Println("failed send message: ", Err) return } return } - fileData, Err := GenerateImage(commandBody[1]) - if Err != nil { - log.Println("failed to genarateImage: ", Err) - return - } - Err = context.fileSend("unkonow.jpeg", fileData) - if Err != nil { - log.Println("failed file send: ", Err) - return - } + colorAction(commandBody[1], Context) case "morse": if len(commandBody) < 2 { - Err := context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") + Err := Context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") if Err != nil { log.Println("failed send message: ", Err) return } return } - contentText, Err := morseCodeOperation(commandBody[1], context.m.Content[len("%morse decode"):]) - if Err != nil { - log.Println("failed decode morse: ", Err) - return - } - Err = context.messageSend(contentText) - if Err != nil { - log.Println("failed send message: ", Err) - return - } + morseAction(commandBody[1], Context.m.Content[len("%morse decode"):], Context) default: contentText := fetchMessage(commandBody[0][len(prefix):]) - Err := context.messageSend(contentText) + Err := Context.messageSend(contentText) if Err != nil { log.Println("failed send message: ", Err) return From db901208b3b4d89a14a293ec5638757503a4743f Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sun, 14 Jun 2020 22:21:21 +0900 Subject: [PATCH 68/83] =?UTF-8?q?[adjust]=E9=96=A2=E6=95=B0=E3=82=92?= =?UTF-8?q?=E6=AD=A3=E3=81=97=E3=81=84=E4=BD=8D=E7=BD=AE=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 23 +++++++++++++++++++++++ src/sendImage.go | 13 +++++++++++++ src/tools.go | 41 +++-------------------------------------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index bc13981..736ddbf 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -102,3 +102,26 @@ func DecodeMorse(messageContent string) (decodeResult string, Err error) { } return } + +func morseCodeOperation(mode string, codeType string) (answerSentence string, Err error) { + switch mode { + case "decode": + answerSentence, Err = DecodeMorse(codeType) + return + default: + return "", fmt.Errorf("Error at morseCodeOperation: No such operation.") + } +} + +func morseAction(command string, codeSentence string, context messageContext) { + contentText, Err := morseCodeOperation(command, codeSentence) + if Err != nil { + log.Println("failed decode morse: ", Err) + return + } + Err = context.messageSend(contentText) + if Err != nil { + log.Println("failed send message: ", Err) + return + } +} diff --git a/src/sendImage.go b/src/sendImage.go index b93360f..c6c968b 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -94,3 +94,16 @@ func GenerateImage(colorCode string) (fileReader io.Reader, Err error) { log.Println("generatedImage: process ended") return } + +func colorAction(command string, context messageContext) { + fileData, Err := GenerateImage(command) + if Err != nil { + log.Println("failed to genarateImage: ", Err) + return + } + Err = context.fileSend("unkonow.jpeg", fileData) + if Err != nil { + log.Println("failed file send: ", Err) + return + } +} diff --git a/src/tools.go b/src/tools.go index 9e44ad9..df0b733 100644 --- a/src/tools.go +++ b/src/tools.go @@ -1,11 +1,11 @@ package src import ( - "fmt" - "github.com/bwmarrin/discordgo" "io" "log" "strings" + + "github.com/bwmarrin/discordgo" ) const ( @@ -36,42 +36,7 @@ func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) return } -func morseCodeOperation(mode string, codeType string) (answerSentence string, Err error) { - switch mode { - case "decode": - answerSentence, Err = DecodeMorse(codeType) - return - default: - return "", fmt.Errorf("Error at morseCodeOperation: No such operation.") - } -} - -func morseAction(command string, codeSentence string, context messageContext) { - contentText, Err := morseCodeOperation(command, codeSentence) - if Err != nil { - log.Println("failed decode morse: ", Err) - return - } - Err = context.messageSend(contentText) - if Err != nil { - log.Println("failed send message: ", Err) - return - } -} - -func colorAction(command string, context messageContext) { - fileData, Err := GenerateImage(command) - if Err != nil { - log.Println("failed to genarateImage: ", Err) - return - } - Err = context.fileSend("unkonow.jpeg", fileData) - if Err != nil { - log.Println("failed file send: ", Err) - return - } -} - +func MessageCreate(session *discordgo.Session, message *discordgo.Message) { func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { Context := messageContext{ session, From 248b66aa4886a62a78fabedc47a9829e8ead3cb1 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sun, 14 Jun 2020 22:22:10 +0900 Subject: [PATCH 69/83] =?UTF-8?q?[fix]MessageCreate=E2=86=92Message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tools.go b/src/tools.go index df0b733..2677228 100644 --- a/src/tools.go +++ b/src/tools.go @@ -15,7 +15,7 @@ const ( type messageContext struct { s *discordgo.Session - m *discordgo.MessageCreate + m *discordgo.Message } func (cxt *messageContext) messageSend(message string) (Err error) { @@ -37,7 +37,6 @@ func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) } func MessageCreate(session *discordgo.Session, message *discordgo.Message) { -func MessageCreate(session *discordgo.Session, message *discordgo.MessageCreate) { Context := messageContext{ session, message, From 563c544487df563cdf5ad787892261f42e83ab5a Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Sun, 14 Jun 2020 23:25:55 +0900 Subject: [PATCH 70/83] =?UTF-8?q?[fix]=E5=A4=89=E6=95=B0=E5=90=8D=E3=81=8A?= =?UTF-8?q?=E3=81=8B=E3=81=97=E3=81=84=E3=82=84=E3=82=8D=E3=81=AA=E3=82=93?= =?UTF-8?q?=E3=81=A7codeType=E3=82=84=E3=81=AD=E3=82=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/decodeMorse.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decodeMorse.go b/src/decodeMorse.go index 736ddbf..fbb6f4a 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -103,10 +103,10 @@ func DecodeMorse(messageContent string) (decodeResult string, Err error) { return } -func morseCodeOperation(mode string, codeType string) (answerSentence string, Err error) { +func morseCodeOperation(mode string, content string) (answerSentence string, Err error) { switch mode { case "decode": - answerSentence, Err = DecodeMorse(codeType) + answerSentence, Err = DecodeMorse(content) return default: return "", fmt.Errorf("Error at morseCodeOperation: No such operation.") From 16c1f204ba731df7f94242e333aa189328d93824 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 6 Jul 2020 13:54:00 +0900 Subject: [PATCH 71/83] =?UTF-8?q?[fix]=E7=92=B0=E5=A2=83=E5=A4=89=E6=95=B0?= =?UTF-8?q?=E3=81=A7=E8=AA=AD=E3=81=BF=E8=BE=BC=E3=82=80=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=97=E3=81=9F(=E3=81=AF=E3=81=9A)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- main.go | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 12a6757..4bd8ebc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.exe + .idea -secret vendor diff --git a/main.go b/main.go index b51e8ca..3984ace 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,18 @@ package main import ( - "change-status-go/secret" command "change-status-go/src" "fmt" - "github.com/bwmarrin/discordgo" "os" "os/signal" "syscall" + + "github.com/bwmarrin/discordgo" ) func main() { discordBrain, err := discordgo.New() - discordBrain.Token = secret.Token + discordBrain.Token = loadToken() if err != nil { fmt.Println("Error logging in") fmt.Println(err) @@ -32,3 +32,8 @@ func main() { <-sc return } + +func loadToken() (token string) { + token = os.Getenv("DISCORD_TOKEN") + return +} From f612eb170e03f7385ffc9e2dcaf9de3edf246d81 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 6 Jul 2020 14:06:23 +0900 Subject: [PATCH 72/83] =?UTF-8?q?[fix]named=20return=20value=E3=82=92?= =?UTF-8?q?=E6=B6=88=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 3984ace..3115c30 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ func main() { return } -func loadToken() (token string) { - token = os.Getenv("DISCORD_TOKEN") - return +func loadToken() string { + token := os.Getenv("DISCORD_TOKEN") + return token } From 157381b066cfa8fc024824dd031ad17a7891ac91 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 6 Jul 2020 14:06:46 +0900 Subject: [PATCH 73/83] =?UTF-8?q?[fix]=E3=83=88=E3=83=BC=E3=82=AF=E3=83=B3?= =?UTF-8?q?=E3=81=8C=E5=AD=98=E5=9C=A8=E3=81=97=E3=81=AA=E3=81=84=E5=A0=B4?= =?UTF-8?q?=E5=90=88=E3=81=AE=E5=87=A6=E7=90=86=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 3115c30..61e34e3 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( command "change-status-go/src" "fmt" + "log" "os" "os/signal" "syscall" @@ -12,7 +13,13 @@ import ( func main() { discordBrain, err := discordgo.New() - discordBrain.Token = loadToken() + + discordToken := loadToken() + if discordToken == "" { + log.Println("Error: no discord token exists.") + return + } + discordBrain.Token = discordToken if err != nil { fmt.Println("Error logging in") fmt.Println(err) From 80fa91628d0b68ae1189497e321cbb0474425c97 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 6 Jul 2020 14:15:30 +0900 Subject: [PATCH 74/83] =?UTF-8?q?[fix]=E5=8D=98=E7=B4=94=E3=81=ABreturn?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.go b/main.go index 61e34e3..4cfeb5b 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,5 @@ func main() { } func loadToken() string { - token := os.Getenv("DISCORD_TOKEN") - return token + return os.Getenv("DISCORD_TOKEN") } From 6173dcf46ee7cf06168fdfcc3fc64c50d0088916 Mon Sep 17 00:00:00 2001 From: kawaemon <34652535+kawaemon@users.noreply.github.com> Date: Mon, 6 Jul 2020 17:59:38 +0900 Subject: [PATCH 75/83] =?UTF-8?q?Dockerfile=E3=81=A8workflow=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/docker.yaml | 32 ++++++++++++++++++++++++++++++++ Dockerfile | 11 +++++++++++ 2 files changed, 43 insertions(+) create mode 100644 .github/workflows/docker.yaml create mode 100644 Dockerfile diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 0000000..015ddc5 --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,32 @@ +name: Docker + +on: + push: + branches: + - master + - refactor + +env: + IMAGE_NAME: change-status-go + +jobs: + push: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Build an image with a tag + run: docker build -t app . + + - name: Log into the registry + run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin + + - name: Push the image + run: | + IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME + VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') + [ "$VERSION" == "master" ] && VERSION=latest + docker tag app $IMAGE_ID:$VERSION + docker push $IMAGE_ID:$VERSION \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0f362ab --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.14.4-alpine3.12 as build +RUN mkdir /src +COPY . /src +WORKDIR /src +RUN go build -o change-status-go + + +FROM alpine:3.12 +COPY --from=build /src/change-status-go /bot/ +WORKDIR /bot/ +ENTRYPOINT [ "/bot/change-status-go" ] From a4de3a02534642d671723c67059fa1b428d15880 Mon Sep 17 00:00:00 2001 From: kawaemon <34652535+kawaemon@users.noreply.github.com> Date: Mon, 6 Jul 2020 18:01:26 +0900 Subject: [PATCH 76/83] LF --- .github/workflows/docker.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index 015ddc5..38d9f0e 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -29,4 +29,4 @@ jobs: [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') [ "$VERSION" == "master" ] && VERSION=latest docker tag app $IMAGE_ID:$VERSION - docker push $IMAGE_ID:$VERSION \ No newline at end of file + docker push $IMAGE_ID:$VERSION From 7370d5de670838d4b65a6735b6d82209a681db45 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 6 Jul 2020 20:48:08 +0900 Subject: [PATCH 77/83] =?UTF-8?q?[fix]=E3=82=A8=E3=83=A9=E3=83=BC=E6=96=87?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E3=81=AE=E6=95=B4=E5=BD=A2=E3=80=81=E8=8B=A5?= =?UTF-8?q?=E5=B9=B2=E3=81=AE=E3=82=A8=E3=83=A9=E3=83=BC=E6=96=87=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 4cfeb5b..bef30ca 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,10 @@ import ( func main() { discordBrain, err := discordgo.New() + if err != nil { + fmt.Println("Error: discordgo.New(): something wrong.") + fmt.Println(err) + } discordToken := loadToken() if discordToken == "" { @@ -20,10 +24,6 @@ func main() { return } discordBrain.Token = discordToken - if err != nil { - fmt.Println("Error logging in") - fmt.Println(err) - } discordBrain.AddHandler(command.MessageCreate) From 2a1878688240bfade8d049889528585650ad72c3 Mon Sep 17 00:00:00 2001 From: raiga0310 Date: Mon, 6 Jul 2020 20:48:30 +0900 Subject: [PATCH 78/83] =?UTF-8?q?[fix]=E5=88=A4=E5=AE=9A=E6=96=87=E3=81=AE?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=BC=8F=E3=81=AE=E7=B5=B1=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools.go b/src/tools.go index 2677228..5eb110f 100644 --- a/src/tools.go +++ b/src/tools.go @@ -61,7 +61,7 @@ func MessageCreate(session *discordgo.Session, message *discordgo.Message) { } colorAction(commandBody[1], Context) case "morse": - if len(commandBody) < 2 { + if len(commandBody) != 2 { Err := Context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") if Err != nil { log.Println("failed send message: ", Err) From bd8aa1de2f2713d0f8f4f797c5794dc6b4a7be7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=82=AC=E3=83=BC?= <44018535+raiga0310@users.noreply.github.com> Date: Tue, 7 Jul 2020 00:04:07 +0900 Subject: [PATCH 79/83] Revert "Refactor" --- .gitignore | 2 +- main.go | 21 ++----- src/decodeMorse.go | 135 +++++++++++++++++++------------------------- src/fecthMessage.go | 4 +- src/message.go | 12 +--- src/sendImage.go | 56 +++++++++--------- src/tools.go | 87 ++++++---------------------- 7 files changed, 116 insertions(+), 201 deletions(-) diff --git a/.gitignore b/.gitignore index 4bd8ebc..12a6757 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.exe - .idea +secret vendor diff --git a/main.go b/main.go index bef30ca..3d22d0a 100644 --- a/main.go +++ b/main.go @@ -1,31 +1,26 @@ package main import ( + "change-status-go/secret" command "change-status-go/src" "fmt" - "log" + "github.com/bwmarrin/discordgo" "os" "os/signal" "syscall" - - "github.com/bwmarrin/discordgo" ) func main() { discordBrain, err := discordgo.New() + discordBrain.Token = secret.Token if err != nil { - fmt.Println("Error: discordgo.New(): something wrong.") + fmt.Println("Error logging in") fmt.Println(err) } - discordToken := loadToken() - if discordToken == "" { - log.Println("Error: no discord token exists.") - return - } - discordBrain.Token = discordToken - discordBrain.AddHandler(command.MessageCreate) + discordBrain.AddHandler(command.GenerateImage) + discordBrain.AddHandler(command.DecodeMorse) err = discordBrain.Open() if err != nil { @@ -39,7 +34,3 @@ func main() { <-sc return } - -func loadToken() string { - return os.Getenv("DISCORD_TOKEN") -} diff --git a/src/decodeMorse.go b/src/decodeMorse.go index fbb6f4a..c736a33 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -3,66 +3,57 @@ package src import ( "errors" "fmt" + "github.com/bwmarrin/discordgo" "log" "regexp" "strings" ) +const ( + prefixMorseDecode = "%decode" +) + var alphabetTable = map[string]string{ - "a": ".-", - "b": "-...", - "c": "-.-.", - "d": "-..", - "e": ".", - "f": "..-.", - "g": "--.", - "h": "....", - "i": "..", - "j": ".---", - "k": "-.-", - "l": ".-..", - "m": "--", - "n": "-.", - "o": "---", - "p": ".--.", - "q": "--.-", - "r": ".-.", - "s": "...", - "t": "-", - "u": "..-", - "v": "...-", - "w": ".--", - "x": "-..-", - "y": "-.--", - "z": "--..", - "0": "-----", - "1": ".----", - "2": "..---", - "3": "...--", - "4": "....-", - "5": ".....", - "6": "-....", - "7": "--...", - "8": "---..", - "9": "----.", - ".": ".-.-.-", - ",": "--..--", - "?": "..--..", - "'": ".----.", - "!": "-.-.--", - "/": "-..-.", - "&": ".-...", - ":": "---...", - ";": "-.-.-.", - "=": "-...-", - "+": ".-.-.", - "-": "-....-", - "_": "..--.-", - "\"": ".-..-.", - "$": "...-..-", - "@": ".--.-.", - " ***amend*** ": "........", - " ": "*", + "a": ".-", + "b": "-...", + "c": "-.-.", + "d": "-..", + "e": ".", + "f": "..-.", + "g": "--.", + "h": "....", + "i": "..", + "j": ".---", + "k": "-.-", + "l": ".-..", + "m": "--", + "n": "-.", + "o": "---", + "p": ".--.", + "q": "--.-", + "r": ".-.", + "s": "...", + "t": "-", + "u": "..-", + "v": "...-", + "w": "-..-", + "x": "-..-", + "z": "--..", + ".": ".--.-.", + ",": "--..--", + ":": "---...", + "?": "..--..", + "'": ".----.", + "-": "-....-", + "(": "-.--.", + ")": "-.--.-", + "/": "-..-.", + "=": "-...-.", + "+": ".-.-.-", + "\"": ".-..-.", + "*": "-..-", + "@": ".--.-.", + "amend": "........", } func searchTable(morseInput string) (string, bool) { @@ -76,7 +67,7 @@ func searchTable(morseInput string) (string, bool) { } func decode(sentence string) (response string, Err error) { - reg := regexp.MustCompile(`\s+`) + reg := regexp.MustCompile(`[ \t]+`) sentence = reg.ReplaceAllString(sentence, " ") log.Printf(sentence) for _, part := range strings.Split(sentence, " ") { @@ -93,35 +84,25 @@ func decode(sentence string) (response string, Err error) { return } -func DecodeMorse(messageContent string) (decodeResult string, Err error) { - sentence := strings.TrimSpace(messageContent) - decodeResult, Err = decode(sentence) - if Err != nil { - log.Println("Failed to decode:", Err) +func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { + if message.Author.Bot { return } - return -} - -func morseCodeOperation(mode string, content string) (answerSentence string, Err error) { - switch mode { - case "decode": - answerSentence, Err = DecodeMorse(content) + if !strings.HasPrefix(message.Content, prefixMorseDecode) { return - default: - return "", fmt.Errorf("Error at morseCodeOperation: No such operation.") } -} - -func morseAction(command string, codeSentence string, context messageContext) { - contentText, Err := morseCodeOperation(command, codeSentence) + sentence := strings.TrimSpace(message.Content[len(prefixMorseDecode):]) + decodeResult, Err := decode(sentence) if Err != nil { - log.Println("failed decode morse: ", Err) + log.Println("Failed to decode:", Err) + _, Err = session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("復号に失敗しました。: %s", Err)) + if Err != nil { + log.Println("Error at ChannelMessageSend: ", Err) + } return } - Err = context.messageSend(contentText) + _, Err = session.ChannelMessageSend(message.ChannelID, decodeResult) if Err != nil { - log.Println("failed send message: ", Err) - return + log.Println("Error at ChannelMessageSend:", Err) } } diff --git a/src/fecthMessage.go b/src/fecthMessage.go index d0ba8ea..b4b9e7a 100644 --- a/src/fecthMessage.go +++ b/src/fecthMessage.go @@ -1,12 +1,12 @@ package src -func fetchMessage(cmd string) string { +func fetchMessage (cmd string) string{ switch cmd { case "help": return helpMessage case "ping": return "Pong!" default: - return "該当するコマンドがありません。`%help`を参照してください。" + return "該当するコマンドがありません`%help`を参照してください。" } } diff --git a/src/message.go b/src/message.go index e4d620c..1a1af83 100644 --- a/src/message.go +++ b/src/message.go @@ -2,7 +2,7 @@ package src var ( helpMessage = "```asciidoc\n" + - `= Title = +`= Title = something = Description = 表示名に絵文字を付けることでステータスを表示するBotの予定 @@ -10,14 +10,8 @@ var ( (Prefix: "%") help :: このBotの概要を説明します。 ping :: Pong!と返します。かわいいですね。 - color :: 第二引数に以下の形式で入力: - (prefix: "#") - [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 - morse :: モールス暗号に関する変換をします。第2引数で動作を指定します。 - decode :: 暗号文 → 平文 - [Tips] - 現在任意の長さの空白を暗号変換に際して圧縮しています。これにより本来長い空白を入れることによって - 平文での空白を表現していたところを、*を代替しています。 + (prefix: "#") + [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 = Source = github.com/brokenManager/change-status-go ` + "```\n" diff --git a/src/sendImage.go b/src/sendImage.go index c6c968b..6a17722 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -4,12 +4,13 @@ import ( "bufio" "bytes" "fmt" + "github.com/bwmarrin/discordgo" "image" "image/color" "image/jpeg" - "io" "log" "strconv" + "strings" ) func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { @@ -38,13 +39,13 @@ func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { func genImage(colorInfo color.RGBA) *image.RGBA { const ( - startX = 0 + statrX = 0 startY = 0 width = 40 height = 30 ) - img := image.NewRGBA(image.Rect(startX, startY, width, height)) + img := image.NewRGBA(image.Rect(statrX, startY, width, height)) for y := img.Rect.Min.Y; y < img.Rect.Max.Y; y++ { for x := img.Rect.Min.X; x < img.Rect.Max.X; x++ { @@ -54,29 +55,40 @@ func genImage(colorInfo color.RGBA) *image.RGBA { return img } -func GenerateImage(colorCode string) (fileReader io.Reader, Err error) { - var ( - buffer bytes.Buffer - fileWriter = bufio.NewWriter(&buffer) - ) +func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) { - colorCodeHex := colorCode[len(colorPrefix):] - log.Println(colorCodeHex) + content := strings.TrimSpace(message.Content) + if !strings.HasPrefix(content, "#") { + return + } - if v, Err := strconv.ParseInt(colorCodeHex, 16, 32); Err != nil || v < 0 { + if v, Err := strconv.ParseInt(content[1:], 16, 32); Err != nil || v < 0 { log.Println("strconv: invalid value; not Hex") - return nil, Err + return + } + + colorCode := content[len("#"):] + if len(colorCode) != 6 { + log.Println("generateImage: len(colorCode) must be just 6") + _, Err := session.ChannelMessageSend(message.ChannelID, "不正な値です。形式は16進のカラーコードである必要があります。") + if Err != nil { + log.Println(Err) + } + return } - colorInfo, Err := ParseColorCode(colorCodeHex) + colorInfo, Err := ParseColorCode(colorCode) if Err != nil { log.Println(Err) - return nil, Err + return } colorImage := genImage(colorInfo) - - fileReader = bufio.NewReader(&buffer) + var ( + buffer bytes.Buffer + fileWriter = bufio.NewWriter(&buffer) + fileReader = bufio.NewReader(&buffer) + ) Err = jpeg.Encode(fileWriter, colorImage, &jpeg.Options{Quality: 60}) if Err != nil { @@ -92,18 +104,10 @@ func GenerateImage(colorCode string) (fileReader io.Reader, Err error) { } log.Println("generatedImage: process ended") - return -} -func colorAction(command string, context messageContext) { - fileData, Err := GenerateImage(command) + _, Err = session.ChannelFileSend(message.ChannelID, "sample.jpeg", fileReader) if Err != nil { - log.Println("failed to genarateImage: ", Err) - return - } - Err = context.fileSend("unkonow.jpeg", fileData) - if Err != nil { - log.Println("failed file send: ", Err) + log.Println(Err) return } } diff --git a/src/tools.go b/src/tools.go index 5eb110f..2aac46f 100644 --- a/src/tools.go +++ b/src/tools.go @@ -1,90 +1,35 @@ package src import ( - "io" + "github.com/bwmarrin/discordgo" "log" "strings" - - "github.com/bwmarrin/discordgo" ) -const ( - prefix = "%" - colorPrefix = "#" -) - -type messageContext struct { - s *discordgo.Session - m *discordgo.Message -} - -func (cxt *messageContext) messageSend(message string) (Err error) { - _, Err = cxt.s.ChannelMessageSend(cxt.m.ChannelID, message) - if Err != nil { - log.Println("failed send message: ", Err) - return - } - return -} - -func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) { - _, Err = cxt.s.ChannelFileSend(cxt.m.ChannelID, fileName, data) - if Err != nil { - log.Println("failed send file: ", Err) - return - } - return -} +var prefix = "%" -func MessageCreate(session *discordgo.Session, message *discordgo.Message) { - Context := messageContext{ - session, - message, - } - - if message.Author.ID == session.State.User.ID { +func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + if m.Author.ID == s.State.User.ID { return } - if !strings.HasPrefix(message.Content, prefix) { + if !strings.HasPrefix(m.Content, prefix) { return } - commandBody := strings.Split(message.Content, " ") - switch commandBody[0][len(prefix):] { - case "color": - if len(commandBody) != 2 { - Err := Context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") - if Err != nil { - log.Println("failed send message: ", Err) - return - } - return - } - colorAction(commandBody[1], Context) - case "morse": - if len(commandBody) != 2 { - Err := Context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") - if Err != nil { - log.Println("failed send message: ", Err) - return - } - return - } - morseAction(commandBody[1], Context.m.Content[len("%morse decode"):], Context) - default: - contentText := fetchMessage(commandBody[0][len(prefix):]) - Err := Context.messageSend(contentText) - if Err != nil { - log.Println("failed send message: ", Err) - return - } - } + cmd := strings.Split(m.Content, " ")[0][len(prefix):] + Err, _ := s.ChannelMessageSend(m.ChannelID, fetchMessage(cmd)) + nilCheck(Err) } -func BootNotify(s *discordgo.Session, _ *discordgo.Ready) { +func BootNotify(s *discordgo.Session, m *discordgo.Ready) { // BootNotify is sending message when this bot is booted. - _, Err := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") + Err, _ := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") + nilCheck(Err) +} + +func nilCheck(Err *discordgo.Message) { if Err != nil { - log.Println("Boot failed: ", Err) + log.Println("Error: ", Err) + return } } From 93a907dce9c243d5368b6c255d1c0152c285aabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=82=AC=E3=83=BC?= <44018535+raiga0310@users.noreply.github.com> Date: Tue, 7 Jul 2020 00:28:42 +0900 Subject: [PATCH 80/83] Revert "Revert "Refactor"" (#24) --- .gitignore | 2 +- main.go | 21 +++++-- src/decodeMorse.go | 135 +++++++++++++++++++++++++------------------- src/fecthMessage.go | 4 +- src/message.go | 12 +++- src/sendImage.go | 56 +++++++++--------- src/tools.go | 87 ++++++++++++++++++++++------ 7 files changed, 201 insertions(+), 116 deletions(-) diff --git a/.gitignore b/.gitignore index 12a6757..4bd8ebc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.exe + .idea -secret vendor diff --git a/main.go b/main.go index 3d22d0a..bef30ca 100644 --- a/main.go +++ b/main.go @@ -1,26 +1,31 @@ package main import ( - "change-status-go/secret" command "change-status-go/src" "fmt" - "github.com/bwmarrin/discordgo" + "log" "os" "os/signal" "syscall" + + "github.com/bwmarrin/discordgo" ) func main() { discordBrain, err := discordgo.New() - discordBrain.Token = secret.Token if err != nil { - fmt.Println("Error logging in") + fmt.Println("Error: discordgo.New(): something wrong.") fmt.Println(err) } + discordToken := loadToken() + if discordToken == "" { + log.Println("Error: no discord token exists.") + return + } + discordBrain.Token = discordToken + discordBrain.AddHandler(command.MessageCreate) - discordBrain.AddHandler(command.GenerateImage) - discordBrain.AddHandler(command.DecodeMorse) err = discordBrain.Open() if err != nil { @@ -34,3 +39,7 @@ func main() { <-sc return } + +func loadToken() string { + return os.Getenv("DISCORD_TOKEN") +} diff --git a/src/decodeMorse.go b/src/decodeMorse.go index c736a33..fbb6f4a 100644 --- a/src/decodeMorse.go +++ b/src/decodeMorse.go @@ -3,57 +3,66 @@ package src import ( "errors" "fmt" - "github.com/bwmarrin/discordgo" "log" "regexp" "strings" ) -const ( - prefixMorseDecode = "%decode" -) - var alphabetTable = map[string]string{ - "a": ".-", - "b": "-...", - "c": "-.-.", - "d": "-..", - "e": ".", - "f": "..-.", - "g": "--.", - "h": "....", - "i": "..", - "j": ".---", - "k": "-.-", - "l": ".-..", - "m": "--", - "n": "-.", - "o": "---", - "p": ".--.", - "q": "--.-", - "r": ".-.", - "s": "...", - "t": "-", - "u": "..-", - "v": "...-", - "w": "-..-", - "x": "-..-", - "z": "--..", - ".": ".--.-.", - ",": "--..--", - ":": "---...", - "?": "..--..", - "'": ".----.", - "-": "-....-", - "(": "-.--.", - ")": "-.--.-", - "/": "-..-.", - "=": "-...-.", - "+": ".-.-.-", - "\"": ".-..-.", - "*": "-..-", - "@": ".--.-.", - "amend": "........", + "a": ".-", + "b": "-...", + "c": "-.-.", + "d": "-..", + "e": ".", + "f": "..-.", + "g": "--.", + "h": "....", + "i": "..", + "j": ".---", + "k": "-.-", + "l": ".-..", + "m": "--", + "n": "-.", + "o": "---", + "p": ".--.", + "q": "--.-", + "r": ".-.", + "s": "...", + "t": "-", + "u": "..-", + "v": "...-", + "w": ".--", + "x": "-..-", + "y": "-.--", + "z": "--..", + "0": "-----", + "1": ".----", + "2": "..---", + "3": "...--", + "4": "....-", + "5": ".....", + "6": "-....", + "7": "--...", + "8": "---..", + "9": "----.", + ".": ".-.-.-", + ",": "--..--", + "?": "..--..", + "'": ".----.", + "!": "-.-.--", + "/": "-..-.", + "&": ".-...", + ":": "---...", + ";": "-.-.-.", + "=": "-...-", + "+": ".-.-.", + "-": "-....-", + "_": "..--.-", + "\"": ".-..-.", + "$": "...-..-", + "@": ".--.-.", + " ***amend*** ": "........", + " ": "*", } func searchTable(morseInput string) (string, bool) { @@ -67,7 +76,7 @@ func searchTable(morseInput string) (string, bool) { } func decode(sentence string) (response string, Err error) { - reg := regexp.MustCompile(`[ \t]+`) + reg := regexp.MustCompile(`\s+`) sentence = reg.ReplaceAllString(sentence, " ") log.Printf(sentence) for _, part := range strings.Split(sentence, " ") { @@ -84,25 +93,35 @@ func decode(sentence string) (response string, Err error) { return } -func DecodeMorse(session *discordgo.Session, message *discordgo.MessageCreate) { - if message.Author.Bot { +func DecodeMorse(messageContent string) (decodeResult string, Err error) { + sentence := strings.TrimSpace(messageContent) + decodeResult, Err = decode(sentence) + if Err != nil { + log.Println("Failed to decode:", Err) return } - if !strings.HasPrefix(message.Content, prefixMorseDecode) { + return +} + +func morseCodeOperation(mode string, content string) (answerSentence string, Err error) { + switch mode { + case "decode": + answerSentence, Err = DecodeMorse(content) return + default: + return "", fmt.Errorf("Error at morseCodeOperation: No such operation.") } - sentence := strings.TrimSpace(message.Content[len(prefixMorseDecode):]) - decodeResult, Err := decode(sentence) +} + +func morseAction(command string, codeSentence string, context messageContext) { + contentText, Err := morseCodeOperation(command, codeSentence) if Err != nil { - log.Println("Failed to decode:", Err) - _, Err = session.ChannelMessageSend(message.ChannelID, fmt.Sprintf("復号に失敗しました。: %s", Err)) - if Err != nil { - log.Println("Error at ChannelMessageSend: ", Err) - } + log.Println("failed decode morse: ", Err) return } - _, Err = session.ChannelMessageSend(message.ChannelID, decodeResult) + Err = context.messageSend(contentText) if Err != nil { - log.Println("Error at ChannelMessageSend:", Err) + log.Println("failed send message: ", Err) + return } } diff --git a/src/fecthMessage.go b/src/fecthMessage.go index b4b9e7a..d0ba8ea 100644 --- a/src/fecthMessage.go +++ b/src/fecthMessage.go @@ -1,12 +1,12 @@ package src -func fetchMessage (cmd string) string{ +func fetchMessage(cmd string) string { switch cmd { case "help": return helpMessage case "ping": return "Pong!" default: - return "該当するコマンドがありません`%help`を参照してください。" + return "該当するコマンドがありません。`%help`を参照してください。" } } diff --git a/src/message.go b/src/message.go index 1a1af83..e4d620c 100644 --- a/src/message.go +++ b/src/message.go @@ -2,7 +2,7 @@ package src var ( helpMessage = "```asciidoc\n" + -`= Title = + `= Title = something = Description = 表示名に絵文字を付けることでステータスを表示するBotの予定 @@ -10,8 +10,14 @@ var ( (Prefix: "%") help :: このBotの概要を説明します。 ping :: Pong!と返します。かわいいですね。 - (prefix: "#") - [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 + color :: 第二引数に以下の形式で入力: + (prefix: "#") + [HexRGB] :: 16進カラーコードの画像を返します。略記法は現在対応していません。 + morse :: モールス暗号に関する変換をします。第2引数で動作を指定します。 + decode :: 暗号文 → 平文 + [Tips] + 現在任意の長さの空白を暗号変換に際して圧縮しています。これにより本来長い空白を入れることによって + 平文での空白を表現していたところを、*を代替しています。 = Source = github.com/brokenManager/change-status-go ` + "```\n" diff --git a/src/sendImage.go b/src/sendImage.go index 6a17722..c6c968b 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -4,13 +4,12 @@ import ( "bufio" "bytes" "fmt" - "github.com/bwmarrin/discordgo" "image" "image/color" "image/jpeg" + "io" "log" "strconv" - "strings" ) func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { @@ -39,13 +38,13 @@ func ParseColorCode(colorCode string) (Result color.RGBA, Err error) { func genImage(colorInfo color.RGBA) *image.RGBA { const ( - statrX = 0 + startX = 0 startY = 0 width = 40 height = 30 ) - img := image.NewRGBA(image.Rect(statrX, startY, width, height)) + img := image.NewRGBA(image.Rect(startX, startY, width, height)) for y := img.Rect.Min.Y; y < img.Rect.Max.Y; y++ { for x := img.Rect.Min.X; x < img.Rect.Max.X; x++ { @@ -55,40 +54,29 @@ func genImage(colorInfo color.RGBA) *image.RGBA { return img } -func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) { +func GenerateImage(colorCode string) (fileReader io.Reader, Err error) { + var ( + buffer bytes.Buffer + fileWriter = bufio.NewWriter(&buffer) + ) - content := strings.TrimSpace(message.Content) - if !strings.HasPrefix(content, "#") { - return - } + colorCodeHex := colorCode[len(colorPrefix):] + log.Println(colorCodeHex) - if v, Err := strconv.ParseInt(content[1:], 16, 32); Err != nil || v < 0 { + if v, Err := strconv.ParseInt(colorCodeHex, 16, 32); Err != nil || v < 0 { log.Println("strconv: invalid value; not Hex") - return - } - - colorCode := content[len("#"):] - if len(colorCode) != 6 { - log.Println("generateImage: len(colorCode) must be just 6") - _, Err := session.ChannelMessageSend(message.ChannelID, "不正な値です。形式は16進のカラーコードである必要があります。") - if Err != nil { - log.Println(Err) - } - return + return nil, Err } - colorInfo, Err := ParseColorCode(colorCode) + colorInfo, Err := ParseColorCode(colorCodeHex) if Err != nil { log.Println(Err) - return + return nil, Err } colorImage := genImage(colorInfo) - var ( - buffer bytes.Buffer - fileWriter = bufio.NewWriter(&buffer) - fileReader = bufio.NewReader(&buffer) - ) + + fileReader = bufio.NewReader(&buffer) Err = jpeg.Encode(fileWriter, colorImage, &jpeg.Options{Quality: 60}) if Err != nil { @@ -104,10 +92,18 @@ func GenerateImage(session *discordgo.Session, message *discordgo.MessageCreate) } log.Println("generatedImage: process ended") + return +} - _, Err = session.ChannelFileSend(message.ChannelID, "sample.jpeg", fileReader) +func colorAction(command string, context messageContext) { + fileData, Err := GenerateImage(command) if Err != nil { - log.Println(Err) + log.Println("failed to genarateImage: ", Err) + return + } + Err = context.fileSend("unkonow.jpeg", fileData) + if Err != nil { + log.Println("failed file send: ", Err) return } } diff --git a/src/tools.go b/src/tools.go index 2aac46f..5eb110f 100644 --- a/src/tools.go +++ b/src/tools.go @@ -1,35 +1,90 @@ package src import ( - "github.com/bwmarrin/discordgo" + "io" "log" "strings" + + "github.com/bwmarrin/discordgo" ) -var prefix = "%" +const ( + prefix = "%" + colorPrefix = "#" +) + +type messageContext struct { + s *discordgo.Session + m *discordgo.Message +} -func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.State.User.ID { +func (cxt *messageContext) messageSend(message string) (Err error) { + _, Err = cxt.s.ChannelMessageSend(cxt.m.ChannelID, message) + if Err != nil { + log.Println("failed send message: ", Err) return } - if !strings.HasPrefix(m.Content, prefix) { + return +} + +func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) { + _, Err = cxt.s.ChannelFileSend(cxt.m.ChannelID, fileName, data) + if Err != nil { + log.Println("failed send file: ", Err) return } - - cmd := strings.Split(m.Content, " ")[0][len(prefix):] - Err, _ := s.ChannelMessageSend(m.ChannelID, fetchMessage(cmd)) - nilCheck(Err) + return } -func BootNotify(s *discordgo.Session, m *discordgo.Ready) { - // BootNotify is sending message when this bot is booted. - Err, _ := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") - nilCheck(Err) +func MessageCreate(session *discordgo.Session, message *discordgo.Message) { + Context := messageContext{ + session, + message, + } + + if message.Author.ID == session.State.User.ID { + return + } + if !strings.HasPrefix(message.Content, prefix) { + return + } + commandBody := strings.Split(message.Content, " ") + switch commandBody[0][len(prefix):] { + case "color": + if len(commandBody) != 2 { + Err := Context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") + if Err != nil { + log.Println("failed send message: ", Err) + return + } + return + } + colorAction(commandBody[1], Context) + case "morse": + if len(commandBody) != 2 { + Err := Context.messageSend("コマンドの形式が間違っています。`%help`を参照してください。") + if Err != nil { + log.Println("failed send message: ", Err) + return + } + return + } + morseAction(commandBody[1], Context.m.Content[len("%morse decode"):], Context) + default: + contentText := fetchMessage(commandBody[0][len(prefix):]) + Err := Context.messageSend(contentText) + if Err != nil { + log.Println("failed send message: ", Err) + return + } + } + } -func nilCheck(Err *discordgo.Message) { +func BootNotify(s *discordgo.Session, _ *discordgo.Ready) { + // BootNotify is sending message when this bot is booted. + _, Err := s.ChannelMessageSend("699941274484080660", "BootBot! <@!622077711309078529>") if Err != nil { - log.Println("Error: ", Err) - return + log.Println("Boot failed: ", Err) } } From 11765a69ca8f3b37f0739d318f2f3d954fd90c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=82=AC=E3=83=BC?= <44018535+raiga0310@users.noreply.github.com> Date: Tue, 7 Jul 2020 00:39:58 +0900 Subject: [PATCH 81/83] =?UTF-8?q?[fix]=E3=81=84=E3=81=A3=E3=81=9D=E3=81=86?= =?UTF-8?q?=E3=83=9E=E3=82=B8=E3=81=A7=E6=B8=88=E3=81=BE=E3=82=93=E3=81=8B?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E2=80=A6=E2=80=A6=E3=80=82=20(#23)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [fix]いっそうマジで済まんかった……。 * [fix]panicをつけた --- main.go | 10 ++++------ src/sendImage.go | 2 +- src/tools.go | 3 --- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index bef30ca..b1f1254 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( command "change-status-go/src" "fmt" - "log" "os" "os/signal" "syscall" @@ -14,14 +13,12 @@ import ( func main() { discordBrain, err := discordgo.New() if err != nil { - fmt.Println("Error: discordgo.New(): something wrong.") - fmt.Println(err) + panic(err) } discordToken := loadToken() if discordToken == "" { - log.Println("Error: no discord token exists.") - return + panic("no discord token exists.") } discordBrain.Token = discordToken @@ -29,8 +26,9 @@ func main() { err = discordBrain.Open() if err != nil { - fmt.Println(err) + panic(err) } + defer discordBrain.Close() fmt.Println("Bot起動完了、命令待機中") discordBrain.AddHandlerOnce(command.BootNotify) diff --git a/src/sendImage.go b/src/sendImage.go index c6c968b..98cda85 100644 --- a/src/sendImage.go +++ b/src/sendImage.go @@ -71,7 +71,7 @@ func GenerateImage(colorCode string) (fileReader io.Reader, Err error) { colorInfo, Err := ParseColorCode(colorCodeHex) if Err != nil { log.Println(Err) - return nil, Err + return } colorImage := genImage(colorInfo) diff --git a/src/tools.go b/src/tools.go index 5eb110f..aadeed9 100644 --- a/src/tools.go +++ b/src/tools.go @@ -22,7 +22,6 @@ func (cxt *messageContext) messageSend(message string) (Err error) { _, Err = cxt.s.ChannelMessageSend(cxt.m.ChannelID, message) if Err != nil { log.Println("failed send message: ", Err) - return } return } @@ -31,7 +30,6 @@ func (cxt *messageContext) fileSend(fileName string, data io.Reader) (Err error) _, Err = cxt.s.ChannelFileSend(cxt.m.ChannelID, fileName, data) if Err != nil { log.Println("failed send file: ", Err) - return } return } @@ -75,7 +73,6 @@ func MessageCreate(session *discordgo.Session, message *discordgo.Message) { Err := Context.messageSend(contentText) if Err != nil { log.Println("failed send message: ", Err) - return } } From c79161317ebe9dd864005fbb79fcd04969b4c04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=8B=E3=82=8F=E3=81=88=E3=82=82=E3=82=93?= <34652535+kawaemon@users.noreply.github.com> Date: Tue, 7 Jul 2020 00:46:19 +0900 Subject: [PATCH 82/83] Add tag --- .github/workflows/docker.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index 38d9f0e..79cb74a 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -4,7 +4,9 @@ on: push: branches: - master - - refactor + + tags: + - v* env: IMAGE_NAME: change-status-go From c764cb45b3df6f6a9c35e37fa8a5f9a8cea4c4db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=8B=E3=82=8F=E3=81=88=E3=82=82=E3=82=93?= <34652535+kawaemon@users.noreply.github.com> Date: Tue, 7 Jul 2020 09:50:00 +0900 Subject: [PATCH 83/83] Use scratch insted of alpine --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0f362ab..00b52ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,10 +2,10 @@ FROM golang:1.14.4-alpine3.12 as build RUN mkdir /src COPY . /src WORKDIR /src -RUN go build -o change-status-go +RUN go build -a -tags netgo -installsuffix netgo -o change-status-go -FROM alpine:3.12 +FROM scratch COPY --from=build /src/change-status-go /bot/ WORKDIR /bot/ ENTRYPOINT [ "/bot/change-status-go" ]