當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Session.GuildMember方法代碼示例

本文整理匯總了Golang中github.com/bwmarrin/discordgo.Session.GuildMember方法的典型用法代碼示例。如果您正苦於以下問題:Golang Session.GuildMember方法的具體用法?Golang Session.GuildMember怎麽用?Golang Session.GuildMember使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/bwmarrin/discordgo.Session的用法示例。


在下文中一共展示了Session.GuildMember方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: getMentionableRoles

func getMentionableRoles(s *discordgo.Session, GuildID string) []*discordgo.Role {
	g, err := s.State.Guild(GuildID)
	if err != nil {
		fmt.Println(err)
		return nil
	}

	m, err := s.GuildMember(GuildID, s.State.User.ID)
	if err != nil {
		fmt.Println(err)
		return nil
	}

	roleID := ""
	rolePos := 0

	if len(m.Roles) >= 1 {
		roleID = m.Roles[0]
	}

	for _, role := range g.Roles {
		if role.ID == roleID {
			rolePos = role.Position
			break
		}
	}

	var roles []*discordgo.Role

	for _, role := range g.Roles {
		if role.Name == "@everyone" || !role.Mentionable || role.Position > rolePos {
			continue
		}

		roles = append(roles, role)
	}

	return roles
}
開發者ID:softashell,項目名稱:lewdbot-discord,代碼行數:39,代碼來源:roles.go

示例2: messageCreate

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"))
		}

	}
}
開發者ID:Muckfoot,項目名稱:Go,代碼行數:92,代碼來源:basic_bot.go

示例3: addRole

func addRole(s *discordgo.Session, GuildID string, UserID string, arg string) string {
	g, err := s.State.Guild(GuildID)
	if err != nil {
		fmt.Println(err)
		return "You fucking broke it~"
	}

	exists, role := roleExists(g, arg)

	if !exists {
		if !strings.HasPrefix(arg, "yes") {
			return fmt.Sprintf("I can't find such group~ Are you sure you didn't mistype it? Say **!subscribe yes %s** to create a new one~", arg)
		}

		if len(arg) < 5 {
			return "Are you sure you actually typed a name?~"
		}

		arg = arg[4:]

		exists, role = roleExists(g, arg)

		if !exists {
			newRole, err := s.GuildRoleCreate(GuildID)

			if err != nil {
				fmt.Println(err)
				return "Failed to create role"
			}

			role, err = s.GuildRoleEdit(GuildID, newRole.ID, arg, newRole.Color, newRole.Hoist, 37080064, true)
			if err != nil {
				fmt.Println(err)

				err = s.GuildRoleDelete(GuildID, newRole.ID)

				if err != nil {
					fmt.Println(err)
				}

				return "You fucking broke it~"
			}
			fmt.Println(role)
		} else {
			return "Why are you trying to recreate that group?"
		}
	}

	member, err := s.GuildMember(GuildID, UserID)
	if err != nil {
		fmt.Println(err)
		return ""
	}

	if exists {
		for _, _role := range member.Roles {
			if _role == role.ID {
				return fmt.Sprintf("You're already subscribed to %s ~", arg)
			}
		}
	}

	newRoles := append(member.Roles, role.ID)

	err = s.GuildMemberEdit(GuildID, UserID, newRoles)
	if err != nil {
		fmt.Println(err)
		return "I can't touch that group dude, do it yourself~"
	}

	if exists {
		return fmt.Sprintf("You're now subscribed to %s~", arg)
	}

	return fmt.Sprintf("Created and subscribed to %s", arg)
}
開發者ID:softashell,項目名稱:lewdbot-discord,代碼行數:76,代碼來源:roles.go

示例4: removeRole

func removeRole(s *discordgo.Session, GuildID string, UserID string, arg string) string {
	fmt.Println("removeRole", arg)

	g, err := s.State.Guild(GuildID)
	if err != nil {
		fmt.Println(err)
		return "You fucking broke it~"
	}

	exists, role := roleExists(g, arg)
	if !exists {
		return "I can't find such group~"
	}

	fmt.Println("Found?", exists, role)

	member, err := s.GuildMember(GuildID, UserID)
	if err != nil {
		fmt.Println(err)
	}

	pos := -1
	for i, r := range member.Roles {
		if r == role.ID {
			pos = i
			break
		}
	}

	if pos < 0 {
		return fmt.Sprintf("You're already not subscribed to %s~", arg)
	}

	member.Roles = append(member.Roles[:pos], member.Roles[pos+1:]...)
	err = s.GuildMemberEdit(GuildID, UserID, member.Roles)
	if err != nil {
		fmt.Println(err)
		return "I can't touch that group dude, do it yourself~"
	}

	delete := true
	for _, member := range g.Members {
		if member.User.ID == UserID {
			continue // Ignore self since it's not updated here yet
		}

		for _, r := range member.Roles {
			if r == role.ID {
				delete = false
				break
			}
		}
	}

	fmt.Println("Should delete it?", delete)

	if delete {
		err := s.GuildRoleDelete(GuildID, role.ID)
		if err != nil {
			fmt.Println(err)
			return fmt.Sprintf("Unsubscribed from but failed to delete %s~", arg)
		}

		fmt.Println("Unsubscribed and deleted")
		return fmt.Sprintf("Unsubscribed from and deleted %s~", arg)
	}

	fmt.Println("Unsubscribed")
	return fmt.Sprintf("Unsubscribed from %s~", arg)
}
開發者ID:softashell,項目名稱:lewdbot-discord,代碼行數:70,代碼來源:roles.go

示例5: handleTeamSet

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+".")
		}
	}
//.........這裏部分代碼省略.........
開發者ID:RyanMorrison04,項目名稱:pokemon-discord,代碼行數:101,代碼來源:pokemon.go


注:本文中的github.com/bwmarrin/discordgo.Session.GuildMember方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。