Skip to content

Instantly share code, notes, and snippets.

@d0p3t
Created March 10, 2020 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d0p3t/e409dff72bb3e68bc67ae237eafc2a77 to your computer and use it in GitHub Desktop.
Save d0p3t/e409dff72bb3e68bc67ae237eafc2a77 to your computer and use it in GitHub Desktop.
Using mongoose with FiveM
const webpack = require("webpack");
const Q3Rcon = require("quake3-rcon");
const config = require("./.local.json");
const rcon = new Q3Rcon({
address: config.ip || "127.0.0.1",
port: config.port || 30120,
password: config.password
});
const compiler = webpack({
entry: "./src/server/server.ts",
mode: "development",
node: {
fs: "empty"
},
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/
}
]
},
plugins: [new webpack.DefinePlugin({ "global.GENTLY": false })],
optimization: {
minimize: false
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
output: {
filename: "server.js",
path: __dirname + "/dist/"
},
target: "async-node"
});
compiler.watch(
{
ignored: /node_modules/
},
(err, stats) => {
if (err !== null) {
console.error(err);
return;
}
console.log(
`Built new {${stats.hash}} in ${((stats.endTime - stats.startTime) /
1000) |
0}s`
);
rcon.send(`restart dope-logging`);
}
);
import * as mongoose from "mongoose";
import * as bluebird from "bluebird";
import { DiscordClient } from "./discord";
import { default as User } from "./models/User";
(<any>mongoose).Promise = bluebird;
const bot = new DiscordClient(
"x",
"1",
"2"
);
on("onServerResourceStart", async (resourceName: string) => {
if (GetCurrentResourceName() !== resourceName) return;
let mongoUrl = GetConvar("mongo_url", "mongodb://127.0.0.1:27017/dopelogs");
mongoose
.connect(mongoUrl, { useNewUrlParser: true, useFindAndModify: false })
.then(() => {
console.info("Connected to MongoDB");
})
.catch(err => {
console.error(`Failed to connect to MongoDB. Error: ${err}`);
});
await bot.assignRole("1234").catch(err => {
console.log(err);
});
});
on("playerConnecting", async (name: string, callback: any, deferrals: any) => {
let _source = (<any>global).source;
deferrals.defer();
let numIdentifiers = GetNumPlayerIdentifiers(_source);
let list: string[] = [];
for (let index = 0; index < numIdentifiers; index++) {
let identifier = GetPlayerIdentifier(_source, index);
list.push(identifier);
}
if (list.find(x => x.substring(0, 8) == "license:") == null) {
console.warn(`${name} did not have a license identifier.`);
callback("License Identifier not found. Contact Server Owner.");
CancelEvent();
} else {
deferrals.update("Updating User Profile and Assigning Discord Role...");
let licenseId = list.find(x => x.substring(0, 8) == "license:").slice(8);
let endpoint = list.find(x => x.substring(0, 3) == "ip:").slice(3);
let steamId = list.find(x => x.substring(0, 6) == "steam:");
if (steamId) steamId = steamId.slice(6);
let discordId = list.find(x => x.substring(0, 8) == "discord:").slice(8);
if (discordId) discordId = discordId.slice(8);
let xblId = list.find(x => x.substring(0, 4) == "xbl:").slice(4);
if (xblId) xblId = xblId.slice(4);
let liveId = list.find(x => x.substring(0, 5) == "live:").slice(5);
if (liveId) liveId = liveId.slice(5);
await User.findOneAndUpdate(
{ licenseId },
{ endpoint, steamId, discordId, xblId, liveId },
{ upsert: true, new: true, setDefaultsOnInsert: true }
);
deferrals.done();
CancelEvent();
}
});
on("playerDropped", async (reason: string) => {
let _source = (<any>global).source;
let numIdentifiers = GetNumPlayerIdentifiers(_source);
let licenseId = "";
for (let index = 0; index < numIdentifiers; index++) {
let identifier = GetPlayerIdentifier(_source, index);
if (identifier.substring(0, 8) == "license:") {
licenseId = identifier.slice(8);
break;
}
}
await User.updateOne({ licenseId }, { disconnectReason: reason });
});
@XEKAOFF
Copy link

XEKAOFF commented May 14, 2020

it's a mixture of what language ts

@TheBrainOne
Copy link

for now mongoose on latest 5.9.20 version throwing a error:
Error loading script src/index.js in resource test: TypeError: firstLineError.stack.split is not a function stack: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] Failed to load script src/index.js.

@d0p3t have you encountered such a problem or maybe you know how to solve it?
can you share your package.json?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment