func fetchPrimaryTextChannelID(sess *discordgo.Session) string { var channelid string retryOnBadGateway(func() error { guilds, err := sess.UserGuilds() if err != nil { return err } guild, err := sess.Guild(guilds[0].ID) if err != nil { return err } channels, err := sess.GuildChannels(guild.ID) if err != nil { return err } for _, channel := range channels { channel, err = sess.Channel(channel.ID) if err != nil { return err } if channel.Type == "text" { channelid = channel.ID return nil } } return errors.New("No primary channel found") }) return channelid }
// the below method is invoked when the Discord Websocket API transmits // a 'messageCreate' event to the bot func onMessageCreate(s *discordgo.Session, event *discordgo.MessageCreate) { logrus.WithFields(logrus.Fields{ "authorId": event.Author.ID, "authorUsername": event.Author.Username, "channelId": event.ChannelID, "timestamp": event.Timestamp, "content": event.Content, }).Debug("Received message create event from Discord Websocket API.") // do not take action if the author of the message is ourself if s.State.Ready.User.ID == event.Author.ID { return } // ensure the message was not received via direct message currChannel, err := s.Channel(event.ChannelID) if err != nil { logrus.WithFields(logrus.Fields{ "channelId": event.ChannelID, "capturedError": err, }).Error("Could not retrieve channel object from identifier.") return } if currChannel.IsPrivate { s.ChannelMessageSend(event.ChannelID, "Hi! I don't respond to commands sent through direct message, as I cannot tell what Discord server you are trying to set on! Please send me a message in the appropriate text channel on a Discord server to try again.") return } // convert the text to lowercase to avoid case-sensitivity issues lowerContent := strings.ToLower(event.ContentWithMentionsReplaced()) if strings.HasPrefix(lowerContent, "!setteam ") { givenTeamName := lowerContent[9:] go handleTeamSet(s, &givenTeamName, &event.Author.ID, &event.ChannelID) } }
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.Bot { return } chanl, err := s.Channel(m.ChannelID) if err != nil { return } guild, _ := s.Guild(chanl.GuildID) var author *discordgo.Member if guild != nil { author, _ = s.GuildMember(guild.ID, m.Author.ID) } checkLink(s, m.Content, chanl.ID, chanl.LastMessageID) messageContents <- m if len(m.Content) > 21 && len(m.Mentions) > 0 && m.Mentions[len(m.Mentions)-1].Username == "MrDestructoid" { arr := strings.Fields(m.Content) switch arr[1] { case "BD": bdLinks(s, m.ChannelID) // case "state": // for _, i := range guild.VoiceStates { // fmt.Println(i.UserID) // } // case "bot": // var mem runtime.MemStats // runtime.ReadMemStats(&mem) // fmt.Println(mem.HeapAlloc) case "myid": s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("```\n%v\n```", m.Author.ID)) // //---------------- NO WAY TO UTILIZE ----------- // case "joinchannel": // ch := make(chan string) // fmt.Println(strings.Join(arr[2:], " ")) // if findNjoinChannel(s, guild.ID, guild.Channels, // strings.Join(arr[2:], " "), ch) { // echo(dgv, ch) // fmt.Println("opened") // } // //---------------- NO WAY TO UTILIZE ---------- case "channels": var str string = fmt.Sprintf("\n%-20s\t%-5s\tID", "Name", "Type") for _, c := range guild.Channels { str += fmt.Sprintf("\n%-20s\t%-5s\t%s", c.Name, c.Type, c.ID) } s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("```%s```", str)) case "updateroles": updateRolesToDB(s, guild.ID) case "guild": var owner string for _, i := range guild.Members { if i.User.ID == guild.OwnerID { owner = i.User.Username } } s.ChannelMessageSend(m.ChannelID, fmt.Sprintf( "```**Guild information** \nName: %s\nOwner: %s\nID: %s\nRegion: %s\nHas members: %d```", guild.Name, owner, guild.ID, guild.Region, len(guild.Members))) case "quit": if checkForPermissions(s, chanl.ID, author.Roles) { quit = true } case "ban": banUser(s, guild, author, m.ChannelID, arr) case "unban": manualUnban(s, chanl.ID, author.Roles, arr) case "bannedusers": bannedUsers(s, m) default: s.ChannelMessageSend(chanl.ID, fmt.Sprintf("No such command")) } } }
func handleTeamSet(s *discordgo.Session, givenTeamName *string, authorID *string, channelID *string) { // validate a team option was provided to the command if *givenTeamName == "" { s.ChannelMessageSend(*channelID, "A team must be provided to this command.") return } // validate the team provided matches a valid option hasTeam := false for _, team := range teams { if *givenTeamName == team { hasTeam = true } } if !hasTeam { s.ChannelMessageSend(*channelID, "The team selected ("+*givenTeamName+") does not exist. Valid options are: "+strings.Join(teams, ", ")+".") return } currChannel, err := s.Channel(*channelID) if err != nil { logrus.WithFields(logrus.Fields{ "channelId": *channelID, "capturedError": err, }).Error("Could not parse channel identifier into channel object.") s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.") return } currGuild, err := s.Guild(currChannel.GuildID) if err != nil { logrus.WithFields(logrus.Fields{ "guildId": currChannel.GuildID, "capturedError": err, }).Error("Could not parse guild identifier into guild object.") s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.") return } currMember, err := s.GuildMember(currGuild.ID, *authorID) if err != nil { logrus.WithFields(logrus.Fields{ "guildId": currGuild.ID, "userId": *authorID, "capturedError": err, }).Error("Could not parse guild identifier and user identifier into guild member object.") s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.") return } // ensure they are not an existing member of a team for _, roleID := range currMember.Roles { var role *discordgo.Role for _, guildRole := range currGuild.Roles { if guildRole.ID == roleID { role = guildRole } } if role == nil { logrus.WithFields(logrus.Fields{ "roleId": roleID, }).Error("Could not retrieve role object from identifier.") s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.") return } for _, teamName := range teams { if role.Name == "Team "+strings.Title(teamName) { logrus.WithFields(logrus.Fields{ "roleName": role.Name, "roleId": role.ID, "userId": currMember.User.ID, "userName": currMember.User.Username, "guildId": currGuild.ID, }).Warn("Attempted team switch detected.") s.ChannelMessageSend(*channelID, "You are already a member of a team and cannot switch now. Ask an administrator if you have made a mistake selecting teams.") return } } } // add the role to the user teamTitle := strings.Title(*givenTeamName) for _, role := range currGuild.Roles { if role.Name == "Team "+teamTitle { err := s.GuildMemberEdit(currGuild.ID, *authorID, append(currMember.Roles, role.ID)) if err != nil { logrus.WithFields(logrus.Fields{ "guildId": currGuild.ID, "userId": *authorID, "roleId": role.ID, "capturedError": err, }).Error("Could not add team role to user.") s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.") return } s.ChannelMessageSend(*channelID, "You have been set to Team "+teamTitle+".") } } }