Skip to content

Commit

Permalink
Merge pull request #339 from Blumlaut/djs-v14-dev
Browse files Browse the repository at this point in the history
chore(deps): update to v14, currently broken
  • Loading branch information
Blumlaut authored Mar 10, 2022
2 parents 965720b + bc9a115 commit c7dad2d
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 49 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ EasyAdmin is an Administration Suite for FiveM and RedM Servers, EasyAdmin is fe
- Translation in 7 Languages (Community Driven)
- Actively Supported & Updated since 2017
- Plugin Support
- Fully integrated Discord Bot, including Discord ACE Permissions, Chat Bridge, Commands and Logs

![image](https://user-images.githubusercontent.com/13604413/126916981-1680e5ac-e024-467b-aad3-a5a9658449e0.png)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"@discordjs/rest": "0.3.0",
"ascii-table": "0.0.9",
"discord-api-types": "0.28.0",
"discord.js": "13.6.0",
"discord.js": "14.0.0-dev.1646914074.dcd4797",
"erlpack": "0.1.4",
"juration": "0.1.1",
"pretty-ms": "7.0.1",
Expand Down
9 changes: 8 additions & 1 deletion server/admin_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,14 @@ Citizen.CreateThread(function()

cachePlayer(source) -- this will do nothing if player is already cached.

if CachedPlayers[source].lastPermRequest and CachedPlayers[source].lastPermRequest+15 > os.time() then
if CachedPlayers[source].lastPermRequest and CachedPlayers[source].lastPermRequest+10 > os.time() then
PrintDebugMessage(getName(source).." hit Permission Check Ratelimit! "..CachedPlayers[source].lastPermRequest+15-os.time().." seconds left.", 3)
return
end

if GetConvar("ea_botToken", "") ~= "" then
exports[GetCurrentResourceName()]:syncDiscordRoles(source)
end
CachedPlayers[source].lastPermRequest = os.time()

local identifiers = getAllPlayerIdentifiers(source)
Expand Down Expand Up @@ -1059,6 +1063,9 @@ Citizen.CreateThread(function()
end
t.id = #reports+1
reports[t.id] = t
if GetConvar("ea_botLogChannel", "") ~= "" then
exports[GetCurrentResourceName()]:logNewReport(t)
end
for i,_ in pairs(OnlineAdmins) do
TriggerLatentClientEvent("EasyAdmin:NewReport", i, 10000, t)
end
Expand Down
11 changes: 5 additions & 6 deletions server/bot/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ process.on('unhandledRejection', function(err) {
console.log('Caught exception: ', err.stack);
});

Discord = require("discord.js")
AsciiTable = require('ascii-table')
sprintf = require('sprintf-js').sprintf
juration = require('juration');
const prettyMilliseconds = require('pretty-ms');
const { MessageAttachment, Collection, Intents, MessageActionRow, MessageButton, MessageSelectMenu, Guild } = require('discord.js');
const { Client, Embed, MessageAttachment, Collection, GatewayIntentBits, Partials, ButtonStyle,ActionRow, ButtonComponent, SelectMenuComponent, Guild, Util } = require('discord.js');
const { SlashCommandBuilder } = require('@discordjs/builders');


client = new Discord.Client({
partials: ['GUILD_MEMBER', 'USER', 'MESSAGE', 'CHANNEL', 'REACTION'],
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS]
client = new Client({
partials: [Partials.GuildMember, Partials.User, Partials.Message, Partials.Channel, Partials.Reaction],
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers]
});
client.commands = new Collection();

Expand Down Expand Up @@ -75,7 +74,7 @@ if (GetConvar("ea_botToken", "") != "") {
.catch(console.error);

client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
if (!interaction.isChatInputCommand()) return;

const command = client.commands.get(interaction.commandName);

Expand Down
20 changes: 10 additions & 10 deletions server/bot/commands/playerinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,30 @@ module.exports = {
}


var embed = new Discord.MessageEmbed()
var embed = new Embed()
.setColor((65280))
.setTimestamp()

embed.addField('Player Info', `Player infos for **${user.name}**`)
embed.addField('Discord Account', `\`\`\`${discordAccount}\`\`\``, true)
embed.addField('Admin', `\`\`\`${exports[EasyAdmin].IsPlayerAdmin(user.id)}\`\`\``, true)
embed.addField('Warnings', `\`\`\`${exports[EasyAdmin].getPlayerWarnings(user.id)}\`\`\``, true)
embed.addFields({ name: 'Player Info', value: `Player infos for **${user.name}**`})
embed.addFields({ name: 'Discord Account', value: `\`\`\`${discordAccount}\`\`\``, inline: true})
embed.addFields({ name: 'Admin', value: `\`\`\`${exports[EasyAdmin].IsPlayerAdmin(user.id)}\`\`\``, inline: true})
embed.addFields({ name: 'Warnings', value: `\`\`\`${exports[EasyAdmin].getPlayerWarnings(user.id)}\`\`\``, inline: true})



if (!user.dropped) {
var playerPed = GetPlayerPed(user.id)
embed.addField('Health', `\`\`\`${GetEntityHealth(playerPed)}\`\`\``, true)
embed.addField('Armour', `\`\`\`${GetPedArmour(playerPed)}\`\`\``, true)
embed.addFields({ name: 'Health', value: `\`\`\`${GetEntityHealth(playerPed)}\`\`\``, inline: true})
embed.addFields({ name: 'Armour', value: `\`\`\`${GetPedArmour(playerPed)}\`\`\``, inline: true})
if (GetPlayerInvincible(user.id)) {
embed.addField('Godmode', '\`\`\`ON\`\`\`', true)
embed.addFields({ name: 'Godmode', value: '\`\`\`ON\`\`\`', inline: true})
}
} else {
embed.addField('Status', `\`\`\`Player Disconnected\`\`\``)
embed.addFields({ name: 'Status', value: `\`\`\`Player Disconnected\`\`\``})
}


embed.addField('Identifiers', `\`\`\`${table}\`\`\``)
embed.addFields({ name: 'Identifiers', value: `\`\`\`${table}\`\`\``})

await interaction.reply({ embeds: [embed]});
},
Expand Down
17 changes: 8 additions & 9 deletions server/bot/commands/playerlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@


function generatePaginatorRow(idFields, curPage, embedTimestamp) {
const row = new MessageActionRow()
const row = new ActionRow()

var selector = new MessageSelectMenu()
var selector = new SelectMenuComponent()

var fieldLength = idFields.length
if (fieldLength == 0) {fieldLength = 1}
selector.setCustomId(`pageSelector${embedTimestamp}`)
selector.setPlaceholder(`Page ${curPage+1}/${fieldLength}`)

for (var i = 0; i < fieldLength; i++) {
selector.addOptions([
selector.addOptions(
{
label: `Page ${i+1}/${(fieldLength)}`,
value: `${i}`,
}])
})
}
if (!idFields[1]) {
selector.setDisabled(true)
}
row.addComponents(selector)

return row

}
Expand Down Expand Up @@ -58,7 +57,7 @@ module.exports = {
var players = await exports[EasyAdmin].getCachedPlayers()
var embedTimestamp = Date.now();

var embed = new Discord.MessageEmbed()
var embed = new Embed()
.setColor((65280))
.setTimestamp()

Expand Down Expand Up @@ -114,7 +113,7 @@ module.exports = {
});

collector.on('collect', async i => {
const embed = new Discord.MessageEmbed()
const embed = new Embed()
.setColor((65280))
.setTimestamp()
if (i.customId === `pageSelector${embedTimestamp}`) {
Expand All @@ -133,10 +132,10 @@ module.exports = {
});
}
} else {
embed = new Discord.MessageEmbed()
embed = new Embed()
.setColor((65280))
.setTimestamp()
.addField('Player List', "There are no players on the server!")
.addFields({name: 'Player List', value: "There are no players on the server!"})

row = generatePaginatorRow(idFields, 0, 0)
}
Expand Down
30 changes: 25 additions & 5 deletions server/bot/functions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// this file contains util functions the bot uses
async function LogDiscordMessage() {
async function LogDiscordMessage(text, feature) {
if (GetConvar("ea_botLogChannel", "") == "") {return}
var text = Array.from(arguments).toString();
if (feature == "report" || feature == "calladmin") {return} // we dont care about reports, these get handled in reports.js

const embed = await prepareGenericEmbed(text)

Expand All @@ -16,13 +16,13 @@ async function prepareGenericEmbed(message,feature,colour,title,image,customAuth
return
}

const embed = new Discord.MessageEmbed()
.setColor((colour || 65280))
const embed = new Embed()
.setColor(Util.resolveColor(colour || 65280))
if (timestamp != false) {
embed.setTimestamp()
}
if (message) {
embed.addField(`**${(title || "EasyAdmin")}**`, message)
embed.addFields({name: `**${(title || "EasyAdmin")}**`, value: message})
}
if (description) {
embed.setDescription(description)
Expand Down Expand Up @@ -79,6 +79,9 @@ async function DoesGuildMemberHavePermission(member, object) { // wrapper for Di

async function getDiscordAccountFromPlayer(user) {
var discordAccount = false
if (!isNaN(user)) {
user = await exports[EasyAdmin].getCachedPlayer(user)
}

for (let identifier of user.identifiers) {
if (identifier.search("discord:") != -1) {
Expand All @@ -87,5 +90,22 @@ async function getDiscordAccountFromPlayer(user) {
}

return discordAccount
}


async function getPlayerFromDiscordAccount(user) {
var id = user.id

var players = await exports[EasyAdmin].getCachedPlayers()

for (let [index, player] of Object.values(players).entries()) {
for (let identifier of player.identifiers) {
if (identifier == `discord:${id}`) {
return player
}
}
}

return false

}
104 changes: 104 additions & 0 deletions server/bot/reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

var reports = []

function generateReportEmbed(report, disabled, closed) {
var reportId = report.id

var embed = new Embed()
.setTimestamp()

if (closed) {
embed.setColor((808080))
} else {
embed.setColor((65280))
}

if (report.type == 1) {
embed.addFields({name:"Player Report", value: `**${report.reporterName}** reported **${report.reportedName}**!`})
} else {
embed.addFields({name:"Admin Call", value: `**${report.reporterName}** called for an Admin!`})
}

embed.addFields(
{name:"Reason", value: `\`\`\`\n${report.reason}\`\`\``},
{name:"Report ID", value: `#${report.id}`, inline: true},
{name:"Claimed by", value:`${(report.claimedName || "Noone")}`, inline: true})



/*
const row = new ActionRow()
var button = new ButtonComponent()
button.setCustomId(`report_close${reportId}`)
button.setLabel('Close Report')
if (disabled) {
button.setDisabled(true)
}
row.addComponents(button)
*/

return {embeds: [embed], /* components: [row] */}

}

async function logNewReport(report) {
var reportId = report.id
reports[reportId] = report



var reportMessage = generateReportEmbed(report)


var msg = await client.channels.cache.get(GetConvar("ea_botLogChannel", "")).send(reportMessage)
reports[reportId].msg = msg


/*
const filter = i => (i.customId === `report_close${reportId}`);
const collector = interaction.channel.createMessageComponentCollector({
filter,
time: 240000
});
collector.on('collect', async i => {
if (i.customId === `report_close${reportId}` && await DoesGuildMemberHavePermission(i.member, "player.reports.process")) {
var reportMessage = generateReportEmbed(report, true, true)
msg.edit(reportMessage)
}
await i.deferUpdate()
});
*/

}
exports('logNewReport', logNewReport)


onNet("EasyAdmin:ClaimReport", async function(reportId) {
var src = source
if (await exports[EasyAdmin].DoesPlayerHavePermission(src, "player.reports.claim")) {
if (!reports[reportId].claimed) {
reports[reportId].claimed = src
reports[reportId].claimedName = exports[EasyAdmin].getName(src,true)
var reportMessage = generateReportEmbed(reports[reportId], true)
reports[reportId].msg.edit(reportMessage)
}
}
})

onNet("EasyAdmin:RemoveReport", async function(report) {
if (await exports[EasyAdmin].DoesPlayerHavePermission(source, "player.reports.process")) {
var reportId = report.id
var reportMessage = generateReportEmbed(reports[reportId], true, true)
reports[reportId].msg.edit(reportMessage)
reports[reportId] = undefined
}
})

// todo: close similar reports
43 changes: 43 additions & 0 deletions server/bot/roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

if (GetConvar("ea_botToken", "") != "") {


async function syncDiscordRoles(player) {
if (!EasyAdmin) {return} // bot is down
var src = player
var member = undefined
try {
var botGuild = await client.guilds.cache.get(guild)
var user = await getDiscordAccountFromPlayer(src)
member = await botGuild.members.fetch(user.id)
} catch (error) {
console.error(error)
return
}

var roles = await member.roles.cache.keys()

for (var role of roles) {
ExecuteCommand(`add_principal identifier.discord:${member.id} role:${role}`)
}
}
exports('syncDiscordRoles', syncDiscordRoles)

client.on("guildMemberUpdate", async function(oldMember, newMember){
var player = await getPlayerFromDiscordAccount(newMember)
if (player) {
oldRoles = await oldMember.roles.cache.keys()

for (var role of oldRoles) {
ExecuteCommand(`remove_principal identifier.discord:${oldMember.id} role:${role}`)
}

newRoles = await newMember.roles.cache.keys()

for (var role of newRoles) {
ExecuteCommand(`add_principal identifier.discord:${newMember.id} role:${role}`)
}
}
});

}
Loading

0 comments on commit c7dad2d

Please sign in to comment.