-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kepler471/refactor-1
Refactor 1
- Loading branch information
Showing
7 changed files
with
228 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
type args struct { | ||
m media | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want media | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := format(tt.args.m) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("format() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("format() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/bwmarrin/discordgo" | ||
"log" | ||
"net/url" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func handleVredditLink(s *discordgo.Session, m *discordgo.MessageCreate) { | ||
c := m.ChannelID | ||
log.Printf("Message by %s contains v.redd.it link: %s", m.Author.Username, m.Content) | ||
log.Print("Message sent on channel: ", c) | ||
uu := strings.Split(m.Content, " ") | ||
for _, u := range uu { | ||
if strings.Contains(u, "v.redd.it") || strings.Contains(u, "reddit.com") { | ||
u, err := url.Parse(u) | ||
if err != nil { | ||
log.Printf("Message segment: `%s`, did not parse as URl: %v", u, err) | ||
continue | ||
} | ||
log.Printf("Message segment: `%s`, is valid URl", u) | ||
f, err := download(u.String()) | ||
if err != nil { | ||
log.Print("Error downloading f: ", err) | ||
continue | ||
} | ||
if f.Size() > 8000000 { | ||
log.Printf("%v bytes is too large for Discord upload", f.Size()) | ||
continue | ||
} | ||
blank := discordgo.MessageEmbed{} | ||
_, err = s.ChannelMessageEditEmbed(c, m.ID, &blank) | ||
if err != nil { | ||
log.Printf("Error removing %s's embedded v.redd.it image: %v", m.Author.Username, err) | ||
} | ||
o, err := os.Open(f.Path) | ||
if err != nil { | ||
log.Print("Error reading ", f.Name()) | ||
} | ||
_, err = s.ChannelFileSend(c, f.Name(), o) | ||
if err != nil { | ||
log.Printf("Error uploading %s's media %s, %v", m.Author.Username, f.Name(), err) | ||
} | ||
_ = o.Close() | ||
} | ||
} | ||
} | ||
|
||
func handleRedditLink(s *discordgo.Session, m *discordgo.MessageCreate) { | ||
log.Printf("Message by %s contains reddit link: %s", m.Author.Username, m.Content) | ||
//// TODO check if standard reddit link contains v.redd.it media through reddit api | ||
//uu := strings.Split(m.Content, " ") | ||
//for _, u := range uu { | ||
// if strings.Contains(u, "reddit.com") { | ||
// u, err := url.Parse(u) | ||
// if err != nil { | ||
// log.Printf("Message segment: `%s`, did not parse as URl: %v", u, err) | ||
// continue | ||
// } | ||
// log.Printf("Message segment: `%s`, is valid URl", u) | ||
// // getVredditLink(URL) | ||
// } | ||
//} | ||
handleVredditLink(s, m) | ||
} | ||
|
||
func handleImgurTest(s *discordgo.Session, m *discordgo.MessageCreate) { | ||
// TODO imgur links seem to embed nicely, so see about using imgur api | ||
_, _ = s.ChannelMessageSend(m.ChannelID, "https://i.imgur.com/ZWexH7h.mp4") | ||
} |
Oops, something went wrong.