Skip to main content
Loading...

More JavaScript Posts

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);
results =
{
 "11345240":"Media Comment-80e47004-1719-493a-af69-a43b2652b41c",
 "11345282":"zoom_3_22_21.mp4",
 "11345303":"Recording-2.m4a",
 "11345348":"February 12%2C 2021.mp4",
 "11345826":"Lecture_Rousso_Chapter_06-2.pptx",
 "11345933":"Themes_InClassAssigment.pptx",
 "11346193":"Agenda – Week of March23.pptx",
 "11346318":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Bb - Score.pdf",
 "11346319":"MUJS 2370 SP2021 Updated Rhythm Changes Drills C - Score.pdf",
 "11346322":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Eb - Score.pdf",
 "11346323":"MUJS 2370 Updated Rhythm Changes Drills Bass - Score.pdf",
 "11346381":"Anthropology with colored highlighter analysis - Mar 22 2021 - 10-36 AM.pdf",
 "11346449":"Anthropology with colored highlighter analysis - Mar 22 2021 - 10-36 AM.pdf",
 "11346450":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Bb - Score.pdf",
 "11346451":"MUJS 2370 SP2021 Updated Rhythm Changes Drills C - Score.pdf",
 "11346453":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Eb - Score.pdf",
 "11346454":"MUJS 2370 Updated Rhythm Changes Drills Bass - Score.pdf",
 "11346495":"Steinel chromatic ornamentation étude rhythm changes - Aug 23 2020 - 12-19 PM.pdf",
 "11346532":"Media Comment-136242b4-7f2c-4d5d-8aa4-091f07e797cf",
 "11346581":"2019 Trust Based Relationship Intervention Manual.pdf",
 "11346584":"TBRI ppt.pptx",
 "11346593":"Trauma informed care.ppt"
}

function NewTab(testing) {
  window.open(testing, "_blank");
}

for(test in results) {
  var testing = 'https://unt.instructure.com/files/' + test + '/download';
  NewTab(testing);
  //window.open = 'https://unt.instructure.com/files/' + test + '/download';
}