util.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package ui
  2. import (
  3. "strings"
  4. "github.com/diamondburned/arikawa/v3/discord"
  5. "github.com/diamondburned/arikawa/v3/state"
  6. )
  7. func channelToString(c discord.Channel) string {
  8. var repr string
  9. switch c.Type {
  10. case discord.GuildText:
  11. repr = "#" + c.Name
  12. case discord.DirectMessage:
  13. rp := c.DMRecipients[0]
  14. repr = rp.Username + "#" + rp.Discriminator
  15. case discord.GroupDM:
  16. repr = c.Name
  17. // if the name wasn't loaded, use it as a backup
  18. if repr == "" {
  19. rps := make([]string, len(c.DMRecipients))
  20. for i, r := range c.DMRecipients {
  21. rps[i] = r.Username + "#" + r.Discriminator
  22. }
  23. repr = strings.Join(rps, ", ")
  24. }
  25. default:
  26. repr = c.Name
  27. }
  28. return repr
  29. }
  30. func findMessageByID(ms []discord.Message, mID discord.MessageID) (int, *discord.Message) {
  31. for i, m := range ms {
  32. if m.ID == mID {
  33. return i, &m
  34. }
  35. }
  36. return -1, nil
  37. }
  38. func channelIsInDMCategory(c *discord.Channel) bool {
  39. return c.Type == discord.DirectMessage || c.Type == discord.GroupDM
  40. }
  41. func hasPermission(s *state.State, cID discord.ChannelID, p discord.Permissions) bool {
  42. perm, err := s.Permissions(cID, s.Ready().User.ID)
  43. if err != nil {
  44. return false
  45. }
  46. return perm&p == p
  47. }