Exemplo n.º 1
0
Arquivo: main.go Projeto: Xwth/h-bot
func connectToDiscord() {
	log.Println("Connecting to discord")

	//var err error
	c := config.Get()
	dg, err := discordgo.New(c.Email, c.Password, c.Token)

	if err != nil {
		log.Println(err.Error())
		return
	}

	// Register messageCreate as a callback for the OnMessageCreate event.
	dg.AddHandler(messageCreate)

	// Retry after broken websocket
	dg.ShouldReconnectOnError = true

	// Open websocket connection
	err = dg.Open()
	if err != nil {
		log.Println(err)
		return
	}

	log.Println("Connected")
}
Exemplo n.º 2
0
Arquivo: main.go Projeto: Xwth/h-bot
func shouldIgnore(user *discordgo.User) bool {
	c := config.Get()

	for _, id := range c.Blacklist {
		if id == user.ID {
			return true
		}
	}

	return false
}
Exemplo n.º 3
0
Arquivo: main.go Projeto: Xwth/h-bot
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
	var deleteMessageAuthor bool
	deleteMessageAuthor = true

	text := m.ContentWithMentionsReplaced()

	if m.Author.ID == s.State.User.ID {
		// Ignore self
		return
	}

	channel, _ := s.State.Channel(m.ChannelID)

	if channel.IsPrivate {
		channel.Name = "direct message"
	}

	if strings.HasPrefix(text, "!") || strings.HasPrefix(text, ".") || strings.HasPrefix(text, "bot.") {
		// Ignore shit meant for other bots
		return
	}

	isMentioned := isUserMentioned(s.State.User, m.Mentions) || m.MentionEveryone

	text = strings.Replace(text, "@everyone", "", -1)

	// Log cleaned up message
	fmt.Printf("%20s %20s %20s > %s\n", channel.Name, time.Now().Format(time.Stamp), m.Author.Username, text)

	if shouldIgnore(m.Author) {
		return
	}

	c := config.Get()

	for _, ChannelID := range c.Channels {
		if m.ChannelID == ChannelID {
			linksFound, reply := lewd.ParseLinks(text)

			if linksFound {
				content := strings.Join(lewd.ContentLinks(m.Content)[:], " ")
				if deleteMessageAuthor {
					s.ChannelMessageDelete(m.ChannelID, m.ID)
				}
				s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s: %s\n%s", m.Author.Username, content, reply))
				return
			}
		}
	}

	// Accept the legacy mention as well and trim it off from text
	if strings.HasPrefix(strings.ToLower(text), "lewdbot, ") {
		text = text[9:]
		isMentioned = true
	}

	if channel.IsPrivate || isMentioned {
		var reply string
		reply = regex.Lewdbot.ReplaceAllString(reply, m.Author.Username)

		// Log our reply
		fmt.Printf("%20s %20s %20s > %s\n", channel.Name, time.Now().Format(time.Stamp), s.State.User.Username, reply)

		s.ChannelMessageSend(m.ChannelID, reply)
	}
}