classes/client/client.js

  1. const { EventEmitter } = require('events')
  2. const ClientWebSocket = require('./webSocket.js')
  3. /**
  4. * The main class for the Guilded.js library
  5. * @class Client
  6. * @extends EventEmitter
  7. * @param {String} token The token of the bot
  8. */
  9. class Client extends EventEmitter {
  10. /**
  11. * Creates a new bot instance.
  12. * See {@tutorial example-bot} for an example
  13. * @param {String} token Your guilded bot's Auth Token
  14. * @param {Object} [options] Options for the client
  15. * @param {String} [options.maxReconnectTries=Infinity] The maximum number of times to retry a request or reconnect
  16. * @returns {Client}
  17. * @example
  18. * const { Client } = require('guilded.js');
  19. * const client = new Client('token');
  20. */
  21. constructor (token = String, options = Object) {
  22. super()
  23. /**
  24. * The token of the bot
  25. * @type {String}
  26. * @readonly
  27. */
  28. this.token = token
  29. /**
  30. * The websocket of the bot
  31. * @type {ClientWebSocket}
  32. * @readonly
  33. */
  34. this.ws = null
  35. /**
  36. * The options of the bot
  37. * @type {Object}
  38. * @readonly
  39. */
  40. this.options = options
  41. }
  42. /**
  43. * Connects the bot to the Guilded API
  44. * @returns {Promise<void>}
  45. * @example
  46. * client.login();
  47. * @example
  48. * client.login().then(() => {
  49. * console.log('Bot is ready!');
  50. * });
  51. */
  52. login () {
  53. /**
  54. * Create a new WebSocket connection
  55. * @param {Client} client
  56. * @returns {ClientWebSocket}
  57. * @constructor
  58. * @private
  59. * @ignore
  60. */
  61. this.ws = new ClientWebSocket(this)
  62. /**
  63. * Connects the bot to the Guilded API
  64. * @returns {Promise<void>}
  65. * @example
  66. * client.ws.connect();
  67. * @example
  68. * client.ws.connect().then(() => {
  69. * console.log('Bot is ready!');
  70. * });
  71. * @private
  72. * @ignore
  73. */
  74. this.ws.connect()
  75. /**
  76. * Emitted when the bot is ready
  77. * @event Client#ready
  78. * @param {Client} client The client that emitted the event
  79. * @example
  80. * client.on('ready', () => {
  81. * console.log('Bot is ready!');
  82. * });
  83. */
  84. this.ws.on('clientReady', (client) => {
  85. this.emit('ready', client)
  86. })
  87. /**
  88. * Emitted when the bot receives a message
  89. * @event Client#message
  90. * @param {Message} message
  91. * @example
  92. * client.on('serverMessageCreate', (message) => {
  93. * console.log(message.content);
  94. * });
  95. */
  96. this.ws.on('messageCreated', (message) => {
  97. this.emit('serverMessageCreate', message)
  98. })
  99. /**
  100. * Emitted when the bot receives a updated message
  101. * @event Client#messageUpdate
  102. * @param {Message} message
  103. * @example
  104. * client.on('serverMessageUpdate', (message) => {
  105. * console.log(message.content);
  106. * });
  107. */
  108. this.ws.on('messageUpdated', (message) => {
  109. this.emit('serverMessageUpdate', message)
  110. })
  111. /**
  112. * Emitted when the bot receives a deleted message
  113. * @event Client#messageDelete
  114. * @param {Message} message
  115. * @example
  116. * client.on('serverMessageDelete', (message) => {
  117. * console.log(message.content);
  118. * });
  119. */
  120. this.ws.on('messageDeleted', (message) => {
  121. this.emit('serverMessageDelete', message)
  122. })
  123. /**
  124. * Emitted when the bot receives a member join
  125. * @event Client#memberJoin
  126. * @param {Member} member
  127. * @example
  128. * client.on('serverMemberJoin', (member) => {
  129. * console.log(member.nickname);
  130. * });
  131. */
  132. this.ws.on('memberAdded', (member) => {
  133. this.emit('serverMemberJoin', member)
  134. })
  135. /**
  136. * Emitted when the bot receives a member leave
  137. * @event Client#memberLeave
  138. * @param {MemberRemoved} member
  139. * @example
  140. * client.on('serverMemberLeft', (member) => {
  141. * console.log(member.username);
  142. * });
  143. */
  144. this.ws.on('memberRemoved', (member) => {
  145. this.emit('serverMemberLeft', member)
  146. })
  147. /**
  148. * Emitted when the bot receives a member ban
  149. * @event Client#memberBan
  150. * @param {MemberBan} memberBan
  151. * @example
  152. * client.on('serverMemberBan', (member) => {
  153. * console.log(member.nickname);
  154. * });
  155. */
  156. this.ws.on('memberBanned', (member) => {
  157. this.emit('serverMemberBan', member)
  158. })
  159. /**
  160. * Emitted when the bot receives a member unban
  161. * @event Client#memberUnban
  162. * @param {MemberBan} memberBan
  163. * @example
  164. * client.on('serverMemberUnban', (member) => {
  165. * console.log(member.nickname);
  166. * });
  167. */
  168. this.ws.on('memberUnbanned', (member) => {
  169. this.emit('serverMemberUnban', member)
  170. })
  171. /**
  172. * Emitted when the bot receives a member update
  173. * @event Client#memberUpdate
  174. * @param {MemberUpdated} member
  175. * @example
  176. * client.on('serverMemberUpdate', (member) => {
  177. * console.log(member.username);
  178. * });
  179. */
  180. this.ws.on('memberUpdated', (member) => {
  181. this.emit('serverMemberUpdate', member)
  182. })
  183. /**
  184. * Emitted when the bot receives a member role update
  185. * @event Client#memberRoleUpdate
  186. * @param {RolesUpdated} rolesUpdated
  187. * @example
  188. * client.on('serverMemberUpdate', (rolesUpdated) => {
  189. * console.log(rolesUpdated[0]);
  190. * });
  191. */
  192. this.ws.on('memberRolesUpdated', (rolesUpdated) => {
  193. this.emit('serverMemberUpdate', rolesUpdated)
  194. })
  195. /**
  196. * Emitted when the bot receives a webhook create
  197. * @event Client#webhookCreate
  198. * @param {Webhook} webhook
  199. * @example
  200. * client.on('serverWebhookCreate', (webhook) => {
  201. * console.log(webhook.name);
  202. * });
  203. */
  204. this.ws.on('webhookCreated', (webhook) => {
  205. this.emit('serverWebhookCreate', webhook)
  206. })
  207. /**
  208. * Emitted when the bot receives a webhook update
  209. * @event Client#webhookUpdate
  210. * @param {Webhook} webhook
  211. * @example
  212. * client.on('serverWebhookUpdate', (webhook) => {
  213. * console.log(webhook.name);
  214. * });
  215. */
  216. this.ws.on('webhookUpdated', (webhook) => {
  217. this.emit('serverWebhookUpdate', webhook)
  218. })
  219. /**
  220. * Emitted when the bot receives a reaction add
  221. * @event Client#reactionAdd
  222. * @param {Reaction} reaction
  223. * @example
  224. * client.on('messageReactionCreated', (reaction) => {
  225. * console.log(reaction.emoteId);
  226. * });
  227. */
  228. this.ws.on('messageReactionCreated', (reaction) => {
  229. this.emit('messageReactionCreated', reaction)
  230. })
  231. /**
  232. * Emitted when the bot receives a reaction remove
  233. * @event Client#reactionRemove
  234. * @param {Reaction} reaction
  235. * @example
  236. * client.on('messageReactionDeleted', (reaction) => {
  237. * console.log(reaction.emoteId);
  238. * });
  239. */
  240. this.ws.on('ChannelMessageReactionDeleted', (reaction) => {
  241. this.emit('ChannelMessageReactionDeleted', reaction)
  242. })
  243. /**
  244. * Emitted when the bot receives a closed event
  245. * @event Client#closed
  246. * @example
  247. * client.on('closed', () => {
  248. * console.log('Bot is closed!');
  249. * });
  250. */
  251. this.ws.on('closed', () => {
  252. this.emit('disconnect')
  253. })
  254. /**
  255. * Emitted when the bot receives a error event
  256. * @event Client#error
  257. * @param {Error} error
  258. * @example
  259. * client.on('error', (error) => {
  260. * console.log(error);
  261. * });
  262. */
  263. this.ws.on('clientError', (error) => {
  264. this.emit('error', error)
  265. })
  266. /**
  267. * Emitted when the bot receives a debug event
  268. * @event Client#debug
  269. * @param {String} message
  270. * @example
  271. * client.on('debug', (message) => {
  272. * console.log(message);
  273. * });
  274. */
  275. this.ws.on('clientDebug', (message) => {
  276. this.emit('debug', message)
  277. })
  278. /**
  279. * Delete the client
  280. * @event Client#destroy
  281. * @example
  282. * client.destroy();
  283. */
  284. this.destroy = async () => {
  285. this.emit('destroy')
  286. await this.ws.close()
  287. }
  288. }
  289. }
  290. module.exports = Client