diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..de1b08a5187a1a83e93616a097d758cd06347310 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +token +node_modules diff --git a/index.js b/index.js new file mode 100644 index 0000000000000000000000000000000000000000..dace01c16133c8d87370326dc66a48ba19d696ac --- /dev/null +++ b/index.js @@ -0,0 +1,97 @@ +const Discord = require('discord.js'); +const fs = require('fs'); +const client = new Discord.Client(); +const Color = require('color'); + +client.on('ready', () => { + console.log(`Logged in as ${client.user.tag}!`); +}); + +client.on('message', async (msg) => { + if (msg.author.bot) return; + + if (msg.content.startsWith('!paint') || msg.content.startsWith('!color')) { + const args = msg.content.split(' '); + args.shift(); + + if (args.length === 0) { + msg.channel.send('> **Usage**: !paint \n> Where is any valid HEX or CSS color value.') + return; + } + + const input = args.join(' '); + let color; + try { + color = Color(input); + } catch (error) { + try { + color = Color('#' + input); + } catch (error) { + msg.channel.send(`> Could not get a color from ${input.replace(/\n/g, '').replace(/`/g, '\\\\`')}`) + return; + } + } + + const hex = color.hex(); + + let role = msg.guild.roles.find(role => role.name === hex); + + if (!role) { + if (msg.guild.roles.length === 250) { + msg.channel.send(`> **Error**: The Discord Role Limit of **250 Roles** has been hit!`); + return; + } else { + try { + role = await msg.guild.createRole({ + name: hex, + color: color.rgbNumber(), + permissions: 0, + }); + } catch (error) { + msg.channel.send(`> **Error**: Could not create a role for you!`); + return; + } + } + } + + try { + const rolesToRemove = msg.member.roles.filter(role => role.name.startsWith('#') && role.name !== hex); + rolesToRemove.map(async (role) => { + msg.member.removeRole(role); + if (role.members.size === 0) { + console.log('Deleting ' + role.name) + role.delete(); + } + }); + await msg.member.addRole(role); + msg.channel.send(`> You\'ve been painted to **${hex}**`); + } catch (error) { + console.log(error); + msg.channel.send(`> **Error**: Could not assign your role, ask the server admin to check my permissions.`); + } + } +}); + +client.on('guildCreate', guild => { + let defaultChannel = ""; + guild.channels.forEach((channel) => { + if (channel.type == "text" && defaultChannel == "") { + if (channel.permissionsFor(guild.me).has("SEND_MESSAGES")) { + defaultChannel = channel; + } + } + }) + //defaultChannel will be the channel object that it first finds the bot has permissions for + defaultChannel.send(`**I'm the Name Painter. I let users customize their name color.** +Some things to note + +- The paint command is available to all users in the server. +- You should not have existing roles that start with # +- You generally should not assign the roles manually + +To use me, simply run +> !paint +> Where is any valid HEX or CSS color value.`); +}); + +client.login(fs.readFileSync('token').toString().replace(/ |\n/g,'')); diff --git a/logo.png b/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..bad5fd014bf71bd435b33ecfa5b826eafa0459fd Binary files /dev/null and b/logo.png differ diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000000000000000000000000000000000000..bfa281c813362569e4c8d44c87dd2ebdd0d2eea2 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,97 @@ +{ + "name": "name-paint", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "async-limiter": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", + "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" + }, + "color": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/color/-/color-3.1.2.tgz", + "integrity": "sha512-vXTJhHebByxZn3lDvDJYw4lR5+uB3vuoHsuYA5AKuxRVn5wzzIfQKGLBmgdVRHKTJYeK5rvJcHnrd0Li49CFpg==", + "requires": { + "color-convert": "^1.9.1", + "color-string": "^1.5.2" + } + }, + "color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "requires": { + "color-name": "1.1.3" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" + }, + "color-string": { + "version": "1.5.3", + "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.5.3.tgz", + "integrity": "sha512-dC2C5qeWoYkxki5UAXapdjqO672AM4vZuPGRQfO8b5HKuKGBbKWpITyDYN7TOFKvRW7kOgAn3746clDBMDJyQw==", + "requires": { + "color-name": "^1.0.0", + "simple-swizzle": "^0.2.2" + } + }, + "discord.js": { + "version": "11.5.1", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.5.1.tgz", + "integrity": "sha512-tGhV5xaZXE3Z+4uXJb3hYM6gQ1NmnSxp9PClcsSAYFVRzH6AJH74040mO3afPDMWEAlj8XsoPXXTJHTxesqcGw==", + "requires": { + "long": "^4.0.0", + "prism-media": "^0.0.3", + "snekfetch": "^3.6.4", + "tweetnacl": "^1.0.0", + "ws": "^6.0.0" + } + }, + "is-arrayish": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz", + "integrity": "sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==" + }, + "long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", + "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" + }, + "prism-media": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-0.0.3.tgz", + "integrity": "sha512-c9KkNifSMU/iXT8FFTaBwBMr+rdVcN+H/uNv1o+CuFeTThNZNTOrQ+RgXA1yL/DeLk098duAeRPP3QNPNbhxYQ==" + }, + "simple-swizzle": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", + "integrity": "sha1-pNprY1/8zMoz9w0Xy5JZLeleVXo=", + "requires": { + "is-arrayish": "^0.3.1" + } + }, + "snekfetch": { + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/snekfetch/-/snekfetch-3.6.4.tgz", + "integrity": "sha512-NjxjITIj04Ffqid5lqr7XdgwM7X61c/Dns073Ly170bPQHLm6jkmelye/eglS++1nfTWktpP6Y2bFXjdPlQqdw==" + }, + "tweetnacl": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.1.tgz", + "integrity": "sha512-kcoMoKTPYnoeS50tzoqjPY3Uv9axeuuFAZY9M/9zFnhoVvRfxz9K29IMPD7jGmt2c8SW7i3gT9WqDl2+nV7p4A==" + }, + "ws": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz", + "integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==", + "requires": { + "async-limiter": "~1.0.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000000000000000000000000000000000000..a81ca003d9b152a0780f42c3ef877bddfb935165 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "name-paint", + "version": "1.0.0", + "description": "Discord Bot to let people paint their name anything.", + "main": "index.js", + "scripts": { + "start": "node index.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "color": "^3.1.2", + "discord.js": "^11.5.1" + } +}