2024-06-19 19:50:14 +00:00
const { App } = require ( '@slack/bolt' ) ;
2024-06-19 20:20:41 +00:00
const postgres = require ( 'postgres' ) ;
2024-06-19 19:50:14 +00:00
require ( 'dotenv' ) . config ( )
2024-06-19 20:20:41 +00:00
const sql = postgres ( {
host : 'hackclub.app' ,
port : 5432 ,
database : 'haroon_slackmaster' ,
username : 'haroon' ,
password : process . env . PGSQL _PASSWORD ,
ssl : 'require'
} )
2024-06-19 19:50:14 +00:00
const BeginnerOpponents = require ( './opponents/beginner.json' ) ;
const app = new App ( {
token : process . env . SLACK _BOT _TOKEN ,
signingSecret : process . env . SLACK _SIGNING _SECRET
} ) ;
2024-06-19 20:20:41 +00:00
app . use ( async ( ctx ) => {
const a = await sql ` SELECT * FROM users WHERE slack_id = ${ ctx . body . user _id } ; `
if ( a . length === 0 ) {
await sql ` INSERT INTO users (slack_id) VALUES ( ${ ctx . body . user _id } ) `
}
await ctx . next ( )
} )
2024-06-19 19:50:14 +00:00
app . command ( '/chooseopponent' , async ( ctx ) => {
await ctx . ack ( ) ;
await ctx . client . views . open ( {
trigger _id : ctx . body . trigger _id ,
view : {
"type" : "modal" ,
"callback_id" : "chooseopponent" ,
"title" : {
"type" : "plain_text" ,
"text" : "Choose an opponent" ,
"emoji" : true
} ,
"submit" : {
"type" : "plain_text" ,
"text" : "Choose" ,
"emoji" : true
} ,
"close" : {
"type" : "plain_text" ,
"text" : "Never mind" ,
"emoji" : true
} ,
"blocks" : [
{
"type" : "section" ,
"text" : {
"type" : "mrkdwn" ,
"text" : ` *Battle Support*: Hiya <@ ${ ctx . body . user _id } >! What rank opponent would you like to battle against? \n \n Not sure yet? Don't worry! Just cancel out and view what opponents you can fight with /viewopponents! `
}
} ,
{
"type" : "divider"
} ,
{
"type" : "input" ,
"element" : {
"type" : "radio_buttons" ,
"options" : [
{
"text" : {
"type" : "plain_text" ,
"text" : "Special" ,
"emoji" : true
} ,
"value" : "SPECIAL"
} ,
{
"text" : {
"type" : "plain_text" ,
"text" : "Beginner" ,
"emoji" : true
} ,
"value" : "BEGINNER"
} ,
{
"text" : {
"type" : "plain_text" ,
"text" : "Casual" ,
"emoji" : true
} ,
"value" : "CASUAL"
}
] ,
"action_id" : "rank-selection"
} ,
"label" : {
"type" : "plain_text" ,
"text" : "Choose a rank:" ,
"emoji" : true
}
}
]
}
} )
} ) ;
app . view ( "chooseopponent" , async ( ctx ) => {
const { selected _option } = Object . values ( ctx . view . state . values ) [ 0 ] [ 'rank-selection' ] ;
const rank = selected _option . value ;
2024-06-19 20:27:12 +00:00
const messages = {
"SPECIAL" : ` *Battle Special*: Wasn't expecting to see ya here <@ ${ ctx . body . user . id } >... I guess you're up for a challenge huh? Alright then, who do ya wanna annoy today? ` ,
"BEGINNER" : ` *Battle Beginner*: Hey there <@ ${ ctx . body . user . id } >! Let's keep things simple, who do you want to battle against? ` ,
"CASUAL" : ` *Battle Casual*: Alright <@ ${ ctx . body . user . id } >, things are about to get a little bit tougher from here... Who do you feel like taking on today? `
}
2024-06-19 19:50:14 +00:00
await ctx . ack ( {
response _action : 'update' ,
view : {
"type" : "modal" ,
"callback_id" : "chooseopponent-" + rank ,
"title" : {
"type" : "plain_text" ,
"text" : "Choose an opponent" ,
"emoji" : true
} ,
"submit" : {
"type" : "plain_text" ,
"text" : "Battle" ,
"emoji" : true
} ,
"close" : {
"type" : "plain_text" ,
"text" : "Never mind" ,
"emoji" : true
} ,
"blocks" : [
{
"type" : "section" ,
"text" : {
"type" : "mrkdwn" ,
2024-06-19 20:27:12 +00:00
"text" : messages [ rank ] || ` *Battle Master*: Greetings <@ ${ ctx . body . user . id } >. Please choose an opponent from below to battle. `
2024-06-19 19:50:14 +00:00
}
} ,
{
"type" : "divider"
} ,
{
"type" : "input" ,
"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
} )
) ,
"action_id" : "opponents"
} ,
"label" : {
"type" : "plain_text" ,
"text" : "Choose an opponent:" ,
"emoji" : true
}
}
]
}
} )
} )
2024-06-19 20:20:41 +00:00
app . view ( "chooseopponent-BEGINNER" , async ( ctx ) => {
await ctx . ack ( ) ;
const userId =
ctx . client . chat . postMessage ( {
channel : ""
} )
} )
2024-06-19 20:49:07 +00:00
app . command ( '/bm-eval' , async ( ctx ) => {
await ctx . ack ( ) ;
const resp = require ( 'util' ) . inspect ( await eval ( ctx . body . text ) , undefined , 1 )
ctx . respond ( {
text : resp ,
response _type : 'ephemeral' ,
blocks : [
{
type : 'section' ,
text : {
type : 'mrkdwn' ,
text : "```" + resp + "```"
}
}
]
} )
} )
2024-06-19 19:50:14 +00:00
app . command ( '/viewopponents' , async ( ctx ) => {
await ctx . ack ( ) ;
const args = ctx . body . text . split ( / +/g ) ;
switch ( args [ 0 ] . toUpperCase ( ) ) {
case "SPECIAL" :
ctx . respond ( {
text : "You're trying to view Special opponents." ,
response _type : 'ephemeral'
} )
break ;
case "BEGINNER" :
const mappedBeginner = BeginnerOpponents . map ( opponent =>
( {
name : opponent . name ,
battlePower :
opponent . stats . health +
opponent . stats . min +
opponent . stats . max
} )
)
ctx . respond ( {
response _type : 'ephemeral' ,
text : ` *Battle Master:* Greetings battler. Here are the avaliable *Beginner* opponents for you to battle. ` ,
blocks : [
{
type : 'section' ,
text : {
type : 'mrkdwn' ,
text : ` *Battle Master:* Greetings <@ ${ ctx . body . user _id } >. Here are the avaliable *Beginner* opponents for you to battle. `
}
} ,
{
type : 'section' ,
text : {
type : 'mrkdwn' ,
text : mappedBeginner . map ( opponent => ` * ${ opponent . name } :* \n \n ${ opponent . battlePower } Battle Power ` ) . join ( '\n\n\n' )
}
}
]
} )
break ;
case "CASUAL" :
ctx . say ( "You're trying to view Casual opponents." )
break ;
default :
ctx . say ( "You either wrote nothing or just chose a rank that doesn't exist." )
}
} ) ;
; ( async ( ) => {
// Start your app
await app . start ( process . env . PORT ) ;
console . log ( '⚡️ Bolt app is running!' ) ;
} ) ( ) ;