From bc8527b896a3e213205feec230611fc292a9ed8c Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 6 Mar 2018 17:34:02 +0100 Subject: [PATCH] Added welcome messages to discord bot --- bots/discord/OnGuildMemberAdd.go | 47 +++++++++++++++ bots/discord/OnGuildMemberRemove.go | 14 +++++ bots/discord/OnMessageCreate.go | 94 +++++++++++++++++++++++++++++ bots/discord/discord.go | 93 ++-------------------------- 4 files changed, 159 insertions(+), 89 deletions(-) create mode 100644 bots/discord/OnGuildMemberAdd.go create mode 100644 bots/discord/OnGuildMemberRemove.go create mode 100644 bots/discord/OnMessageCreate.go diff --git a/bots/discord/OnGuildMemberAdd.go b/bots/discord/OnGuildMemberAdd.go new file mode 100644 index 00000000..f8def814 --- /dev/null +++ b/bots/discord/OnGuildMemberAdd.go @@ -0,0 +1,47 @@ +package main + +import ( + "fmt" + + "github.com/bwmarrin/discordgo" +) + +// Channels +const welcomeChannel = "420600996268474368" +const enChannel = "134910939140063232" +const jpChannel = "334016470474424322" + +// Emoji +const ramFive = ":ramFive:418520880511975426" +const remFive = ":remFive:403671154553782275" + +// OnGuildMemberAdd is called every time a user joins the server. +func OnGuildMemberAdd(session *discordgo.Session, event *discordgo.GuildMemberAdd) { + fmt.Println(event.Member.User.Username + " just joined!") + + msg, err := session.ChannelMessageSend(welcomeChannel, fmt.Sprintf( + "**Welcome** %s!\n\nPlease introduce yourself in <#%s>.\n\n日本人は <#%s> で自己紹介して下さい!", + event.Member.User.Mention(), + enChannel, + jpChannel, + )) + + if err != nil { + fmt.Println(err) + return + } + + err = session.MessageReactionAdd(welcomeChannel, msg.ID, ramFive) + + if err != nil { + fmt.Println(err) + return + } + + err = session.MessageReactionAdd(welcomeChannel, msg.ID, remFive) + + if err != nil { + fmt.Println(err) + return + } +} diff --git a/bots/discord/OnGuildMemberRemove.go b/bots/discord/OnGuildMemberRemove.go new file mode 100644 index 00000000..7e7d8c23 --- /dev/null +++ b/bots/discord/OnGuildMemberRemove.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + "github.com/bwmarrin/discordgo" +) + +// OnGuildMemberRemove is called every time a user leaves the server. +func OnGuildMemberRemove(session *discordgo.Session, event *discordgo.GuildMemberRemove) { + fmt.Println(event.Member.User.Username + " just left!") + + // session.ChannelMessageSend(welcomeChannel, "Good bye "+event.Member.User.Mention()+"!") +} diff --git a/bots/discord/OnMessageCreate.go b/bots/discord/OnMessageCreate.go new file mode 100644 index 00000000..16bdcaf2 --- /dev/null +++ b/bots/discord/OnMessageCreate.go @@ -0,0 +1,94 @@ +package main + +import ( + "strings" + + "github.com/animenotifier/arn" + "github.com/bwmarrin/discordgo" +) + +// OnMessageCreate is called every time a new message is created on any channel. +func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) { + // Ignore all messages created by the bot itself + if msg.Author.ID == s.State.User.ID { + return + } + + if msg.Content == "!commands" { + s.ChannelMessageSend(msg.ChannelID, ` +**!user** [username] +**!anime** [id] +**!animelist** [username] +**!tag** [forum tag]`) + } + + // Has the bot been mentioned? + for _, user := range msg.Mentions { + if user.ID == discord.State.User.ID { + s.ChannelMessageSend(msg.ChannelID, msg.Author.Mention()+" :heart:") + return + } + } + + if strings.HasPrefix(msg.Content, "!user ") { + s.ChannelMessageSend(msg.ChannelID, "https://notify.moe/+"+strings.Split(msg.Content, " ")[1]) + return + } + + if strings.HasPrefix(msg.Content, "!anime ") { + s.ChannelMessageSend(msg.ChannelID, "https://notify.moe/anime/"+strings.Split(msg.Content, " ")[1]) + return + } + + if strings.HasPrefix(msg.Content, "!animelist ") { + s.ChannelMessageSend(msg.ChannelID, "https://notify.moe/+"+strings.Split(msg.Content, " ")[1]+"/animelist") + return + } + + if strings.HasPrefix(msg.Content, "!tag ") { + s.ChannelMessageSend(msg.ChannelID, "https://notify.moe/forum/"+strings.ToLower(strings.Split(msg.Content, " ")[1])) + return + } + + if strings.HasPrefix(msg.Content, "!play ") { + s.UpdateStatus(0, msg.Content[len("!play "):]) + return + } + + if strings.HasPrefix(msg.Content, "!s ") { + term := msg.Content[len("!s "):] + users, animes, posts, threads, tracks, characters := arn.Search(term, 3, 3, 3, 3, 3, 3) + message := "" + + for _, user := range users { + message += "https://notify.moe" + user.Link() + "\n" + } + + for _, anime := range animes { + message += "https://notify.moe" + anime.Link() + "\n" + } + + for _, post := range posts { + message += "https://notify.moe" + post.Link() + "\n" + } + + for _, thread := range threads { + message += "https://notify.moe" + thread.Link() + "\n" + } + + for _, track := range tracks { + message += "https://notify.moe" + track.Link() + "\n" + } + + for _, character := range characters { + message += "https://notify.moe" + character.Link() + "\n" + } + + if len(users) == 0 && len(animes) == 0 && len(posts) == 0 && len(threads) == 0 && len(tracks) == 0 { + message = "Sorry, I couldn't find anything using that term." + } + + s.ChannelMessageSend(msg.ChannelID, message) + return + } +} diff --git a/bots/discord/discord.go b/bots/discord/discord.go index c467355b..ec81ebe9 100644 --- a/bots/discord/discord.go +++ b/bots/discord/discord.go @@ -4,7 +4,6 @@ import ( "log" "os" "os/signal" - "strings" "syscall" "github.com/animenotifier/arn" @@ -42,8 +41,10 @@ func main() { defer discord.Close() - // Receive messages - discord.AddHandler(onMessage) + // Receive events + discord.AddHandler(OnMessageCreate) + discord.AddHandler(OnGuildMemberAdd) + discord.AddHandler(OnGuildMemberRemove) // Wait for a CTRL-C log.Printf("Tsundere is ready. Press CTRL-C to exit.") @@ -51,89 +52,3 @@ func main() { signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) <-sc } - -// This function will be called every time a new message is created on any channel. -func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) { - // Ignore all messages created by the bot itself - if m.Author.ID == s.State.User.ID { - return - } - - if m.Content == "!commands" { - s.ChannelMessageSend(m.ChannelID, ` -**!user** [username] -**!anime** [id] -**!animelist** [username] -**!tag** [forum tag]`) - } - - // Has the bot been mentioned? - for _, user := range m.Mentions { - if user.ID == discord.State.User.ID { - s.ChannelMessageSend(m.ChannelID, m.Author.Mention()+" :heart:") - return - } - } - - if strings.HasPrefix(m.Content, "!user ") { - s.ChannelMessageSend(m.ChannelID, "https://notify.moe/+"+strings.Split(m.Content, " ")[1]) - return - } - - if strings.HasPrefix(m.Content, "!anime ") { - s.ChannelMessageSend(m.ChannelID, "https://notify.moe/anime/"+strings.Split(m.Content, " ")[1]) - return - } - - if strings.HasPrefix(m.Content, "!animelist ") { - s.ChannelMessageSend(m.ChannelID, "https://notify.moe/+"+strings.Split(m.Content, " ")[1]+"/animelist") - return - } - - if strings.HasPrefix(m.Content, "!tag ") { - s.ChannelMessageSend(m.ChannelID, "https://notify.moe/forum/"+strings.ToLower(strings.Split(m.Content, " ")[1])) - return - } - - if strings.HasPrefix(m.Content, "!play ") { - s.UpdateStatus(0, strings.Split(m.Content, " ")[1]) - return - } - - if strings.HasPrefix(m.Content, "!s ") { - term := m.Content[len("!s "):] - users, animes, posts, threads, tracks, characters := arn.Search(term, 3, 3, 3, 3, 3, 3) - message := "" - - for _, user := range users { - message += "https://notify.moe" + user.Link() + "\n" - } - - for _, anime := range animes { - message += "https://notify.moe" + anime.Link() + "\n" - } - - for _, post := range posts { - message += "https://notify.moe" + post.Link() + "\n" - } - - for _, thread := range threads { - message += "https://notify.moe" + thread.Link() + "\n" - } - - for _, track := range tracks { - message += "https://notify.moe" + track.Link() + "\n" - } - - for _, character := range characters { - message += "https://notify.moe" + character.Link() + "\n" - } - - if len(users) == 0 && len(animes) == 0 && len(posts) == 0 && len(threads) == 0 && len(tracks) == 0 { - message = "Sorry, I couldn't find anything using that term." - } - - s.ChannelMessageSend(m.ChannelID, message) - return - } -}