• Nov 19, 2022 •CodeCatch
0 likes • 2 views
const getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]); // Examples getSubsets([1, 2]); // [[], [1], [2], [1, 2]] getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
• Mar 28, 2023 •Helper
0 likes • 1 view
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 console client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); }); // When a user sends a message, check if they are authenticated client.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 user msg.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 code if (m.content.startsWith('!auth ')) { const code = m.content.slice(6); const { access_token } = await getAccessToken(code); // Set the user's authenticated status and access token msg.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 user function 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 token async 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);
• Jan 26, 2023 •AustinLeath
0 likes • 5 views
function printHeap(heap, index, level) { if (index >= heap.length) { return; } console.log(" ".repeat(level) + heap[index]); printHeap(heap, 2 * index + 1, level + 1); printHeap(heap, 2 * index + 2, level + 1); } //You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0. let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9]; printHeap(heap,0,0)
0 likes • 0 views
arr = [] // empty array const isEmpty = arr => !Array.isArray(arr) || arr.length === 0; // Examples isEmpty([]); // true isEmpty([1, 2, 3]); // false
// promisify(f, true) to get array of results function promisify(f, manyArgs = false) { return function (...args) { return new Promise((resolve, reject) => { function callback(err, ...results) { // our custom callback for f if (err) { reject(err); } else { // resolve with all callback results if manyArgs is specified resolve(manyArgs ? results : results[0]); } } args.push(callback); f.call(this, ...args); }); }; } // usage: f = promisify(f, true); f(...).then(arrayOfResults => ..., err => ...);
• Apr 10, 2025 •hasnaoui1
0 likes • 4 views
const express = require('express') const mongoose = require('mongoose') const cors = require('cors') const dotenv = require('dotenv') const server = express() dotenv.config() server.use(express.json()) server.get('/' , (req , res)=>{ res.send('Hello') }) //4.lancement du serveur server.listen(process.env.PORT , ()=>{ console.log('server run on http://localhost:'+process.env.PORT) })