Simple bridge server for various bots of mine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.4 KiB

2 years ago
  1. require "date"
  2. def validate(*a)
  3. # Check arguments against either a list of acceptable classes or just a class
  4. raise ArgumentError, "expected an even number of arguments" if a.count%2 != 0
  5. for i in (0..a.count-1).step(2) do
  6. if a[i+1].kind_of?(Class) then
  7. raise TypeError, "expected argument #{i/2} of type #{a[i+1].to_s}" unless a[i].kind_of?(a[i+1])
  8. elsif a[i+1].kind_of?(Array) then
  9. raise TypeError, "expected argument #{i/2} of any of the types #{a[i+1].to_s}" unless a[i+1].include?(a[i].class)
  10. end
  11. end
  12. end
  13. module Heimdall
  14. # Core protocol
  15. class NetEntity
  16. # Abstraction that stores basic object properties
  17. @createdAt
  18. def initialize()
  19. @createdAt = DateTime.new
  20. end
  21. attr_reader :createdAt
  22. end
  23. class User < NetEntity
  24. # User abstraction (not bound to a group chat)
  25. @username
  26. @nickname
  27. def initialize(username,nickname = nil)
  28. validate(
  29. username, String,
  30. nickname, [String,NilClass]
  31. )
  32. super()
  33. nickname = username if not nickname
  34. @username = username
  35. @nickname = nickname
  36. end
  37. attr_reader :username
  38. attr_accessor :nickname
  39. end
  40. class Channel < NetEntity
  41. # Channel abstraction (not bound to a group)
  42. # Practically acts as a message stack
  43. # Read access to all elements
  44. # Write access only to top
  45. @messages
  46. def initialize()
  47. validate(
  48. id, String
  49. )
  50. super()
  51. @messages = []
  52. end
  53. def [](index)
  54. @messages[id]
  55. end
  56. def <<(message)
  57. @messages.append(message)
  58. end
  59. def top(count = 1)
  60. return @messages[-1] if count == 1
  61. @messages[(-1*count)..-1]
  62. end
  63. def filter(&block)
  64. @messages.filter(block)
  65. end
  66. def snapshot()
  67. @messages.clone
  68. end
  69. end
  70. class Message < NetEntity
  71. # Message abstraction with edits and content history (not bound to a group)
  72. @content
  73. @author
  74. @editedAt
  75. @contentHist
  76. def initialize(content,author,channel)
  77. validate(
  78. content, String,
  79. author, Heimdall::User,
  80. channel, Heimdall::Channel
  81. )
  82. super()
  83. @editedAt = DateTime.new
  84. @contentHist = [content]
  85. @content = content
  86. @author = author
  87. end
  88. def edit(content)
  89. @content = content
  90. @editedAt = DateTime.new
  91. @contentHist.append(content)
  92. end
  93. attr_reader :content
  94. attr_reader :author
  95. attr_reader :contentHist
  96. attr_reader :editedAt
  97. end
  98. end