123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- package main
- import (
- "fmt"
- "log"
- "os"
- "strings"
- "time"
- "github.com/bwmarrin/discordgo"
- "github.com/fatih/color"
- )
- type fileItem struct {
- Link string
- Filename string
- Time time.Time
- }
- //#region Events
- var lastMessageID string
- func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
- if lastMessageID != m.ID {
- handleMessage(m.Message, nil, false, false)
- }
- lastMessageID = m.ID
- }
- func messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) {
- if lastMessageID != m.ID {
- if m.EditedTimestamp != nil {
- handleMessage(m.Message, nil, true, false)
- }
- }
- lastMessageID = m.ID
- }
- func handleMessage(m *discordgo.Message, c *discordgo.Channel, edited bool, history bool) (int64, int64) {
- // Ignore own messages unless told not to
- if m.Author.ID == botUser.ID && !config.ScanOwnMessages {
- return -1, 0
- }
- if !history && !edited {
- timeLastMessage = time.Now()
- }
- // Admin Channel
- if isAdminChannelRegistered(m.ChannelID) {
- m = fixMessage(m)
- // Log
- sendLabel := fmt.Sprintf("%s in \"%s\"#%s",
- getUserIdentifier(*m.Author),
- getGuildName(m.GuildID), getChannelName(m.ChannelID, nil),
- )
- content := m.Content
- if len(m.Attachments) > 0 {
- content = content + fmt.Sprintf(" (%d attachments)", len(m.Attachments))
- }
- if edited {
- log.Println(lg("Message", "ADMIN CHANNEL", color.CyanString, "Edited [%s]: %s", sendLabel, content))
- } else {
- log.Println(lg("Message", "ADMIN CHANNEL", color.CyanString, "[%s]: %s", sendLabel, content))
- }
- }
- // Registered Channel
- if channelConfig := getSource(m, c); channelConfig != emptyConfig {
- // Ignore bots if told to do so
- if m.Author.Bot && *channelConfig.IgnoreBots {
- return -1, 0
- }
- // Ignore if told so by config
- if (!history && !*channelConfig.Enabled) || (edited && !*channelConfig.ScanEdits) {
- return -1, 0
- }
- m = fixMessage(m)
- // Log
- if config.MessageOutput {
- sendLabel := fmt.Sprintf("%s in \"%s\"#%s",
- getUserIdentifier(*m.Author),
- getGuildName(m.GuildID), getChannelName(m.ChannelID, nil),
- )
- content := m.Content
- if len(m.Attachments) > 0 {
- content += fmt.Sprintf(" \t[%d attachments]", len(m.Attachments))
- }
- content += fmt.Sprintf(" \t<%s>", m.ID)
- if !history || config.MessageOutputHistory {
- addOut := ""
- if history && config.MessageOutputHistory && !m.Timestamp.IsZero() {
- addOut = fmt.Sprintf(" @ %s", m.Timestamp.String()[:19])
- }
- if edited {
- log.Println(lg("Message", "", color.CyanString, "Edited [%s%s]: %s", sendLabel, addOut, content))
- } else {
- log.Println(lg("Message", "", color.CyanString, "[%s%s]: %s", sendLabel, addOut, content))
- }
- }
- }
- // Log Messages to File
- if channelConfig.LogMessages != nil {
- if channelConfig.LogMessages.Destination != "" {
- logPath := channelConfig.LogMessages.Destination
- if *channelConfig.LogMessages.DestinationIsFolder {
- if !strings.HasSuffix(logPath, string(os.PathSeparator)) {
- logPath += string(os.PathSeparator)
- }
- err := os.MkdirAll(logPath, 0755)
- if err == nil {
- logPath += "Log_Messages"
- if *channelConfig.LogMessages.DivideLogsByServer {
- if m.GuildID == "" {
- ch, err := bot.State.Channel(m.ChannelID)
- if err != nil && c != nil {
- ch = c
- }
- if ch != nil {
- if ch.Type == discordgo.ChannelTypeDM {
- logPath += " DM"
- } else if ch.Type == discordgo.ChannelTypeGroupDM {
- logPath += " GroupDM"
- } else if ch.Type == discordgo.ChannelTypeGuildText {
- logPath += " Text"
- } else if ch.Type == discordgo.ChannelTypeGuildCategory {
- logPath += " Category"
- } else if ch.Type == discordgo.ChannelTypeGuildForum {
- logPath += " Forum"
- } else if ch.Type == discordgo.ChannelTypeGuildPrivateThread || ch.Type == discordgo.ChannelTypeGuildPublicThread {
- logPath += " Thread"
- } else {
- logPath += " Unknown"
- }
- if ch.Name != "" {
- logPath += " - " + clearPath(ch.Name) + " -"
- } else if ch.Topic != "" {
- logPath += " - " + clearPath(ch.Topic) + " -"
- }
- }
- } else {
- logPath += " SID_" + m.GuildID
- }
- }
- if *channelConfig.LogMessages.DivideLogsByChannel {
- logPath += " CID_" + m.ChannelID
- }
- if *channelConfig.LogMessages.DivideLogsByUser {
- logPath += " UID_" + m.Author.ID
- }
- }
- logPath += ".txt"
- }
- // Read
- currentLog, err := os.ReadFile(logPath)
- currentLogS := ""
- if err == nil {
- currentLogS = string(currentLog)
- }
- canLog := true
- // Filter Duplicates
- if channelConfig.LogMessages.FilterDuplicates != nil {
- if *channelConfig.LogMessages.FilterDuplicates {
- if strings.Contains(currentLogS, fmt.Sprintf("[%s/%s/%s]", m.GuildID, m.ChannelID, m.ID)) {
- canLog = false
- }
- }
- }
- if canLog {
- // Writer
- f, err := os.OpenFile(logPath, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0600)
- if err != nil {
- log.Println(lg("Message", "", color.RedString, "[channelConfig.LogMessages] Failed to open log file:\t%s", err))
- f.Close()
- }
- defer f.Close()
- var newLine string
- // Prepend
- prefix := ""
- if channelConfig.LogMessages.Prefix != nil {
- prefix = *channelConfig.LogMessages.Prefix
- }
- // More Data
- additionalInfo := ""
- if channelConfig.LogMessages.UserData != nil {
- if *channelConfig.LogMessages.UserData {
- additionalInfo = fmt.Sprintf("[%s/%s/%s] \"%s\"#%s (%s) @ %s: ", m.GuildID, m.ChannelID, m.ID, m.Author.Username, m.Author.Discriminator, m.Author.ID, m.Timestamp)
- }
- }
- if len(m.Attachments) > 0 {
- additionalInfo += fmt.Sprintf("<%d ATTACHMENTS> ", len(m.Attachments))
- }
- // Append
- suffix := ""
- if channelConfig.LogMessages.Suffix != nil {
- suffix = *channelConfig.LogMessages.Suffix
- }
- // New Line
- contentFmt, err := m.ContentWithMoreMentionsReplaced(bot)
- if err == nil {
- newLine += "\n" + prefix + additionalInfo + contentFmt + suffix
- } else {
- newLine += "\n" + prefix + additionalInfo + m.Content + suffix
- }
- if _, err = f.WriteString(newLine); err != nil {
- log.Println(lg("Message", "", color.RedString, "[channelConfig.LogMessages] Failed to append file:\t%s", err))
- }
- }
- }
- }
- // Filters
- if channelConfig.Filters != nil {
- shouldAbort := false
- if channelConfig.Filters.AllowedPhrases != nil ||
- channelConfig.Filters.AllowedUsers != nil ||
- channelConfig.Filters.AllowedRoles != nil {
- shouldAbort = true
- if config.Debug {
- log.Println(lg("Debug", "Message", color.YellowString,
- "%s Filter will be ignoring by default...",
- color.HiMagentaString("(FILTER)")))
- }
- }
- if channelConfig.Filters.BlockedPhrases != nil {
- for _, phrase := range *channelConfig.Filters.BlockedPhrases {
- if strings.Contains(m.Content, phrase) {
- shouldAbort = true
- if config.Debug {
- log.Println(lg("Debug", "Message", color.YellowString,
- "%s blockedPhrases found \"%s\" in message, planning to abort...",
- color.HiMagentaString("(FILTER)"), phrase))
- }
- break
- }
- }
- }
- if channelConfig.Filters.AllowedPhrases != nil {
- for _, phrase := range *channelConfig.Filters.AllowedPhrases {
- if strings.Contains(m.Content, phrase) {
- shouldAbort = false
- if config.Debug {
- log.Println(lg("Debug", "Message", color.YellowString,
- "%s allowedPhrases found \"%s\" in message, planning to process...",
- color.HiMagentaString("(FILTER)"), phrase))
- }
- break
- }
- }
- }
- if channelConfig.Filters.BlockedUsers != nil {
- if stringInSlice(m.Author.ID, *channelConfig.Filters.BlockedUsers) {
- shouldAbort = true
- if config.Debug {
- log.Println(lg("Debug", "Message", color.YellowString,
- "%s blockedUsers caught %s, planning to abort...",
- color.HiMagentaString("(FILTER)"), m.Author.ID))
- }
- }
- }
- if channelConfig.Filters.AllowedUsers != nil {
- if stringInSlice(m.Author.ID, *channelConfig.Filters.AllowedUsers) {
- shouldAbort = false
- if config.Debug {
- log.Println(lg("Debug", "Message", color.YellowString,
- "%s allowedUsers caught %s, planning to process...",
- color.HiMagentaString("(FILTER)"), m.Author.ID))
- }
- }
- }
- if channelConfig.Filters.BlockedRoles != nil {
- member := m.Member
- if member == nil {
- member, _ = bot.GuildMember(m.GuildID, m.Author.ID)
- }
- if member != nil {
- for _, role := range member.Roles {
- if stringInSlice(role, *channelConfig.Filters.BlockedRoles) {
- shouldAbort = true
- if config.Debug {
- log.Println(lg("Debug", "Message", color.YellowString,
- "%s blockedRoles caught %s, planning to abort...",
- color.HiMagentaString("(FILTER)"), role))
- }
- break
- }
- }
- }
- }
- if channelConfig.Filters.AllowedRoles != nil {
- member := m.Member
- if member == nil {
- member, _ = bot.GuildMember(m.GuildID, m.Author.ID)
- }
- if member != nil {
- for _, role := range member.Roles {
- if stringInSlice(role, *channelConfig.Filters.AllowedRoles) {
- shouldAbort = false
- if config.Debug {
- log.Println(lg("Debug", "Message", color.YellowString,
- "%s allowedRoles caught %s, planning to allow...",
- color.HiMagentaString("(FILTER)"), role))
- }
- break
- }
- }
- }
- }
- // Abort
- if shouldAbort {
- if config.Debug {
- log.Println(lg("Debug", "Message", color.YellowString,
- "%s Filter decided to ignore message...",
- color.HiMagentaString("(FILTER)")))
- }
- return -1, 0
- }
- }
- // Process Files
- var downloadCount int64 = 0
- var totalfilesize int64 = 0
- files := getFileLinks(m)
- for _, file := range files {
- if file.Link == "" {
- continue
- }
- if config.Debug && (!history || config.MessageOutputHistory) {
- log.Println(lg("Debug", "Message", color.HiCyanString, "FOUND FILE: "+file.Link+fmt.Sprintf(" \t<%s>", m.ID)))
- }
- status, filesize := downloadRequestStruct{
- InputURL: file.Link,
- Filename: file.Filename,
- Path: channelConfig.Destination,
- Message: m,
- FileTime: file.Time,
- HistoryCmd: history,
- EmojiCmd: false,
- StartTime: time.Now(),
- }.handleDownload()
- if status.Status == downloadSuccess {
- downloadCount++
- totalfilesize += filesize
- }
- }
- return downloadCount, totalfilesize
- }
- return -1, 0
- }
- //#endregion
|