1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-17 16:30:58 -05:00

Setup Octokit authentication and changes to updater.

This commit is contained in:
AnxietyisReal 2023-04-17 18:17:27 +10:00
parent 0c266dd461
commit 0158bec8f8
3 changed files with 14 additions and 7 deletions

View File

@ -32,6 +32,7 @@
"mongoose": "7.0.3", "mongoose": "7.0.3",
"systeminformation": "5.17.12", "systeminformation": "5.17.12",
"@octokit/rest": "19.0.7", "@octokit/rest": "19.0.7",
"@octokit/auth-token": "3.0.3",
"typescript": "5.0.4", "typescript": "5.0.4",
"xml-js": "1.6.11" "xml-js": "1.6.11"
}, },

View File

@ -1,5 +1,6 @@
import Discord,{SlashCommandBuilder} from 'discord.js'; import Discord,{SlashCommandBuilder} from 'discord.js';
import {Octokit} from '@octokit/rest'; import {Octokit} from '@octokit/rest';
import {createTokenAuth} from '@octokit/auth-token';
import {exec} from 'node:child_process'; import {exec} from 'node:child_process';
import fs from 'node:fs'; import fs from 'node:fs';
import util from 'node:util'; import util from 'node:util';
@ -48,7 +49,7 @@ export default {
if (typeof output == 'object') output = 'js\n'+util.formatWithOptions({depth: 1}, '%O', output) if (typeof output == 'object') output = 'js\n'+util.formatWithOptions({depth: 1}, '%O', output)
else output = '\n' + String(output); else output = '\n' + String(output);
[client.tokens.main,client.tokens.beta,client.tokens.toast,client.tokens.tae,client.tokens.webhook_url,client.tokens.webhook_url_test,client.tokens.mongodb_uri,client.tokens.mongodb_uri_dev].forEach((x)=>{ [client.tokens.main,client.tokens.beta,client.tokens.toast,client.tokens.tae,client.tokens.octokit,client.tokens.webhook_url,client.tokens.webhook_url_test,client.tokens.mongodb_uri,client.tokens.mongodb_uri_dev].forEach((x)=>{
const regexp = new RegExp(x as string,'g'); const regexp = new RegExp(x as string,'g');
output = output.replace(regexp, ':noblank: No token?'); output = output.replace(regexp, ':noblank: No token?');
}) })
@ -59,17 +60,21 @@ export default {
interaction.reply({embeds: [embed]}).catch(()=>(interaction.channel as Discord.TextChannel).send({embeds: [embed]})); interaction.reply({embeds: [embed]}).catch(()=>(interaction.channel as Discord.TextChannel).send({embeds: [embed]}));
}, },
update: async()=>{ update: async()=>{
var githubRepo = {owner: 'AnxietyisReal', repo: 'Daggerbot-TS', ref: 'HEAD'} const SummonAuthentication = createTokenAuth(client.tokens.octokit);
const octokit = new Octokit({timeZone: 'Australia/NSW', userAgent: 'Daggerbot'}) const {token} = await SummonAuthentication();
const fetchCommitMsg = await octokit.repos.getCommit(githubRepo).then(x=>x.data.commit.message).catch(err=>{console.log(err); interaction.reply({content: 'Placeholder error for `fetchCommitMsg`', ephemeral: true})}); var githubRepo = {owner: 'AnxietyisReal', repo: 'Daggerbot-TS', ref: 'HEAD'};
const fetchCommitAuthor = await octokit.repos.getCommit(githubRepo).then(x=>x.data.commit.author.name).catch(err=>{console.log(err); interaction.reply({content: 'Placeholder error for `fetchCommitAuthor`', ephemeral: true})});
const clarkson = await interaction.reply({content: 'Pulling from repository...', fetchReply: true}); const clarkson = await interaction.reply({content: 'Pulling from repository...', fetchReply: true});
const octokit = new Octokit({auth: token, timeZone: 'Australia/NSW', userAgent: 'Daggerbot-TS'});
const fetchCommitMsg = await octokit.repos.getCommit(githubRepo).then(x=>x.data.commit.message).catch((err:Error)=>err.message);
const fetchCommitAuthor = await octokit.repos.getCommit(githubRepo).then(x=>x.data.commit.author.name).catch((err:Error)=>err.message);
const fetchTotalChanges = await octokit.repos.getCommit(githubRepo).then(x=>x.data.stats.total.toLocaleString('en-US')).catch((err:Error)=>err.message);
const getCurrentCommitURL = await octokit.repos.getCommit(githubRepo).then(x=>x.data.html_url).catch((err:Error)=>err.message);
exec('git pull',(err:Error,stdout)=>{ exec('git pull',(err:Error,stdout)=>{
if (err) clarkson.edit(`\`\`\`${removeUsername(err.message)}\`\`\``) if (err) clarkson.edit(`\`\`\`${removeUsername(err.message)}\`\`\``)
else if (stdout.includes('Already up to date')) clarkson.edit('Bot is already up to date with the repository, did you forgor to push the changes? :skull:') else if (stdout.includes('Already up to date')) clarkson.edit('I am already up to date with the upstream repository.')
else clarkson.edit('Compiling TypeScript files...').then(()=>exec('tsc', (err:Error)=>{ else clarkson.edit('Compiling TypeScript files...').then(()=>exec('tsc', (err:Error)=>{
if (err) clarkson.edit(`\`\`\`${removeUsername(err.message)}\`\`\``) if (err) clarkson.edit(`\`\`\`${removeUsername(err.message)}\`\`\``)
else clarkson.edit(`Commit: **${fetchCommitMsg}**\nCommit author: **${fetchCommitAuthor}**\n\nSuccessfully compiled TypeScript files into JavaScript!\nUptime before restarting: **${client.formatTime(client.uptime as number, 3, {commas: true, longNames: true})}**`).then(()=>exec('pm2 restart Daggerbot')) else clarkson.edit(`[Commit:](<${getCurrentCommitURL}>) **${fetchCommitMsg}**\nCommit author: **${fetchCommitAuthor}**\nTotal commit changes: **${fetchTotalChanges}**\n\nSuccessfully compiled TypeScript files into JavaScript!\nUptime before restarting: **${client.formatTime(client.uptime as number, 3, {commas: true, longNames: true})}**`).then(()=>exec('pm2 restart Toast'))
})) }))
}); });
}, },

View File

@ -126,6 +126,7 @@ export interface Tokens {
beta: string beta: string
toast: string toast: string
tae: string tae: string
octokit: string
webhook_url: string webhook_url: string
webhook_url_test: string webhook_url_test: string
mongodb_uri: string mongodb_uri: string