Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bumped Discord.js to v14.11.0 + discord plugins + log-parser #297

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions core/log-parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Logger from '../logger.js';
import TailLogReader from './log-readers/tail.js';
import FTPLogReader from './log-readers/ftp.js';

const addedLines = [];

export default class LogParser extends EventEmitter {
constructor(filename = 'filename.log', options = {}) {
super();
Expand Down Expand Up @@ -46,24 +48,28 @@ export default class LogParser extends EventEmitter {
async processLine(line) {
Logger.verbose('LogParser', 4, `Matching on line: ${line}`);

for (const rule of this.getRules()) {
let i = this.getRules().length;
while (i--) {
const rule = this.getRules()[i];
const match = line.match(rule.regex);
if (!match) continue;

addedLines.push({ rule, match });
}
this.linesPerMinute += 1;
this.onLine(addedLines);
addedLines.length = 0;
}

onLine(addedLine) {
for(const ad of addedLine) {
const { rule, match } = ad;
Logger.verbose('LogParser', 3, `Matched on line: ${match[0]}`);

match[1] = moment.utc(match[1], 'YYYY.MM.DD-hh.mm.ss:SSS').toDate();
match[2] = parseInt(match[2]);

rule.onMatch(match, this);

this.matchingLinesPerMinute++;
this.matchingLatency += Date.now() - match[1];

break;
}

this.linesPerMinute++;
this.matchingLinesPerMinute += 1;
this.matchingLatency += Number(Date.now()) - match[1];
};
}

// manage cleanup disconnected players, session data.
Expand Down Expand Up @@ -100,7 +106,7 @@ export default class LogParser extends EventEmitter {
} lines per minute | Matching lines per minute: ${
this.matchingLinesPerMinute
} matching lines per minute | Average matching latency: ${
this.matchingLatency / this.matchingLinesPerMinute
Number.isNaN(this.matchingLatency / this.matchingLinesPerMinute) ? 0 : this.matchingLatency / this.matchingLinesPerMinute
}ms`
);
this.linesPerMinute = 0;
Expand Down
10 changes: 8 additions & 2 deletions squad-server/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

import Discord from 'discord.js';
import { Client, GatewayIntentBits } from 'discord.js';
import sequelize from 'sequelize';
import AwnAPI from './utils/awn-api.js';

Expand Down Expand Up @@ -103,7 +103,13 @@ export default class SquadServerFactory {
Logger.verbose('SquadServerFactory', 1, `Starting ${type} connector ${connectorName}...`);

if (type === 'discord') {
const connector = new Discord.Client();
const connector = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
await connector.login(connectorConfig);
return connector;
}
Expand Down
2 changes: 1 addition & 1 deletion squad-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"axios": "^0.21.1",
"core": "1.0.0",
"didyoumean": "^1.2.1",
"discord.js": "^12.3.1",
"discord.js": "^14.11.0",
"gamedig": "^2.0.20",
"graphql": "^15.4.0",
"graphql-request": "^3.4.0",
Expand Down
98 changes: 47 additions & 51 deletions squad-server/plugins/cbl-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export default class CBLInfo extends DiscordBasePlugin {

async onPlayerConnected(info) {
try {
const steamID = this.isValid(info, '<steamID>');
if(!steamID) {
return;
};
const data = await request(
'https://communitybanlist.com/graphql',
gql`
Expand Down Expand Up @@ -85,73 +89,65 @@ export default class CBLInfo extends DiscordBasePlugin {
}
}
`,
{ id: info.player.steamID }
{ id: steamID }
);

if (!data.steamUser) {
this.verbose(
2,
`Player ${info.player.name} (Steam ID: ${info.player.steamID}) is not listed in the Community Ban List.`
`Player ${info.player.name} (Steam ID: ${steamID}) is not listed in the Community Ban List.`
);
return;
}

if (data.steamUser.reputationPoints < this.options.threshold) {
this.verbose(
2,
`Player ${info.player.name} (Steam ID: ${info.player.steamID}) has a reputation below the threshold.`
`Player ${info.player.name} (Steam ID: ${steamID}) has a reputation below the threshold.`
);
return;
}

await this.sendDiscordMessage({
embed: {
title: `${info.player.name} is a potentially harmful player!`,
author: {
name: 'Community Ban List',
url: 'https://communitybanlist.com/',
icon_url: 'https://communitybanlist.com/static/media/cbl-logo.caf6584e.png'
},
thumbnail: {
url: data.steamUser.avatarFull
},
description: `[${info.player.name}](https://communitybanlist.com/search/${info.player.steamID}) has ${data.steamUser.reputationPoints} reputation points on the Community Ban List and is therefore a potentially harmful player.`,
fields: [
{
name: 'Reputation Points',
value: `${data.steamUser.reputationPoints} (${
data.steamUser.reputationPointsMonthChange || 0
} from this month)`,
inline: true
},
{
name: 'Risk Rating',
value: `${data.steamUser.riskRating} / 10`,
inline: true
},
{
name: 'Reputation Rank',
value: `#${data.steamUser.reputationRank}`,
inline: true
},
{
name: 'Active Bans',
value: `${data.steamUser.activeBans.edges.length}`,
inline: true
},
{
name: 'Expired Bans',
value: `${data.steamUser.expiredBans.edges.length}`,
inline: true
}
],
color: '#ffc40b',
timestamp: info.time.toISOString(),
footer: {
text: 'Powered by SquadJS and the Community Ban List'
}
const embed = this.buildEmbed()
.setColor('#ffc40b')
.setTitle(`${info.player.name} is a potentially harmful player!`)
.setAuthor({
name: 'Community Ban List',
iconURL: 'https://communitybanlist.com/static/media/cbl-logo.caf6584e.png',
url: 'https://communitybanlist.com/'
})
.setDescription(`[${info.player.name}](https://communitybanlist.com/search/${steamID}) has ${data.steamUser.reputationPoints} reputation points on the Community Ban List and is therefore a potentially harmful player.`)
.setThumbnail(data.steamUser.avatarFull)
.addFields(
{
name: 'Reputation Points',
value: `${data.steamUser.reputationPoints} (${
data.steamUser.reputationPointsMonthChange || 0
} from this month)`,
inline: true
},
{
name: 'Risk Rating',
value: `${data.steamUser.riskRating} / 10`,
inline: true
},
{
name: 'Reputation Rank',
value: `#${data.steamUser.reputationRank}`,
inline: true
},
{
name: 'Active Bans',
value: `${data.steamUser.activeBans.edges.length}`,
inline: true
},
{
name: 'Expired Bans',
value: `${data.steamUser.expiredBans.edges.length}`,
inline: true
}
});
)
.setFooter({ text: 'Powered by SquadJS and the Community Ban List', iconURL: null });
await this.sendDiscordMessage({ embeds: [embed] });
} catch (err) {
this.verbose(
1,
Expand Down
16 changes: 4 additions & 12 deletions squad-server/plugins/discord-admin-broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,10 @@ export default class DiscordAdminBroadcast extends DiscordBasePlugin {
}

async onAdminBroadcast(info) {
await this.sendDiscordMessage({
embed: {
title: 'Admin Broadcast',
color: this.options.color,
fields: [
{
name: 'Message',
value: `${info.message}`
}
],
timestamp: info.time.toISOString()
}
const embed = this.buildEmbed(this.options.color, info.time, 'Admin Broadcast').addFields({
name: 'Message',
value: `${info.message}`
});
await this.sendDiscordMessage(this.objEmbed(embed));
}
}
68 changes: 28 additions & 40 deletions squad-server/plugins/discord-admin-cam-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,50 +46,38 @@ export default class DiscordAdminCamLogs extends DiscordBasePlugin {
}

async onEntry(info) {
await this.sendDiscordMessage({
embed: {
title: `Admin Entered Admin Camera`,
color: this.options.color,
fields: [
{
name: "Admin's Name",
value: info.player.name,
inline: true
},
{
name: "Admin's SteamID",
value: `[${info.player.steamID}](https://steamcommunity.com/profiles/${info.player.steamID})`,
inline: true
}
],
timestamp: info.time.toISOString()
const embed = this.buildEmbed(this.options.color, info.time, 'Admin Entered Admin Camera').addFields(
{
name: "Admin's Name",
value: info.player.name,
inline: true
},
{
name: "Admin's SteamID",
value: `[${info.player.steamID}](https://steamcommunity.com/profiles/${info.player.steamID})`,
inline: true
}
});
);
await this.sendDiscordMessage(this.objEmbed(embed));
}

async onExit(info) {
await this.sendDiscordMessage({
embed: {
title: `Admin Left Admin Camera`,
color: this.options.color,
fields: [
{
name: "Admin's Name",
value: info.player.name,
inline: true
},
{
name: "Admin's SteamID",
value: `[${info.player.steamID}](https://steamcommunity.com/profiles/${info.player.steamID})`,
inline: true
},
{
name: 'Time in Admin Camera',
value: `${Math.round(info.duration / 60000)} mins`
}
],
timestamp: info.time.toISOString()
const embed = this.buildEmbed(this.options.color, info.time, 'Admin Left Admin Camera').addFields(
{
name: "Admin's Name",
value: info.player.name,
inline: true
},
{
name: "Admin's SteamID",
value: `[${info.player.steamID}](https://steamcommunity.com/profiles/${info.player.steamID})`,
inline: true
},
{
name: 'Time in Admin Camera',
value: `${Math.round(info.duration / 60000)} mins`
}
});
);
await this.sendDiscordMessage(this.objEmbed(embed));
}
}
55 changes: 25 additions & 30 deletions squad-server/plugins/discord-admin-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,32 @@ export default class DiscordAdminRequest extends DiscordBasePlugin {
await this.server.rcon.warn(player.steamID, `[${info.player.name}] - ${info.message}`);
}

const message = {
embed: {
title: `${info.player.name} has requested admin support!`,
color: this.options.color,
fields: [
{
name: 'Player',
value: info.player.name,
inline: true
},
{
name: 'SteamID',
value: `[${info.player.steamID}](https://steamcommunity.com/profiles/${info.player.steamID})`,
inline: true
},
{
name: 'Team & Squad',
value: `Team: ${info.player.teamID}, Squad: ${info.player.squadID || 'Unassigned'}`
},
{
name: 'Message',
value: info.message
},
{
name: 'Admins Online',
value: amountAdmins
}
],
timestamp: info.time.toISOString()
const message = { };
const embed = this.buildEmbed(this.options.color, info.time, `${info.player.name} has requested admin support!`).addFields(
{
name: 'Player',
value: info.player.name,
inline: true
},
{
name: 'SteamID',
value: `[${info.player.steamID}](https://steamcommunity.com/profiles/${info.player.steamID})`,
inline: true
},
{
name: 'Team & Squad',
value: `Team: ${info.player.teamID}, Squad: ${info.player.squadID || 'Unassigned'}`
},
{
name: 'Message',
value: info.message
},
{
name: 'Admins Online',
value: amountAdmins
}
};
);
Object.assign(message, this.objEmbed(embed));

if (this.options.pingGroups.length > 0 && Date.now() - this.options.pingDelay > this.lastPing) {
message.content = this.options.pingGroups.map((groupID) => `<@&${groupID}>`).join(' ');
Expand Down
4 changes: 2 additions & 2 deletions squad-server/plugins/discord-base-message-updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export default class DiscordBaseMessageUpdater extends BasePlugin {
}

async mount() {
this.options.discordClient.on('message', this.onDiscordMessage);
this.options.discordClient.on('messageCreate', this.onDiscordMessage);
}

async unmount() {
this.options.discordClient.removeEventListener('message', this.onDiscordMessage);
this.options.discordClient.removeEventListener('messageCreate', this.onDiscordMessage);
}

async generateMessage() {
Expand Down
Loading