Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions bot/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ const { isAdmin } = require('./botUtils.js');
if (process.env.NODE_ENV === 'production' && !process.env.DEBUG_MODE) process.env.DEBUG_MODE = 0;

const client = new Discord.Client();
let toggle = false;
let interval = 0;

// Pre-load controllers
const controllers = require('./controllers')();

// Alert when ready
client.on('ready', () => {
util.log('Bot Online and Ready', 0);
util.log('Bot Online and Ready and willing to go!', 0);
});

// Listen for messages
client.on('message', (message) => {

// Create a ToS message for new members
const newMessage = message;
newMessage.content = message.type === 'GUILD_MEMBER_JOIN' ? '!terms' : new_message.content;
newMessage.content = message.type === 'GUILD_MEMBER_JOIN' ? '!terms' : newMessage.content;

// Check for ! prefix on message to ensure it is a command
if (newMessage.content.charAt(0) === '!') {
Expand Down Expand Up @@ -60,6 +62,42 @@ client.on('message', (message) => {
newMessage.reply(helpString);
}
}

// Listens for the $loop command and repeats a message every X seconds
if (newMessage.content.startsWith("!loop")) {
// Grab the message string and dissect it for the time, in seconds, and string to be repeated
let result = newMessage.content.substr(newMessage.content.indexOf(" ") + 1);
let result2 = result.substr(result.indexOf(" ") + 1);
result = result.split(" ");
result = Number(result[0]);
util.log(result);
util.log(result2);

// Check if the pre-existing toggle is true or false
// If the toggle is true, then set the toggle to false and turn off the loop
if (toggle !== false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can simplify with if (toggle) ...

clearInterval(interval);
newMessage.channel.send("Loop is off");
toggle = false;
} else if (toggle !== true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can simplify with else if (!toggle)...

// If the toggle is false, set it to true and check that the criteria for the command
// is correct.
if ((typeof result == 'number') && (result >= 5)) {
if (result2.length > 0) {
interval = setInterval(function () {
newMessage.channel.send(result2)
}, (result * 1000));
newMessage.channel.send("Loop is now on");
toggle = true;
} else {
newMessage.channel.send("please enter a message");
}
} else {
newMessage.channel.send("please input a time in seconds and make it greater than or equal to 5");
}
}
}

});

// Listen out for new users to the discord
Expand All @@ -76,4 +114,6 @@ client.on('guildMemberRemove', (member) => {
mainChannel.send(`${member} has left... it was nice knowing you! ;-;`);
});



client.login(process.env.TOKEN);