Skip to content

Commit

Permalink
🆕 route: profile with database values
Browse files Browse the repository at this point in the history
  • Loading branch information
skillzl committed Nov 24, 2023
1 parent eba4c99 commit b1f3f5b
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 192 deletions.
8 changes: 4 additions & 4 deletions backend/checkAuth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = async (req, res, next) => {
if (req.isAuthenticated()) return next();
req.session.backURL = req.url;
res.redirect("/login");
}
if (req.isAuthenticated()) return next();
req.session.backURL = req.url;
res.redirect('/login');
};
83 changes: 0 additions & 83 deletions routes/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,89 +8,6 @@ const router = express.Router();
const db = require('../database/manager');
const checkAuth = require('../backend/checkAuth');

router.get('/server/:guildID/profile', checkAuth, async (req, res) => {
const guild = req.client.guilds.cache.get(req.params.guildID);
if (!guild) {
return res.status(404).send('Guild not found');
}

const userObj = req.client.guilds.cache.get(req.params.guildID).members.cache.get(req.user.id);

const userSubscription = {
undefined: 'None',
0: 'None',
1: 'Nitro Classic',
2: 'Nitro Premium',
};

const status = {
'online': '#43b581',
'idle': '#faa61a',
'dnd': '#f04747',
'offline': '#747f8d',
};

const statusName = {
'online': 'Online',
'idle': 'Idle',
'dnd': 'Do Not Disturb',
'offline': 'Invisible',
};

let statusTypeData;
let statusGameData;
let statusColorData;

if (userObj.presence === null) {
statusTypeData = 'Invisible';
statusGameData = 'Not Playing/Streaming';
statusColorData = '#747f8d';
}
else {
statusTypeData = statusName[userObj.presence.status];
statusGameData = userObj.presence.activities[0] ? userObj.presence.activities[0].name : 'Not Playing/Streaming';
statusColorData = status[userObj.presence.status];
}

const flags = {
'Staff': 'Discord Employee ⚒',
'Partner': 'Partnered Server Owner ♾',
'BugHunterLevel1': 'Bug Hunter (Level 1) 🐞',
'BugHunterLevel2': 'Bug Hunter (Level 2) 🐛',
'Hypesquad': 'HypeSquad Events',
'HypeSquadOnlineHouse1': 'House of Bravery',
'HypeSquadOnlineHouse2': 'House of Brilliance',
'HypeSquadOnlineHouse3': 'House of Balance',
'PremiumEarlySupporter': 'Early Nitro Supporter',
'TeamPseudoUser': 'Team User',
'VerifiedBot': 'Verified Bot',
'VerifiedDeveloper': 'Early Verified Bot Developer',
'CertifiedModerator': 'Discord Certified Moderator',
};

let userFlags;

try {
userFlags = userObj.user.flags.toArray();
}
catch (e) {
userFlags = [];
}

res.render('dashboard/profile', {
tag: (req.user ? req.user.tag : 'Login'),
bot: req.client,
userObj: userObj,
userFlags: userFlags.length ? userFlags.map(flag => flags[flag]).join(', ') : 'None',
status: statusTypeData,
statusColor: statusColorData,
statusGame: statusGameData,
dayjs: dayjs,
userSubscription: userSubscription[req.user.premium_type],
user: req.user || null,
});
});

router.get('/server/:guildID', checkAuth, async (req, res) => {
const server = req.client.guilds.cache.get(req.params.guildID);

Expand Down
30 changes: 30 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const package = require('../package.json');

const router = express.Router();

const db = require('../database/manager');
const checkAuth = require('../backend/checkAuth');

router.get('/', async (req, res) => {
res.render('index', {
tag: (req.user ? req.user.tag : 'Login'),
Expand All @@ -16,6 +19,33 @@ router.get('/', async (req, res) => {
});
});

router.get('/profile/:userID/me', checkAuth, async (req, res) => {
const userReq = req.client.users.cache.get(req.params.userID);
if (!userReq) {
return res.status(404).send('Not allowed');
}

const { user } = await db.getUserById(req.user.id);

const calculateUserXp = (xp) => Math.floor(0.1 * Math.sqrt(xp));
const level = calculateUserXp(user.xp);

const minXp = (level * level) / 0.01;
const maxXp = ((level + 1) * (level + 1)) / 0.01;

res.render('profile/me', {
bot: req.client,
level: level || 0,
xp: user.xp.toLocaleString() || 0,
about: user.about || 0,
balance: user.balance.toLocaleString() || 0,
reputation: user.reputation.toLocaleString() || 0,
user: req.user || null,
minXp: minXp || 0,
maxXp: maxXp || 0,
});
});

router.get('/stats', async (req, res) => {
res.render('stats', {
tag: (req.user ? req.user.tag : 'Login'),
Expand Down
5 changes: 0 additions & 5 deletions views/dashboard/manage.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
<div class="container" style="padding-top: 50px; padding-bottom: 30px;">
<div class="columns is-centered">
<div class="field is-grouped has-image-centered">
<p class="control">
<a href="/dashboard/server/<%= guild.id %>/profile" class="button is-medium is-rounded is-link">
<i class="fa-solid fa-face-grin-beam" style="padding-right: 5px;"></i> Your Profile
</a>
</p>
<p class="control">
<a href="/dashboard/server/<%= guild.id %>/members" class="button is-medium is-rounded is-success">
<i class="fas fa-users" style="padding-right: 5px;"></i> Members
Expand Down
98 changes: 0 additions & 98 deletions views/dashboard/profile.ejs

This file was deleted.

4 changes: 2 additions & 2 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
</a>
</p>
<p class="control">
<a href="<%= process.env.SUPPORT_SERVER %>" class="button is-link is-rounded is-medium">
<i class="fab fa-discord" style="padding-right: 5px;"></i> Support
<a href="/profile/<%= user.id%>/me" class="button is-warning is-rounded is-medium">
<i class="fa fa-user" style="padding-right: 5px;"></i> Profile
</a>
</p>
<p class="control">
Expand Down
3 changes: 3 additions & 0 deletions views/partials/header.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<a href="/dashboard/servers" class="dropdown-item">
<i class="fas fa-server" style="padding-right: 5px;"></i> Servers
</a>
<a href="/profile/<%= user.id%>/me" class="dropdown-item">
<i class="fas fa-user" style="padding-right: 5px;"></i> Profile
</a>
<hr class="dropdown-divider">
<a href="/logout" class="dropdown-item">
<i class="fas fa-sign-out-alt" style="padding-right: 5px;"></i> Log Out
Expand Down
57 changes: 57 additions & 0 deletions views/profile/me.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<%- include("../partials/header") %>

<title><%= bot.user.username.toLowerCase() %>.fun - your profile</title>

<section class="hero is-small has-text-centered is-link">
<div class="hero-body"
style="padding-top: 50px; padding-bottom: 50px; background-repeat: no-repeat; background-position: center; background-size: cover; background-image: url('https://i.imgur.com/DwUXLF3.png')">
<figure class="image is-128x128 has-image-centered">
<img class="is-rounded" style="box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.8);"
src="https://cdn.discordapp.com/avatars/<%= user.id %>/<%= user.avatar %>">
</figure>
<br>
<h1 class="is-size-1" style="text-shadow: 0 0 10px #000000;">
<%= user.username %><%= user.discriminator && user.discriminator !== '0' ? `#${user.discriminator}` : '' %>
</h1>
<h2>
<%= about %>
</h2>
</div>
</section>

<div class="container" style="padding-top: 25px; padding-bottom: 10px;">
<h1 class="has-text-white is-size-5">LEVEL : <%= level %></h1>
<progress class="progress is-success" value="<%=minXp %>" max="<%=maxXp %>" style="width:20%"> <%= xp %></progress>
</div>
<div class="container has-text-centered" style="padding-top: 30px; padding-bottom: 50px;">
<table class="table is-fullwidth">
<tbody>
<tr class="has-text-centered">
<td style="font-size: 20px;"><i class="fa-solid fa-wand-magic-sparkles"></i> XP</td>
<td style="font-size: 20px;">
<%= xp %>
</td>
</tr>
<tr class="has-text-centered">
<td style="font-size: 20px;"><i class="fa-solid fa-trophy"></i> LEVEL</td>
<td style="font-size: 20px;">
<%= level %>
</td>
</tr>
<tr class="has-text-centered">
<td style="font-size: 20px;"><i class="fa-solid fa-coins"></i> BALANCE</td>
<td style="font-size: 20px;">
<%= balance %>
</td>
</tr>
<tr class="has-text-centered">
<td style="font-size: 20px;"><i class="fa-solid fa-medal"></i> REPUTATION</td>
<td style="font-size: 20px;">
<%= reputation %>
</td>
</tr>
</tbody>
</table>
</div>

<%- include("../partials/footer") %>

0 comments on commit b1f3f5b

Please sign in to comment.