Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Discord = require('discord.js');
- const client = new Discord.Client();
- client.on('message', interpretMessage);
- secretToken = "Nice try Kyle";
- client.login(secretToken).catch(console.log);
- function interpretMessage(receivedMessage)
- {
- try
- {
- content = receivedMessage.content;
- if (receivedMessage.author != client.user && content.includes("@"))
- {
- console.log(`Interpreting message "${content}"`);
- potentialPings = content.match(/@[^@#:]*(?=\s+|$)/g);
- console.log(potentialPings);
- guildMembers = receivedMessage.guild.members.array();
- originalMentionedUsers = receivedMessage.mentions;
- usersToMention = [];
- messageToPost = "";
- for (let ping of potentialPings)
- {
- for (let member of guildMembers)
- {
- regexPattern = getRegExpFromNickname(member.displayName)
- if(regexPattern)
- {
- pingWithoutAtSymbol = ping.substring(1)
- execResults = regexPattern.exec(pingWithoutAtSymbol)
- if(execResults && execResults[0] == pingWithoutAtSymbol && !(member.user in originalMentionedUsers) && !(member in usersToMention))
- {
- usersToMention.push(member);
- }
- }
- }
- }
- messageToPost = usersToMention.join(" ");
- console.log(`Posting "${messageToPost}"`);
- if(messageToPost)
- {
- receivedMessage.channel.send(messageToPost);
- }
- else
- {
- console.log("Empty message");
- }
- }
- }
- catch(e)
- {
- console.log(e);
- }
- }
- // //Returns a RegExp object of nickname if it can be represented as a regex, otherwise, returns null.
- function getRegExpFromNickname(nickname)
- {
- //Found on stackoverflow. Apparently javascript doesn't have a .count() method.
- slashesCount = (nickname.match(/\//g) || []).length;
- if (nickname[0] == "/" && slashesCount >= 2) //We can't just check the front and back of the name, because there might be flags included
- {
- lastSlashIndex = nickname.lastIndexOf("/");
- regexString = nickname.substring(1, lastSlashIndex);
- flags = "";
- if (lastSlashIndex != nickname.length - 1)
- {
- flags = nickname.substring(lastSlashIndex + 1);
- }
- regexPattern = null;
- try
- {
- regexPattern = new RegExp(regexString, flags); //Attempts to build a regex. If it succeeds, the name must be a regex.
- return regexPattern;
- }
- catch (e)
- {
- return null;
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement