Skip to content

Commit

Permalink
feat/express (#442)
Browse files Browse the repository at this point in the history
* remove: package lock

* update: version, feat: express

* setup: express

* fix: roles remove

* fix: reference null on entitlement

* upgrade: yarn packages

* remove: console log

---------

Co-authored-by: JustDams <noreply@github.com>
  • Loading branch information
JustDams and web-flow authored Oct 23, 2024
1 parent bab9921 commit 0b57040
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 2,683 deletions.
18 changes: 18 additions & 0 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { Client, GatewayIntentBits } = require('discord.js')
const fs = require('fs')
const express = require('express')
const AntiSpam = require('./templates/antispam')
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages] })
const { updateRoles } = require('./functions/roles')

client.antispam = new AntiSpam()

Expand All @@ -12,3 +14,19 @@ fs.readdirSync('./events').filter(file => file.endsWith('.js')).forEach(async (f

// Start the bot
client.login(process.env.TOKEN)

// start the API
const app = express()
const PORT = process.env.EXPRESS_PORT || 3001

app.put('/users/:id/roles', async (req, res) => {
const { id } = req.params
const { remove } = req.query
await updateRoles(client, id, null, remove === 'true')

res.status(200).send()
})

app.listen(PORT, () => {
console.log(`🐉 API server is running on port ${PORT}`)
})
2 changes: 2 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ services:
- ./node_modules:/usr/src/app/node_modules
environment:
- NODE_ENV=dev
ports:
- '3001:3001'
env_file:
- .env
networks:
Expand Down
4 changes: 2 additions & 2 deletions functions/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ const setupRoles = async (client, user, guildId, remove) => {
.then(e => logRoleUpdate(client, member, role, guildDatas, playerElo, REMOVE))
.catch((err) => handleRoleErrors(err, role))

// Add role if it fits the criteria and the role isn't already assigned
if (!removeRole && !rolesFit)
// Add role if it fits the criteria and the role isn't already assigned and the remove flag isn't set
if ((!removeRole && !rolesFit) && !remove)
await member.roles.add(roleId)
.then(e => logRoleUpdate(client, member, role, guildDatas, playerElo, ADD))
.catch((err) => handleRoleErrors(err, role))
Expand Down
4 changes: 2 additions & 2 deletions functions/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ const getCurrentEloString = (playerLastStats) => {

const getActiveGuildsEntitlements = async (client, renewCache = false) => {
if (renewCache) client.entitlements = await new EntitlementManager(client).fetch()
const activeGuildEntitlements = client.entitlements.filter(e => e.isActive() && e.isGuildSubscription())
const activeGuildEntitlements = client.entitlements?.filter(e => e.isActive() && e.isGuildSubscription())
return activeGuildEntitlements
}

const currentGuildIsPremium = async (client, guildId) => {
const premiumGuilds = await getActiveGuildsEntitlements(client)
const isPremium = premiumGuilds.map(g => g.guildId).includes(guildId)
const isPremium = premiumGuilds?.map(g => g.guildId).includes(guildId)
return isPremium
}

Expand Down
Loading

0 comments on commit 0b57040

Please sign in to comment.