A continuation of vrobot3.
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.

82 lines
2.3KB

  1. class Mod_DTL < Vrobot4::Module::Module
  2. def self.type() "DTL" end
  3. Vrobot4::Module.add_module_type self, server: "Discord"
  4. def initialize info
  5. super
  6. register :c_setnotify, "setnotify", "Adds you to a notification group."
  7. register :c_unsetnotify, "unsetnotify", "Removes you from a notification group."
  8. register :c_listnotify, "listnotify", "Lists available notification groups."
  9. register :c_notify, "notify", "Notifies a notification group."
  10. @member_id = @info[:member_id].resolve_id
  11. @group_ids = @info[:group_ids].map! do |ids|
  12. {not: ids[:not].resolve_id,
  13. dev: ids[:dev].resolve_id}
  14. end
  15. end
  16. def ensure_role m
  17. if m.chan.real.server
  18. m.user.real.add_role @member_id unless m.user.real.role? @member_id
  19. end
  20. end
  21. def get_notification_roles m, argv
  22. roles = m.chan.real.server.roles
  23. role = roles.find do |r| r.name.casecmp(argv) == 0 end
  24. ids = @group_ids.detect do |ids| role.id == ids[:not] end if role
  25. unless ids
  26. raise ArgumentError, "Invalid notification group, use `.listnotify` for a list of available groups."
  27. end
  28. ids
  29. end
  30. def on_message m
  31. ensure_role m
  32. super m
  33. end
  34. def on_command m, cnam, argv
  35. ensure_role m
  36. super m, cnam, argv
  37. end
  38. def c_setnotify m, argv
  39. ensure_role m
  40. m.user.real.add_role get_notification_roles(m, argv)[:not]
  41. m.reply "Successfully added you to the notification group."
  42. end
  43. def c_unsetnotify m, argv
  44. m.user.real.remove_role get_notification_roles(m, argv)[:not]
  45. m.reply "Successfully removed you from the notification group."
  46. end
  47. def c_listnotify m, argv
  48. text = ""
  49. for ids in @group_ids do
  50. text << m.chan.real.server.role(ids[:not]).name + ?\n
  51. end
  52. m.reply text
  53. end
  54. def c_notify m, argv
  55. argv = argv.split ?,, 2
  56. ids = get_notification_roles m, argv[0]
  57. if m.user.real.role? ids[:dev]
  58. notif = m.chan.real.server.role ids[:not]
  59. if argv[1]
  60. m.reply "#{notif.mention} - #{argv[1].strip}"
  61. else
  62. m.reply notif.mention
  63. end
  64. else
  65. m.reply "You don't have permission for that."
  66. end
  67. end
  68. end
  69. ## EOF