discord.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package util
  2. import (
  3. "strings"
  4. "github.com/ayntgl/discordgo"
  5. )
  6. // ChannelToString constructs a string representation of the given channel. The string representation may vary for different channel types.
  7. func ChannelToString(c *discordgo.Channel) string {
  8. var repr string
  9. if c.Name != "" {
  10. repr = "#" + c.Name
  11. } else if len(c.Recipients) == 1 {
  12. rp := c.Recipients[0]
  13. repr = rp.Username + "#" + rp.Discriminator
  14. } else {
  15. rps := make([]string, len(c.Recipients))
  16. for i, r := range c.Recipients {
  17. rps[i] = r.Username + "#" + r.Discriminator
  18. }
  19. repr = strings.Join(rps, ", ")
  20. }
  21. return repr
  22. }
  23. // ChannelIsUnread returns `true` if the given channel is marked as read by the client user, otherwise `false`.
  24. func ChannelIsUnread(s *discordgo.State, c *discordgo.Channel) bool {
  25. if c.LastMessageID == "" {
  26. return false
  27. }
  28. for _, rs := range s.ReadState {
  29. if c.ID == rs.ID {
  30. return c.LastMessageID != rs.LastMessageID
  31. }
  32. }
  33. return false
  34. }
  35. // 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.
  36. func FindMessageByID(ms []*discordgo.Message, mID string) (int, *discordgo.Message) {
  37. for i, m := range ms {
  38. if m.ID == mID {
  39. return i, m
  40. }
  41. }
  42. return -1, nil
  43. }