Skip to content

Commit

Permalink
Move bot to src folder, update MongoDB, fix crash.
Browse files Browse the repository at this point in the history
- VSCode debugging is broken at the moment.
  • Loading branch information
retrixe committed Jul 30, 2021
1 parent 5b444e7 commit 380ec3a
Show file tree
Hide file tree
Showing 24 changed files with 107 additions and 126 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ It's planned to have some nifty commands like /assistant which directly communic
- `/purge`
- `/slowmode`

[Complete list of commands along with their descriptions available here.](https://github.com/retrixe/IveBot/blob/master/server/bot/commands/help.ts#L6)
[Complete list of commands along with their descriptions available here.](https://github.com/retrixe/IveBot/blob/master/src/commands/help.ts#L6)

</details>
<hr />
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "ivebot",
"version": "3.2.0",
"description": "The bot that created the iPhone X.",
"main": "server/index.ts",
"main": "src/index.ts",
"private": true,
"repository": "https://github.com/retrixe/IveBot.git",
"scripts": {
"prepare": "husky install",
"lint": "eslint . --ext .js,.ts,.tsx && tsc --noEmit && graphql-schema-linter dashboard/schema.graphql",
"start": "node lib/index.js --production",
"build": "tsc",
"dev": "nodemon server/index.ts",
"dev": "nodemon src/index.ts",
"mongo": "mongod --dbpath=database"
},
"author": "Ibrahim Ansari <ansari.ibrahim1@gmail.com>",
Expand All @@ -24,12 +24,11 @@
"json5": "^0.5.1",
"mathjs": "^9.4.3",
"moment": "^2.24.0",
"mongodb": "^3.0.6",
"mongodb": "^4.0.1",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@babel/core": "^7.1.6",
"@types/mongodb": "^3.0.12",
"@types/node": "^16.4.7",
"@types/node-fetch": "^2.5.12",
"@typescript-eslint/eslint-plugin": "^1.5.0",
Expand Down
2 changes: 1 addition & 1 deletion server/bot/client.ts → src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Message, MessageContent, Client, GuildTextableChannel } from 'eris'
import { DB, Command as IveBotCommand, IveBotCommandGenerator, Context, CommandResponse } from './imports/types'
import { Db } from 'mongodb'
import { getInsult } from './imports/tools'
import botCallback from '.'
import botCallback from './events'

function isEquivalent (a: { [index: string]: boolean }, b: { [index: string]: boolean }) {
// Create arrays of property names
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import moment from 'moment'
import { ObjectID } from 'mongodb'
import { ObjectId } from 'mongodb'
import { Command } from '../../imports/types'
import { getUser, getInsult } from '../../imports/tools'
import { checkRolePosition } from '../../imports/permissions'
Expand Down Expand Up @@ -174,15 +174,15 @@ export const handleRemovewarn: Command = {
// Remove the warning of the person internally.
try {
const warn = await db.collection('warnings').findOne({
_id: new ObjectID(args[0]), serverID: message.member.guild.id
_id: new ObjectId(args[0]), serverID: message.member.guild.id
})
if (!warn) return { content: 'This warning does not exist..', error: true }
else if (warn.warnedID !== user.id) {
return { content: 'This warning does not belong to the specified user..', error: true }
}
try {
await db.collection('warnings').deleteOne({
_id: new ObjectID(args[0]), serverID: message.member.guild.id
_id: new ObjectId(args[0]), serverID: message.member.guild.id
})
} catch (e) { return `Something went wrong 👾 Error: ${e}` }
} catch (err) { return `Something went wrong 👾 Error: ${err}` }
Expand Down
2 changes: 1 addition & 1 deletion server/bot/commands/api.ts → src/commands/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { zeroWidthSpace, getInsult, fetchLimited, getIdFromMention } from '../im
import 'json5/lib/require'
import {
NASAtoken, fixerAPIkey, weatherAPIkey, oxfordAPI, cvAPIkey
} from '../../../config.json5'
} from '../../config.json5'

export const handleOcr: Command = {
name: 'ocr',
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion server/bot/commands/help.ts → src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command as IveBotCommand } from '../imports/types'
import { zeroWidthSpace, getInsult } from '../imports/tools'
import { rootURL } from '../../../config.json5'
import { rootURL } from '../../config.json5'
import { Command } from '../client'

const generalHelp = {
Expand Down
4 changes: 2 additions & 2 deletions server/bot/commands/tools.ts → src/commands/tools.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Command } from '../imports/types'
import { randomBytes } from 'crypto'
import { version } from '../../../package.json'
import { version } from '../../package.json'
import { execSync } from 'child_process'
import moment from 'moment'
import 'json5/lib/require'
import { host, testPilots } from '../../../config.json5'
import { host, testPilots } from '../../config.json5'
import { runInNewContext } from 'vm'
import { inspect } from 'util'
import { getIdFromMention, getInsult } from '../imports/tools'
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Command } from '../imports/types'
import { getIdFromMention, getInsult, getUser } from '../imports/tools'
import ms from 'ms'
import 'json5/lib/require'
import { host, testPilots } from '../../../config.json5'
import { host, testPilots } from '../../config.json5'
import moment from 'moment'

export const handleServerinfo: Command = {
Expand Down Expand Up @@ -400,7 +400,7 @@ export const handleRemindme: Command = {
channel ? message.author.mention + ' ' : ''
} ${args.slice(channel ? 2 : 1).join(' ')}\nReminder set ${args[0]} ago.`
})
if (res.insertedCount !== 1) return 'Failed to add a reminder to the database!'
if (!res.acknowledged) return 'Failed to add a reminder to the database!'
} catch (e) { return 'Failed to add a reminder to the database!' + channel ? '' : ' Can I DM you?' }
} else {
setTimeout(async () => {
Expand Down
4 changes: 2 additions & 2 deletions server/bot/index.ts → src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Db } from 'mongodb'
// Database reading function.
import { getServerSettings } from './imports/tools'
// Tokens and stuffs.
import { cvAPIkey } from '../../config.json5'
import { cvAPIkey } from '../config.json5'

// When a server gains a member, this function will be called.
export const guildMemberAdd = (client: Client, db: Db, tempDB: DB) => async (
Expand Down Expand Up @@ -102,7 +102,7 @@ export default async (message: Message, client: Client, tempDB: DB, db: Db) => {
) return
} catch (e) {}
// Content of message and sendResponse.
const sendResponse = message.channel.createMessage
const sendResponse = (content: string) => message.channel.createMessage(content)
const command = message.content.toLowerCase()
// Handle answers to trivia
const session = tempDB.trivia[message.channel.id]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 7 additions & 9 deletions server/index.ts → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'json5/lib/require'
// Tokens and stuff.
import { Client } from 'eris'
import CommandParser from './bot/client'
// Get MongoDB.
import { MongoClient } from 'mongodb'
// Import fs.
import { readdir, statSync } from 'fs'
import { inspect } from 'util'
// Import types.
import { DB, Command } from './bot/imports/types'
import { DB, Command } from './imports/types'
// Import the bot.
import { guildMemberAdd, guildMemberRemove, guildDelete, guildBanAdd } from './bot'
import CommandParser from './client'
import { guildMemberAdd, guildMemberRemove, guildDelete, guildBanAdd } from './events'
// Get the token needed.
import { token, mongoURL } from '../config.json5'

Expand All @@ -30,9 +30,7 @@ const client = new Client('Bot ' + token === 'dotenv' ? process.env.IVEBOT_TOKEN
client.connect()

// Create a MongoDB instance.
MongoClient.connect(mongoURL === 'dotenv' ? process.env.MONGO_URL : mongoURL, {
useNewUrlParser: true
}, (err, mongoDB) => {
MongoClient.connect(mongoURL === 'dotenv' ? process.env.MONGO_URL : mongoURL, (err, mongoDB) => {
if (err) throw new Error('Error:\n' + err)
console.log('Bot connected successfully to MongoDB.')
const db = mongoDB.db('ivebot')
Expand All @@ -46,8 +44,8 @@ MongoClient.connect(mongoURL === 'dotenv' ? process.env.MONGO_URL : mongoURL, {
const commandParser = new CommandParser(client, tempDB, db)
client.on('messageCreate', commandParser.onMessage)
client.on('messageUpdate', commandParser.onMessageUpdate)
// Register all commands in bot/commands onto the CommandParser.
const toRead = dev ? './server/bot/commands/' : './lib/bot/commands/'
// Register all commands in src/commands onto the CommandParser.
const toRead = dev ? './src/commands/' : './lib/commands/'
readdir(toRead, (err, commandFiles) => {
// Handle any errors.
if (err) { console.error(err); throw new Error('Commands could not be retrieved.') }
Expand All @@ -57,7 +55,7 @@ MongoClient.connect(mongoURL === 'dotenv' ? process.env.MONGO_URL : mongoURL, {
commandFiles.forEach(commandFile => {
// If it's a file..
if (statSync(toRead + commandFile).isFile() && (commandFile.endsWith('.ts') || commandFile.endsWith('.js'))) {
const commands: { [index: string]: Command } = require('./bot/commands/' + commandFile)
const commands: { [index: string]: Command } = require('./commands/' + commandFile)
// ..and there are commands..
if (!Object.keys(commands).length) return
// ..register the commands.
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"esModuleInterop": true
},
"include": [
"server/**/*.ts",
"src/**/*.ts",
"index.d.ts"
],
"exclude": [
Expand Down
Loading

0 comments on commit 380ec3a

Please sign in to comment.