Skip to content

Commit

Permalink
Refactor: Use native MongoDB driver instead of Mongoose, removed unus…
Browse files Browse the repository at this point in the history
…ed packages.
  • Loading branch information
RoarkGit committed Aug 9, 2023
1 parent 3679293 commit 8241fe8
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 754 deletions.
953 changes: 256 additions & 697 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
"author": "Liam Galt",
"license": "ISC",
"dependencies": {
"@discordjs/builders": "^1.6.4",
"@discordjs/rest": "^2.0.0",
"@supercharge/promise-pool": "^3.0.0",
"@xivapi/js": "^0.4.2",
"@xivapi/nodestone": "^0.2.5",
"axios": "^1.4.0",
"copyfiles": "^2.4.1",
"discord-api-types": "^0.37.51",
"discord.js": "^14.12.1",
"dotenv": "^16.3.1",
"mongoose": "^7.4.2",
"mongodb": "^5.7.0",
"ts-node": "^10.9.1",
"winston": "^3.10.0",
"winston-loki": "^6.0.7"
"winston-loki": "^6.0.7",
"winston-transport": "^4.5.0"
},
"devDependencies": {
"@types/node": "^20.4.7",
Expand All @@ -36,7 +35,6 @@
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-promise": "^6.1.1",
"nodemon": "^3.0.1",
"typedoc": "^0.24.8",
"typescript": "^5.1.6"
}
}
16 changes: 9 additions & 7 deletions src/commands/grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Strago } from '../interfaces/Strago'

import { CommandInteraction, GuildMemberRoleManager, SlashCommandBuilder, TextChannel } from 'discord.js'

import CharacterModel from '../database/models/CharacterModel'
import { Command } from '../interfaces/Command'
import * as xivlib from '../modules/xivlib'

Expand All @@ -25,14 +24,15 @@ export const grant: Command = {

// Check if user recently ran command.
if (strago.grantSpamSet.has(member.id)) {
await interaction.reply({ content: 'You\'re doing that too quickly, wait at least ten minutes and try again.', ephemeral: true })
return
//await interaction.reply({ content: 'You\'re doing that too quickly, wait at least ten minutes and try again.', ephemeral: true })
//return
} else {
strago.grantSpamSet.add(member.id)
}

await interaction.reply({ content: 'Checking registration...', ephemeral: true })
const character = await CharacterModel.findOne({
const characters = strago.db.collection('characters')
const character = await characters.findOne({
discordId: interaction.user.id
})

Expand All @@ -41,7 +41,7 @@ export const grant: Command = {
return
}

const achievementsPublic = await xivlib.getAchievementsPublic(character.get('characterId') as string)
const achievementsPublic = await xivlib.getAchievementsPublic(character.characterId as string)

if (!achievementsPublic) {
await interaction.editReply({ content: 'I could not view your achievements, please make sure they are public and try again.' })
Expand All @@ -56,15 +56,16 @@ export const grant: Command = {
await interaction.editReply({ content: lines.join('\n') })
}

const characterName: string = character.get('characterName') as string
const characterName: string = character.characterName
await updateState(`Beginning achievement scan for ${characterName}...`)

const granted = new Set<string>()
const characterAchievements = await xivlib.getAchievementsComplete(
character.get('characterId') as string, Object.keys(strago.data.achievementData.achievementIds))
character.characterId, Object.keys(strago.data.achievementData.achievementIds))
const memberRoles: GuildMemberRoleManager = member.roles

for (const role of strago.data.achievementData.roles) {
console.log(role)
const discordRole = guild.roles.cache.find(r => r.name === role.name)
if (discordRole === undefined) {
strago.logger.error(`Undefined role: ${role.name}`)
Expand Down Expand Up @@ -114,6 +115,7 @@ export const grant: Command = {
}

await updateState(`${role.name}: Granted!`)
console.log(discordRole)
await memberRoles.add(discordRole)
granted.add(role.name)

Expand Down
6 changes: 3 additions & 3 deletions src/commands/register.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Strago } from '../interfaces/Strago'

import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, CommandInteraction, ComponentType, MessageActionRowComponentBuilder, SlashCommandBuilder } from 'discord.js'
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, ComponentType, MessageActionRowComponentBuilder, SlashCommandBuilder } from 'discord.js'

import CharacterModel from '../database/models/CharacterModel'
import { Command } from '../interfaces/Command'
import * as xivlib from '../modules/xivlib'

Expand Down Expand Up @@ -164,7 +163,8 @@ export const register: Command = {
if (characterId != null && await xivlib.verifyCharacter(characterId)) {
strago.logger.info(`Successfully registered ${characterId}`)
await interaction.editReply({ content: 'You have successfully registered your character!', components: [] })
await CharacterModel.findOneAndUpdate(
const characters = strago.db.collection('characters')
await characters.findOneAndReplace(
{ discordId: i.user.id },
{ discordId: i.user.id, characterId, characterName: character },
{ upsert: true })
Expand Down
18 changes: 0 additions & 18 deletions src/database/models/CharacterModel.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/database/models/UserModel.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/events/checkLFGSpam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Strago } from '../interfaces/Strago'


import { ChannelType, Message, TextChannel } from 'discord.js'
import UserModel from '../database/models/UserModel'

const timeoutDuration = 60 * 60 * 1000

Expand All @@ -18,14 +17,15 @@ export const checkLFGSpam = async (message: Message, strago: Strago): Promise<vo
// Check if roles were actually mentioned.
if (message.mentions.roles.size > 0) {
if (strago.lfgSpamSet.has(member.id)) {
const user = await UserModel.findOne({
const users = strago.db.collection('users')
const user = await users.findOne({
discordId: member.id
})
if (user === null) {
await UserModel.create({ discordId: member.id, numTimeouts: 1 })
await users.insertOne({ discordId: member.id, numTimeouts: 1 })
} else {
await user.updateOne({$inc: {numTimeouts: 1}})
const numTimeouts = user.get('numTimeouts') + 1
await users.updateOne({ discordId: member.id }, {$inc: {numTimeouts: 1}})
const numTimeouts = user.numTimeouts + 1
await (strago.channels.cache.get(strago.config.modChannelId) as TextChannel).send(
`Timed out ${member}, they have been timed out ${numTimeouts} times.`
)
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join } from 'path'

import achievementData from './data/achievementData.json'
import { handleEvents } from './events/handleEvents'
import { connectDatabase } from './database/connectDatabase'
import { connectDatabase } from './utils/connectDatabase'
import { Spell } from './interfaces/Spell'
import { validateEnv } from './modules/validateEnv'
import { loadCommands } from './utils/loadCommands'
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/Strago.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Client, Collection } from 'discord.js'
import { Db } from 'mongodb'
import { Logger } from 'winston'

import { Command } from './Command'
Expand Down Expand Up @@ -42,6 +43,7 @@ export interface Strago extends Client {
}
spellData: Collection<string, Spell>
}
db: Db
grantSpamSet: TimeoutSet
lfgSpamSet: TimeoutSet
logger: Logger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Strago } from '../interfaces/Strago'

import { connect } from 'mongoose'
import { MongoClient } from 'mongodb'

/**
* Attempts to connect to the bot's database.
Expand All @@ -9,7 +9,8 @@ import { connect } from 'mongoose'
*/
export const connectDatabase = async (strago: Strago): Promise<boolean> => {
try {
await connect(strago.config.databaseUri)
const client = new MongoClient(strago.config.databaseUri)
strago.db = client.db(strago.config.env)

return true
} catch (error) {
Expand Down

0 comments on commit 8241fe8

Please sign in to comment.