feat: profile command

This commit is contained in:
DaInfLoop 2024-06-19 23:07:29 +01:00
parent 8187a01b43
commit 2cc16e68c4

149
index.js
View file

@ -2,7 +2,7 @@ const { App } = require('@slack/bolt');
const postgres = require('postgres');
require('dotenv').config()
const sql = postgres({
const sql = postgres({
host: 'hackclub.app',
port: 5432,
database: 'haroon_slackmaster',
@ -18,13 +18,19 @@ const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET
});
app.use(async (ctx) => {
const a = await sql`SELECT * FROM users WHERE slack_id = ${ctx.body.user_id};`
async function initializeUser(slackUserId) {
let a = await sql`SELECT * FROM users WHERE slack_id = ${slackUserId};`
if (a.length === 0) {
await sql`INSERT INTO users (slack_id) VALUES (${ctx.body.user_id})`
a = await sql`INSERT INTO users (slack_id) VALUES (${slackUserId}) RETURNING *;`
}
return a[0];
}
app.use(async (ctx) => {
await initializeUser(ctx.body.user_id)
await ctx.next()
})
@ -105,6 +111,115 @@ app.command('/chooseopponent', async (ctx) => {
})
});
function generateProfile(dbUser, slackUser) {
return [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*User:* " + slackUser.display_name_normalized
},
{
"type": "mrkdwn",
"text": "*Rank:* " + dbUser.rank
},
{
"type": "mrkdwn",
"text": "*Battle Power*: " + (dbUser.health + dbUser.mindmg + dbUser.maxdmg)
},
{
"type": "mrkdwn",
"text": `*Victories/Losses:* ${dbUser.victories}/${dbUser.losses} (${dbUser.victories / (dbUser.victories + dbUser.losses) || 0}%)`
},
{
"type": "mrkdwn",
"text": "*Current Win Streak:* " + dbUser.curstreak
},
{
"type": "mrkdwn",
"text": "*Highest Win Streak*: " + dbUser.highstreak
}
],
"accessory": {
"type": "image",
"image_url": slackUser.image_1024,
"alt_text": "user profile"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `*Base Health:* ${dbUser.health}\n*Base Min Damage:* ${dbUser.mindmg}\n*Base Max Damage:* ${dbUser.maxdmg}`
}
}
]
}
app.command('/profile', async (ctx) => {
await ctx.ack();
const args = ctx.body.text.slice().split(/ +/g).filter(x => x);
let match;
// If there is an argument and the first one is a Slack ping
if (args.length && (match = args[0].match(/\<\@(.+)\|(.+)>/))) {
const mentionedUser = match[1];
const dbUser = await initializeUser(mentionedUser);
const slackUser = (await ctx.client.users.info({ user: mentionedUser })).user.profile;
ctx.say({
"text": `@${slackUser.display_name_normalized}'s profile`,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `<@${ctx.body.user_id}> ran \`/profile @${slackUser.display_name_normalized}\``
}
},
...generateProfile(dbUser, slackUser)
]
})
}
// If there is an argument but it isn't a Slack ping
else if (args.length) {
ctx.respond({
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Battle Master:* Greetings <@${ctx.body.user_id}>. You have tried to view the profile of an invalid user. Please ensure you either send a user ping as an argument or provide no argument at all.`
}
}
]
})
}
// There is no argument
else {
const dbUser = await initializeUser(ctx.body.user_id);
const slackUser = (await ctx.client.users.info({ user: ctx.body.user_id })).user.profile;
ctx.say({
"text": `@${slackUser.display_name_normalized}'s profile`,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `<@${ctx.body.user_id}> ran \`/profile\``
}
},
...generateProfile(dbUser, slackUser)
]
})
}
})
app.view("chooseopponent", async (ctx) => {
const { selected_option } = Object.values(ctx.view.state.values)[0]['rank-selection'];
@ -152,14 +267,14 @@ app.view("chooseopponent", async (ctx) => {
"element": {
"type": "static_select",
"options": BeginnerOpponents.map(opponent =>
({
"text": {
"type": "plain_text",
"text": `${opponent.name} // ${opponent.stats.health + opponent.stats.min + opponent.stats.max} Battle Power`,
"emoji": true
},
"value": opponent.rawId
})
({
"text": {
"type": "plain_text",
"text": `${opponent.name} // ${opponent.stats.health + opponent.stats.min + opponent.stats.max} Battle Power`,
"emoji": true
},
"value": opponent.rawId
})
),
"action_id": "opponents"
},
@ -177,11 +292,11 @@ app.view("chooseopponent", async (ctx) => {
app.view("chooseopponent-BEGINNER", async (ctx) => {
await ctx.ack();
const userId =
// const userId =
ctx.client.chat.postMessage({
channel: ""
})
// ctx.client.chat.postMessage({
// channel: ""
// })
})
app.command('/bm-eval', async (ctx) => {
@ -207,7 +322,7 @@ app.command('/bm-eval', async (ctx) => {
app.command('/viewopponents', async (ctx) => {
await ctx.ack();
const args = ctx.body.text.split(/ +/g);
const args = ctx.body.text.slice().split(/ +/g);
switch (args[0].toUpperCase()) {
case "SPECIAL":