• Mar 28, 2023 •Helper
0 likes • 2 views
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);
• Nov 19, 2022 •CodeCatch
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'); // true luhnCheck(6011329933655299); // true luhnCheck(123456789); // false
• Mar 11, 2021 •C S
0 likes • 1 view
const mongoose = require('mongoose') const UserSchema = new mongoose.Schema({ username: { type: String, required: true }, email: { type: String, unique: true, required: true }, password: { type: String, required: true }, date: { type: Date, default: Date.now } }) module.exports = User = mongoose.model('user', UserSchema)
0 likes • 3 views
const reverse = str => str.split('').reverse().join(''); reverse('hello world'); // Result: 'dlrow olleh'
0 likes • 4 views
// Time Complexity : O(N) // Space Complexity : O(1) var isMonotonic = function(nums) { let isMono = null; for(let i = 1; i < nums.length; i++) { if(isMono === null) { if(nums[i - 1] < nums[i]) isMono = 0; else if(nums[i - 1] > nums[i]) isMono = 1; continue; } if(nums[i - 1] < nums[i] && isMono !== 0) { return false; } else if(nums[i - 1] > nums[i] && isMono !== 1) { return false; } } return true; }; let nums1 = [1,2,2,3] let nums2 = [6,5,4,4] let nums3 = [1,3,2] console.log(isMonotonic(nums1)); console.log(isMonotonic(nums2)); console.log(isMonotonic(nums3));
• Oct 15, 2022 •CodeCatch
3 likes • 4 views
const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected successfully to server"); const db = client.db(dbName); client.close(); });