Loading...
More JavaScript Posts
// Orconst randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`;console.log(randomColor);
// `date` is a `Date` objectconst formatYmd = date => date.toISOString().slice(0, 10);// ExampleformatYmd(new Date()); // 2020-05-06
const luhnCheck = num => {let arr = (num + '').split('').reverse().map(x => parseInt(x));let lastDigit = arr.splice(0, 1)[0];let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val *= 2) > 9 ? val - 9 : val)),0);sum += lastDigit;return sum % 10 === 0;};luhnCheck('4485275742308327'); // trueluhnCheck(6011329933655299); // trueluhnCheck(123456789); // false
var invertTree = function(root) {const reverseNode = node => {if (node == null) {return null}reverseNode(node.left);reverseNode(node.right);let holdLeft = node.left;node.left = node.right;node.right = holdLeft;return node;}return reverseNode(root);};
const Discord = require('discord.js');const client = new Discord.Client();const token = 'YOUR_BOT_TOKEN';// When the bot is ready, log a message to the consoleclient.on('ready', () => {console.log(`Logged in as ${client.user.tag}!`);});// When a user sends a message, check if they are authenticatedclient.on('message', async (msg) => {if (!msg.author.bot && !msg.author.authenticated) {const filter = (m) => m.author.id === msg.author.id;const collector = msg.channel.createMessageCollector(filter, { time: 15000 });// Send an authentication message to the usermsg.author.send('Please authenticate yourself by clicking this link: ' + generateAuthURL(msg.author.id));collector.on('collect', async (m) => {// Check if the message contains the authentication codeif (m.content.startsWith('!auth ')) {const code = m.content.slice(6);const { access_token } = await getAccessToken(code);// Set the user's authenticated status and access tokenmsg.author.authenticated = true;msg.author.access_token = access_token;collector.stop();}});collector.on('end', () => {if (!msg.author.authenticated) {msg.author.send('Authentication timed out.');}});}});// Generate an authentication URL for the userfunction generateAuthURL(userId) {const redirectURI = encodeURIComponent('YOUR_REDIRECT_URL');const scopes = 'identify';const clientID = 'YOUR_CLIENT_ID';return `https://discord.com/api/oauth2/authorize?client_id=${clientID}&redirect_uri=${redirectURI}&response_type=code&scope=${scopes}&state=${userId}`;}// Exchange the authorization code for an access tokenasync function getAccessToken(code) {const redirectURI = encodeURIComponent('YOUR_REDIRECT_URL');const clientID = 'YOUR_CLIENT_ID';const clientSecret = 'YOUR_CLIENT_SECRET';const res = await fetch('https://discord.com/api/oauth2/token', {method: 'POST',headers: {'Content-Type': 'application/x-www-form-urlencoded',},body: `grant_type=authorization_code&code=${code}&redirect_uri=${redirectURI}&client_id=${clientID}&client_secret=${clientSecret}`,});return await res.json();}client.login(token);
const nums = [1, 2, 3];const strs = Array.from('est');nums.push(6);nums.push(4, 9);strs.unshift('t');nums.length; // 6nums[nums.length - 1]; // 9strs[0]; // 't'strs[2]; // 's'nums.slice(1, 3); // [2, 3]nums.map(n => n * 2); // [2, 4, 6, 12, 8, 18]nums.filter(n => n % 2 === 0); // [2, 6, 4]nums.reduce((a, n) => a + n, 0); // 25strs.reverse(); // ['t', 's', 'e', 't']strs.join(''); // 'test'const nums = new Set([1, 2, 3]);nums.add(4);nums.add(1);nums.add(5);nums.add(4);nums.size; // 5nums.has(4); // truenums.delete(4);nums.has(4); // false[...nums]; // [1, 2, 3, 5]nums.clear();nums.size; // 0const items = new Map([[1, { name: 'John' }],[2, { name: 'Mary' }]]);items.set(4, { name: 'Alan' });items.set(2, { name: 'Jeff' });items.size; // 3items.has(4); // trueitems.get(2); // { name: 'Jeff' }items.delete(2);items.size; // 2[...items.keys()]; // [1, 4][...items.values()]; // [{ name: 'John' }, { name: 'Alan' }]items.clear();items.size; // 0