Last active
June 5, 2017 11:10
-
-
Save Proximyst/2df1fdcf060eb394088bd541dca8f5a0 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"flag" | |
"github.com/bwmarrin/discordgo" | |
"fmt" | |
"log" | |
"os" | |
"os/signal" | |
"syscall" | |
) | |
var ( | |
Token string | |
) | |
func init() { | |
flag.StringVar(&Token, "token", "", "Token to listen to. (Prefix \"Bot \" if its a bot account)") | |
flag.Parse() | |
} | |
func main() { | |
bot, err := discordgo.New(Token) | |
if err != nil { | |
fmt.Println("Couldn't create discordgo instance.") | |
log.Fatalln(err) | |
return | |
} | |
bot.AddHandler(onMessageReceived) | |
err = bot.Open() | |
if err != nil { | |
fmt.Println("Couldn't connect to Discord.") | |
log.Fatalln(err) | |
return | |
} | |
channel := make(chan os.Signal, 1) | |
signal.Notify(channel, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) | |
<- channel | |
bot.Close() | |
} | |
func onMessageReceived(session *discordgo.Session, event *discordgo.MessageCreate) { | |
if event.Mentions == nil || len(event.Mentions) == 0 || !contains(event.Mentions, session.State.User) { | |
return | |
} | |
channel, _ := session.Channel(event.ChannelID) | |
fmt.Println(channel.GuildID, "-", event.Message.Author.ID, "(" + event.Message.Author.Username + "#" + event.Message.Author.Discriminator + ")::", event.Message.Content) | |
} | |
func contains(s []*discordgo.User, e *discordgo.User) bool { | |
for _, a := range s { | |
if a.ID == e.ID { | |
return true | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment