Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
feat: update github user data
Browse files Browse the repository at this point in the history
  • Loading branch information
wdhdev committed Jul 14, 2023
1 parent f36e561 commit c6d9924
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
40 changes: 34 additions & 6 deletions src/commands/user/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
async execute(interaction, client, Discord) {
try {
if(interaction.options.getSubcommand() === "account") {
const data = await GitHubUser.findOne({ _id: interaction.user.id });
let data = await GitHubUser.findOne({ _id: interaction.user.id });

if(!data) {
const error = new Discord.EmbedBuilder()
Expand All @@ -52,13 +52,41 @@ module.exports = {
return;
}

const octokit = new Octokit({ auth: data.token });
const user = (await octokit.request("GET /user", {})).data;

const oldData = {
id: data.id,
avatar_url: data.avatar_url,
username: data.username,
email: data.email
}

const newData = {
id: user.id,
avatar_url: user.avatar_url,
username: user.login,
email: user.email
}

if(oldData.id !== newData.id || oldData.avatar_url !== newData.avatar_url || oldData.username !== newData.username || oldData.email !== newData.email) {
data = await GitHubUser.findOneAndUpdate({ _id: interaction.user.id }, {
id: user.id,
avatar_url: user.avatar_url,
username: user.login,
email: user.email,
lastUpdated: Date.now()
}, { returnOriginal: false })
}

const account = new Discord.EmbedBuilder()
.setColor(client.config_embeds.github)
.setThumbnail(data.avatar_url)
.setTitle("Your GitHub Account")
.addFields (
{ name: "Username", value: data.username },
{ name: "Email", value: data.email }
{ name: "Email", value: data.email },
{ name: "Linked", value: `<t:${data.linked.toString().slice(0, -3)}> (<t:${data.linked.toString().slice(0, -3)}:R>)` }
)

const actions = new Discord.ActionRowBuilder()
Expand Down Expand Up @@ -90,7 +118,7 @@ module.exports = {
const auth = createOAuthDeviceAuth({
clientType: "oauth-app",
clientId: process.env.github_client_id,
scopes: ["user"],
scopes: ["user, repo"],
async onVerification(verify) {
const login = new Discord.EmbedBuilder()
.setColor(client.config_embeds.github)
Expand Down Expand Up @@ -119,16 +147,16 @@ module.exports = {
completed = true;

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

const user = (await octokit.request("GET /user", {})).data;

new GitHubUser({
_id: interaction.user.id,
linked: Date.now(),
avatar_url: user.avatar_url,
username: user.login,
email: user.email,
token: octoAuth.token
token: octoAuth.token,
linked: Date.now(),
lastUpdated: Date.now()
}).save()

const linked = new Discord.EmbedBuilder()
Expand Down
6 changes: 4 additions & 2 deletions src/models/GitHubUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ const mongoose = require("mongoose");

const schema = new mongoose.Schema({
_id: String,
linked: String,
id: Number,
avatar_url: String,
username: String,
email: String,
token: String
token: String,
linked: String,
lastUpdated: String
})

module.exports = mongoose.model("github-users", schema, "github-users")

0 comments on commit c6d9924

Please sign in to comment.