discord.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package util
  2. import "github.com/ayntgl/discordgo"
  3. // FindMessageByID returns the index and the `*Message` struct of the current message if the given message ID *mID* is equal to the current message ID. If the given message ID *mID* is not found in the given slice *ms*, `-1` and `nil` are returned instead.
  4. func FindMessageByID(ms []*discordgo.Message, mID string) (int, *discordgo.Message) {
  5. for i, m := range ms {
  6. if m.ID == mID {
  7. return i, m
  8. }
  9. }
  10. return -1, nil
  11. }
  12. // ChannelIsUnread returns `true` if the given channel is marked as read by the client user, otherwise `false`.
  13. func ChannelIsUnread(s *discordgo.State, c *discordgo.Channel) bool {
  14. if c.LastMessageID == "" {
  15. return false
  16. }
  17. for _, rs := range s.ReadState {
  18. if c.ID == rs.ID {
  19. return c.LastMessageID != rs.LastMessageID
  20. }
  21. }
  22. return false
  23. }
  24. // HasPermission returns a boolean that indicates whether the client user has the given permission *p* in the given channel ID *cID*.
  25. func HasPermission(s *discordgo.State, cID string, p int64) bool {
  26. perm, err := s.UserChannelPermissions(s.User.ID, cID)
  27. if err != nil {
  28. return false
  29. }
  30. return perm&p == p
  31. }