22
33// -- Dependencies
44const Discord = require ( 'discord.js' ) ;
5- const { prefix, token, adminRoles, queueTitle, useEmbeds } = require ( './config.json' ) ;
65const client = new Discord . Client ( ) ;
76const lodash = require ( 'lodash' ) ;
7+ const Config = ( exports . Config = require ( './config.json' ) ) ;
88
9- // -- Variables
9+ // #region Variables
1010// Variable to hold the ID for the queue message
1111let queueMessageId = 0 ;
1212// Variable to hold the queue message
1313let queueMessageText = '' ;
14+ // #endregion
1415
15- // -- Constants
16- // Discord Queue Bot version number
17- const dqbVersion = process . env . npm_package_version ;
18- // Constant to hold the title of the queue message
19- const queueMessageTitle = queueTitle ;
16+ // #region Constants
2017// Array to hold users in the queue
2118const usersInQueue = [ ] ;
2219// An embed for the the help command
2320const embedHelp = new Discord . MessageEmbed ( )
24- . setColor ( '#7ec699' )
25- . setTitle ( 'Queue Commands' )
26- . setAuthor ( 'Discord Queue Bot' , 'https://i.imgur.com/yAoS21k.png' , 'https://github.com/binlabs/discord-queue-bot' )
27- . setDescription ( 'A list of available commands is available below. Please note that some commands are reserved for queue administators that requre specific roles.' )
21+ . setColor ( Config . embedAccentColor )
22+ . setTitle ( Config . embedHelpTitle )
23+ . setAuthor ( Config . embedAuthorName , Config . embedAuthorImage , Config . embedAuthorLink )
24+ . setDescription ( Config . embedHelpDescription )
2825 . addFields (
29- { name : 'Join the queue' , value : '`!queue join`' , inline : false } ,
30- { name : 'Leave the queue' , value : '`!queue leave`' , inline : false } ,
31- { name : 'See how long you have been in the queue' , value : '`!queue time`' , inline : false } ,
32- { name : 'Get a list of available commands' , value : '`!queue commands` or `!queue help`' , inline : false } ,
26+ { name : 'Join the queue' , value : '`!queue join`' , inline : false } ,
27+ { name : 'Leave the queue' , value : '`!queue leave`' , inline : false } ,
28+ { name : 'See how long you have been in the queue' , value : '`!queue time`' , inline : false } ,
29+ { name : 'Get a list of available commands' , value : '`!queue commands` or `!queue help`' , inline : false } ,
3330 )
34- . setFooter ( `Running Discord Queue Bot v ${ dqbVersion } \nVisit https://github.com/binlabs/discord-queue-bot for more information.` ) ;
31+ . setFooter ( Config . embedFooterText + process . env . npm_package_version ) ;
3532// An embed template for the queue message
3633const embedQueue = new Discord . MessageEmbed ( )
37- . setColor ( '#7ec699' )
38- . setTitle ( queueMessageTitle )
39- . setAuthor ( 'Discord Queue Bot' , 'https://i.imgur.com/yAoS21k.png' , 'https://github.com/binlabs/discord-queue-bot' )
34+ . setColor ( Config . embedAccentColor )
35+ . setTitle ( Config . queueTitle )
36+ . setAuthor ( Config . embedAuthorName , Config . embedAuthorImage , Config . embedAuthorLink )
4037 . setDescription ( queueMessageText )
41- . setFooter ( `Running Discord Queue Bot v${ dqbVersion } \nVisit https://github.com/binlabs/discord-queue-bot for more information.` ) ;
38+ . setFooter ( Config . embedFooterText + process . env . npm_package_version ) ;
39+ // #endregion
4240
43- // -- Functions
41+ // #region Functions
4442/**
4543 * Create a User object
44+ * @param {string } username Discord Username
45+ * @param {string } userId Discord User ID
4646 */
4747function User ( username , userId ) {
48- // Discord Username
4948 this . username = username ;
50- // Discord User ID
5149 this . userId = userId ;
5250 // Timestamp representing when user was added to the queue
5351 this . timestamp = new Date ( ) ;
@@ -59,7 +57,7 @@ function User (username, userId) {
5957 * @param {string } [timeFormat=true] Format to return the time in
6058 * @returns {string } Amount of time user has been in queue
6159 */
62- function timeInQueue ( userId , timeFormat = 'minutes ' ) {
60+ function timeInQueue ( userId , timeFormat = 'm ' ) {
6361 const userIsInQueue = isUserInQueue ( userId ) ;
6462 const queuedUserObj = getUserFromQueueByUserId ( userId ) ;
6563 if ( userIsInQueue && queuedUserObj ) {
@@ -71,17 +69,17 @@ function timeInQueue (userId, timeFormat = 'minutes') {
7169 const diffHrs = Math . floor ( ( diffMs % 86400000 ) / 3600000 ) ;
7270 const diffMins = Math . round ( ( ( diffMs % 86400000 ) % 3600000 ) / 60000 ) ;
7371 switch ( timeFormat ) {
74- case 'minutes ' :
72+ case 'm ' :
7573 timeValueReturned = diffMins + ' minute(s)' ;
7674 break ;
77- case 'hours ' :
75+ case 'h ' :
7876 timeValueReturned = diffHrs + ' hour(s)' ;
7977 break ;
80- case 'days ' :
78+ case 'd ' :
8179 timeValueReturned = diffDays + ' day(s)' ;
8280 break ;
8381 default :
84- timeValueReturned = diffMs + ' millisecond (s)' ;
82+ timeValueReturned = diffMins + ' minute (s)' ;
8583 }
8684 return timeValueReturned ;
8785 }
@@ -90,8 +88,8 @@ function timeInQueue (userId, timeFormat = 'minutes') {
9088
9189/**
9290 * Add a User object to the usersInQueue array
93- * @param {string } username Discord name of user
94- * @param {string|number } userId Discord ID of user
91+ * @param {string } username Discord name of user
92+ * @param {string|number } userId Discord ID of user
9593 */
9694function addUserToQueue ( username , userId ) {
9795 // Check to see if the user already exists in the queue before adding
@@ -122,7 +120,7 @@ function removeUserFromQueueByUserId (userId) {
122120 * @returns {boolean } True if a user has an admin role, otherwise return false
123121 */
124122function isUserIsAdmin ( userRoles ) {
125- if ( userRoles . some ( r => adminRoles . includes ( r . name ) ) ) {
123+ if ( userRoles . some ( r => Config . adminRoles . includes ( r . name ) ) ) {
126124 return true ;
127125 }
128126 return false ;
@@ -164,18 +162,19 @@ function isUserInQueue (userToCheck, checkByUserId = true) {
164162 */
165163function assembleQueueMessage ( ) {
166164 // Set an empty string if using embeds, otherwise prepend the title before iteration
167- if ( useEmbeds ) {
165+ if ( Config . useEmbeds ) {
168166 queueMessageText = '' ;
169167 } else {
170- queueMessageText = queueMessageTitle ;
168+ queueMessageText = Config . queueTitle ;
171169 }
172170 // Iterate over each user object in the array and add them to the message
173171 usersInQueue . forEach ( function ( arrayObj ) {
174172 queueMessageText += '\n' + arrayObj . username ;
175173 } ) ;
176174}
175+ // #endregion functions
177176
178- // -- Event Listeners
177+ // #region Event Listeners
179178// Listen for the ready state
180179client . on ( 'ready' , ( ) => {
181180 // Bot is running and ready
@@ -185,10 +184,10 @@ client.on('ready', () => {
185184// Listen for messages
186185client . on ( 'message' , message => {
187186 // If the message doesn't contain our prefix, or if the message is from the bot, ignore it
188- if ( ! message . content . startsWith ( prefix ) || message . author . bot ) return ;
187+ if ( ! message . content . startsWith ( Config . prefix ) || message . author . bot ) return ;
189188
190189 // Split the arguments from the command for parsing
191- const args = message . content . slice ( prefix . length ) . split ( / + / ) ;
190+ const args = message . content . slice ( Config . prefix . length ) . split ( / + / ) ;
192191 // Set the command to a constant for brevity
193192 const command = args . shift ( ) . toLowerCase ( ) ;
194193 // Set the subcommand to false so we can check if it set in the future
@@ -207,14 +206,14 @@ client.on('message', message => {
207206 message . channel . send ( 'A queue already exists.' ) ;
208207 return ;
209208 }
210- if ( useEmbeds ) {
209+ if ( Config . useEmbeds ) {
211210 message . channel . send ( embedQueue ) . then ( queueMessage => {
212211 console . log ( 'Queue Created using an embed' ) ;
213212 // Set the queueMessageId to the ID of the message the bot just created
214213 queueMessageId = queueMessage . id ;
215214 } ) ;
216215 } else {
217- message . channel . send ( queueMessageTitle ) . then ( queueMessage => {
216+ message . channel . send ( Config . queueTitle ) . then ( queueMessage => {
218217 console . log ( 'Queue Created using a plain-text message' ) ;
219218 // Set the queueMessageId to the ID of the message the bot just created
220219 queueMessageId = queueMessage . id ;
@@ -232,7 +231,7 @@ client.on('message', message => {
232231 message . channel . messages . fetch ( queueMessageId ) . then ( queueMessage => {
233232 if ( addUserToQueue ( message . member . displayName , message . member . id , args ) ) {
234233 assembleQueueMessage ( ) ;
235- if ( useEmbeds ) {
234+ if ( Config . useEmbeds ) {
236235 queueMessage . edit ( embedQueue . setDescription ( queueMessageText ) ) ;
237236 } else {
238237 queueMessage . edit ( queueMessageText ) ;
@@ -259,7 +258,7 @@ client.on('message', message => {
259258 message . channel . send ( `${ message . author } wasn't in the queue` ) ;
260259 } else {
261260 assembleQueueMessage ( ) ;
262- if ( useEmbeds ) {
261+ if ( Config . useEmbeds ) {
263262 queueMessage . edit ( embedQueue . setDescription ( queueMessageText ) ) ;
264263 } else {
265264 queueMessage . edit ( queueMessageText ) ;
@@ -279,12 +278,11 @@ client.on('message', message => {
279278 userToAdd = userToAdd . replace ( / \D / g, '' ) ;
280279 }
281280 message . channel . messages . fetch ( queueMessageId ) . then ( queueMessage => {
282- const userToAddObj = client . users . fetch ( userToAdd ) . then ( userSnowflake => {
283- console . log ( userToAddObj ) ;
281+ client . users . fetch ( userToAdd ) . then ( userSnowflake => {
284282 const userAdded = addUserToQueue ( userSnowflake . username , userSnowflake . id ) ;
285283 if ( userAdded ) {
286284 assembleQueueMessage ( ) ;
287- if ( useEmbeds ) {
285+ if ( Config . useEmbeds ) {
288286 queueMessage . edit ( embedQueue . setDescription ( queueMessageText ) ) ;
289287 } else {
290288 queueMessage . edit ( queueMessageText ) ;
@@ -309,7 +307,7 @@ client.on('message', message => {
309307 const userRemoved = removeUserFromQueueByUserId ( userToRemove ) ;
310308 console . log ( userRemoved ) ;
311309 assembleQueueMessage ( ) ;
312- if ( useEmbeds ) {
310+ if ( Config . useEmbeds ) {
313311 queueMessage . edit ( embedQueue . setDescription ( queueMessageText ) ) ;
314312 } else {
315313 queueMessage . edit ( queueMessageText ) ;
@@ -345,9 +343,9 @@ client.on('message', message => {
345343 // command: !queue help
346344 if ( command === 'queue' && ( subcommand === 'help' || subcommand === 'commands' ) ) {
347345 message . channel . send ( embedHelp ) ;
348- //message.channel.send('**Queue Commands**:\n`!queue join`\tJoin the queue\n`!queue leave`\tLeave the queue\n`!queue help` _alias:_ `!queue commands`\tView a list of available commands.\n`!queue time`\tCheck how long you\'ve been in the queue');
346+ // message.channel.send('**Queue Commands**:\n`!queue join`\tJoin the queue\n`!queue leave`\tLeave the queue\n`!queue help` _alias:_ `!queue commands`\tView a list of available commands.\n`!queue time`\tCheck how long you\'ve been in the queue');
349347 }
350348} ) ;
349+ // #endregion
351350
352- // TODO: Create an ENV Variable for this at some point so we don't have to store it in the repo
353- client . login ( token ) ;
351+ client . login ( Config . token ) ;
0 commit comments