Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
lxRbckl committed Feb 2, 2024
1 parent 9171551 commit e2ae38d
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 120 deletions.
127 changes: 7 additions & 120 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,144 +2,31 @@


/// import <
const cron = require('node-cron');
const {githubUpdate} = require('lxrbckl');
const {Octokit} = require('@Octokit/rest');
const {

Client,
IntentsBitField

} = require('discord.js');
const Client = require('./source/client.js');
const Database = require('./source/database.js');

// >


// setup <
// initialize <
const owner = process.env.owner;
const users = process.env.users;
const branch = process.env.branch;
const filepath = process.env.filepath;
const channelId = process.env.channelId;
const repository = process.env.repository;
const token = {

octokit : process.env.tokenOctokit,
discord : process.env.tokenDiscord

};

const octokit = new Octokit({auth : token.octokit});
const client = new Client({

rest : {version : '10'},
intents : [

IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent

]

});

// >


async function fetch() {

var data = {};
await Promise.all(users.map(async u => {

const repos = await octokit.paginate(`GET /users/${u}/repos`);
for (const r of repos) {

let result = (await octokit.repos.get({

owner : u,
repo : r.name

})).data;

data[result.name] = {

'url' : result.url,
'topics' : result.topics,
'language' : result.language,
'description' : result.description

};

}

}));

return data;

}


async function update(data) {

return await githubUpdate({

pData : data,
pOwner : owner,
pPath : filepath,
pGithub : octokit,
opBranch : branch,
opShowError : false,
pRepository : repository

});

}


function message(result) {

client.channels.cache.get(channelId).send({

content : {

// (successful) <
// (failure) <
true : '`Update was successful.`',
false : '`Failed to update.`'

// >

}[result == undefined]

});

}


function schedule() {

client.on('ready', async () => {

cron.schedule('0 0 * * *', async () => {

let data = await fetch();
let result = await update(data);

message(result);

});

});

}
(async () => {

let client = new Client({

(async () => {
pToken : token.discord,
pDatabase : new Database(token.octokit)

client.login(token.discord);
schedule();
}).run();

})();

Expand Down
114 changes: 114 additions & 0 deletions source/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// import <
const cron = require('node-cron');
const {

Client,
IntentsBitField

} = require('discord.js');

// >


class client {

constructor({

pToken,
pDatabase

}) {

// setup <
// initialize <
this.token = pToken;
this.database = pDatabase;
this.users = process.env.users;
this.channelId = process.env.channelId;

this.client = new Client({

rest : {version : '10'},
intents : [

IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent

]

});

// >

}


async fetch() {

var data = {};
await Promise.all(this.users.map(async u => {

let result = this.database.getRepositories(u);
data[u] = result;

}));

return data;

}


message(result) {

client.channels.cache.get(this.channelId).send({

content : {

// event (failure) <
// event (success) <
false : '`Failed to update.`',
true : '`Update was successful.`'

// >

}[result]

});

}


schedule() {

this.client.on('ready', async () => {

cron.schedule('0 0 * * *', async () => {

let data = await fetch();
let result = await this.database.updateFile(data);

this.message(result);

});

});

}


async run() {

this.client.run(this.token);
this.schedule();

}

}


// export <
module.exports = client;

// >
81 changes: 81 additions & 0 deletions source/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// import <
const {githubUpdate} = require('lxrbckl');
const {Octokit} = require('@Octokit/rest');

// >


class database {

constructor(pToken) {

// setup <
// initialize <
this.token = pToken;
this.owner = process.env.owner;
this.branch = process.env.branch;
this.filepath = process.env.filepath;
this.repository = process.env.repository;

this.octokit = new Octokit({auth : this.token});

// >

}


async getRepositories(user) {

var data = {};
let query = `GET /users/${user}/repos`;
var repos = await this.octokit.paginate(query);
for (const r of repos) {

let result = (await octokit.repos.get({

owner : user,
repo : r.name

})).data;

data[result.name] = {

'url' : result.url,
'topics' : result.topics,
'language' : result.language,
'description' : result.description

};

}

return data;

}


async updateFile(data) {

let result = await githubUpdate({

pData : data,
pOwner : this.owner,
opShowError : false,
pPath : this.filepath,
pGithub : this.octokit,
opBranch : this.branch,
pRepository : this.repository

});

return (result == undefined);

}

}


// export <
module.exports = database;

// >

0 comments on commit e2ae38d

Please sign in to comment.