first real test

This commit is contained in:
Expand 2023-12-17 03:50:44 +11:00
parent 470506866b
commit 9b53f0486f
13 changed files with 587 additions and 7769 deletions

BIN
bun.lockb Executable file

Binary file not shown.

View file

@ -1,84 +0,0 @@
const { CommandInteraction, ApplicationCommandType, ApplicationCommandOptionType, EmbedBuilder } = require('discord.js');
const { sendResponse, sendReply } = require('../../../utils/utils');
const {dbclient} = require("../../../mongo")
const path = require('path');
const { Rcon } = require("rcon-client");
module.exports = {
name: `linkmc`,
description: `link your Discord Account to the Minecraft Server`,
cooldown: 5,
type: ApplicationCommandType.ChatInput,
options: [{
name: `mcusername`,
description: `The username of the MC account you want to link`,
type: ApplicationCommandOptionType.String,
required: true,
}],
/**
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
let mcusr = interaction.options.getString("mcusername")
let collection = await dbclient.db("ntcmcbot").collection("users");
let userdb = await collection.findOne({DISCORDID: interaction.member.id})
await interaction.deferReply()
let highest = "default";
switch(true) {
case interaction.member.roles.cache.has(process.env.MCSTAFF_ROLE):
highest = "mcstaff";
break;
case interaction.member.roles.cache.has(process.env.DISCORDSTAFF_ROLE):
highest = "discordstaff";
break;
case interaction.member.roles.cache.has(process.env.PATREON_ROLE):
highest = "patreon";
break;
case interaction.member.roles.cache.has(process.env.BOOSTER_ROLE):
highest = "booster";
break;
}
collection = await dbclient.db("ntcmcbot").collection("users");
// perform actions on the collection object
collection.updateOne(
{ DISCORDID: `${interaction.member.id}` }, // Query parameter
{ $set: { // Replacement document
DISCORDID: `${interaction.member.id}`,
MCUSER: `${mcusr}`,
USRROLE: `${highest}`
}},
{ upsert: true } // Options
)
const rcon = new Rcon({
host: `${process.env.MCHOST}`,
port: `${process.env.RCON_PORT}`,
password: `${process.env.RCONPASS}`,
});
let connected = true
let error
try{
await rcon.connect();
} catch(e){
console.log(e)
connected = false
error = e
}
console.log(userdb)
if(userdb.USRROLE != undefined){
let clear = await rcon.send(`lp user ${mcusr} group remove ${userdb.USRROLE}`)
}
let res = await rcon.send(`lp user ${mcusr} group add ${highest}`);
console.log(res)
await interaction.editReply(`added ${mcusr} to the role ${highest}`);
await rcon.end();
}
}

View file

@ -2,8 +2,6 @@ const { CommandInteraction, ApplicationCommandType, ApplicationCommandOptionType
const { sendResponse, sendReply, channelSend } = require('../../../utils/utils');
const path = require('path');
const { Rcon } = require("rcon-client");
const rconpass = process.env.RCONPASS;
const host = process.env.MCHOST;
@ -16,26 +14,24 @@ module.exports = {
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
console.log("test")
console.log(host);
const rcon = new Rcon({
host: `${host}`,
port: `${process.env.RCON_PORT}`,
password: `${rconpass}`,
});
let connected = true
let error
try{
await rcon.connect();
} catch(e){
console.log(e)
connected = false
error = e
}
let res = await rcon.send(`list`);
console.log(res)
var rep = res.replace(/§[6crf7]/g, "");
interaction.reply(`${rep}`, [],[],[], false);
rcon.end();
}
const { member, guild, options } = interaction
await interaction.deferReply({ ephemeral: true }).catch(err => console.error(`There was a problem deferring an interaction: `, err));
const rcon = new Rcon({
host: `${process.env.MCHOST}`,
port: parseInt(process.env.RCON_PORT),
password: `${process.env.RCON_PASS}`,
});
let connected = true
try{
await rcon.connect();
} catch(e){
console.log(e)
connected = false
sendResponse(interaction, `error sending command: ${e}`);
}
let res = await rcon.send(`list`);
rcon.end();
sendResponse(interaction, `${res}`);
},
}

View file

@ -0,0 +1,47 @@
const fs = require("fs");
const { CommandInteraction, ApplicationCommandType, ApplicationCommandOptionType, EmbedBuilder } = require('discord.js');
const { sendResponse } = require('../../../utils/utils');
const { Rcon } = require("rcon-client");
const { spawn } = require("child_process");
module.exports = {
name: `mc`,
description: `run mc command on server`,
cooldown: 5,
defaultMemberPermissions: ['ModerateMembers'],
type: ApplicationCommandType.ChatInput,
options: [{
name: `command`,
description: `the minecraft command`,
type: ApplicationCommandOptionType.String,
required: true
}],
/**
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
const { member, guild, options } = interaction
await interaction.deferReply({ ephemeral: true }).catch(err => console.error(`There was a problem deferring an interaction: `, err));
const rcon = new Rcon({
host: `${process.env.MCHOST}`,
port: parseInt(process.env.RCON_PORT),
password: `${process.env.RCON_PASS}`,
});
let connected = true
let content = options.getString("command")
try{
await rcon.connect();
} catch(e){
console.log(e)
connected = false
sendResponse(interaction, `error sending command: ${e}`);
}
let res = await rcon.send(`${content}`);
rcon.end();
sendResponse(interaction, `Sent Message: ${content}`);
},
};

View file

@ -0,0 +1,47 @@
const fs = require("fs");
const { CommandInteraction, ApplicationCommandType, ApplicationCommandOptionType, EmbedBuilder } = require('discord.js');
const { sendResponse } = require('../../../utils/utils');
const { Rcon } = require("rcon-client");
const { spawn } = require("child_process");
module.exports = {
name: `mcsay`,
description: `run mc command on server`,
cooldown: 5,
defaultMemberPermissions: ['ModerateMembers'],
type: ApplicationCommandType.ChatInput,
options: [{
name: `say`,
description: `speak to them jimmy`,
type: ApplicationCommandOptionType.String,
required: true
}],
/**
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
const { member, guild, options } = interaction
await interaction.deferReply({ ephemeral: true }).catch(err => console.error(`There was a problem deferring an interaction: `, err));
const rcon = new Rcon({
host: `${process.env.MCHOST}`,
port: parseInt(process.env.RCON_PORT),
password: `${process.env.RCON_PASS}`,
});
let connected = true
let content = options.getString("say")
command =`say ${member.user.username}: ${content} `;
try{
await rcon.connect();
} catch(e){
console.log(e)
connected = false
sendResponse(interaction, `error sending message: ${e}`);
}
let res = await rcon.send(`${command}`);
rcon.end();
sendResponse(interaction, `Sent Message: ${content}`);
},
};

View file

@ -1,84 +0,0 @@
const { CommandInteraction, ApplicationCommandType, ApplicationCommandOptionType, EmbedBuilder, PermissionsBitField } = require('discord.js');
const { sendResponse, sendReply } = require('../../../utils/utils');
const {dbclient} = require("../../../mongo")
const path = require('path');
const { Rcon } = require("rcon-client");
module.exports = {
name: `updateroles`,
description: `Manually update all roles`,
cooldown: 5,
defaultMemberPermissions: [PermissionsBitField.Flags.BanMembers],
type: ApplicationCommandType.ChatInput,
/**
* @param {CommandInteraction} interaction
*/
async execute(interaction) {
let changed = []
await interaction.deferReply()
const collection = dbclient.db("ntcmcbot").collection("users");
const cursor = await collection.find().forEach(async function(user){
let member = await interaction.guild.members.cache.get(user.DISCORDID)
console.log(`checking ${member.user.username}`)
let highest = "stdusr";
switch(true) {
case interaction.member.roles.cache.has(process.env.MCSTAFF_ROLE):
highest = "mcstaff";
break;
case interaction.member.roles.cache.has(process.env.DISCORDSTAFF_ROLE):
highest = "discordstaff";
break;
case interaction.member.roles.cache.has(process.env.PATREON_ROLE):
highest = "patreon";
break;
case interaction.member.roles.cache.has(process.env.BOOSTER_ROLE):
highest = "booster";
break;
}
if(highest != user.USRROLE){
collection.updateOne(
{ DISCORDID: `${user.DISCORDID}` }, // Query parameter
{ $set: { // Replacement document
USRROLE: `${highest}`
}},
{ upsert: true } // Options
)
console.log(`updated user in database to highest role`)
const rcon = new Rcon({
host: `${process.env.MCHOST}`,
port: `${process.env.RCON_PORT}`,
password: `${process.env.RCONPASS}`,
});
let connected = true
let error
try{
await rcon.connect();
} catch(e){
console.log(e)
connected = false
error = e
}
let clear = await rcon.send(`lp user ${user.MCUSR} group remove ${user.USRROLE}`)
let res = await rcon.send(`lp user ${user.MCUSR} group add ${highest}`);
changed.push(`${member.user.username}(${user.MCUSER}) -> ${highest}`)
}
if (changed.length <= 0){
sendResponse(interaction, `nothing to do here boss`)
}else {
sendResponse(interaction, `Heres what we changed boss: \n${changed}`)
}
})
}
}

View file

@ -1,11 +1,14 @@
const cronjob = require('cron').CronJob;
const { dbclient } = require('../../mongo');
const { spawn } = require('node:child_process');
const fs = require('fs');
const { promisify } = require('util');
const { glob } = require('glob');
const PG = promisify(glob);
const dig = require("gamedig")
const path = require('path');
const { channelSend } = require('../../utils/utils');
serverofflinefor = 0;
module.exports = {
name: 'ready',
@ -15,63 +18,37 @@ module.exports = {
console.log('Client is online!');
console.timeEnd('Time to online');
// Connect to database
const checkDB = new cronjob('59 23 * * *', async function () {
let guild = client.guilds.cache.get(`${process.env.GUILD_ID}`)
const collection = dbclient.db("ntcmcbot").collection("users");
const cursor = collection.find().forEach(async function(user){
runevery30seconds(1)
function runevery30seconds(i) {
setTimeout(() => {
let member = guild.members.cache.get(`${user.DISCORDID}`)
console.log(`updating ${member.name}`)
let highest = "default";
switch(true) {
case interaction.member.roles.cache.has(process.env.MCSTAFF_ROLE):
highest = "mcstaff";
break;
case interaction.member.roles.cache.has(process.env.DISCORDSTAFF_ROLE):
highest = "discordstaff";
break;
case interaction.member.roles.cache.has(process.env.PATREON_ROLE):
highest = "patreon";
break;
case interaction.member.roles.cache.has(process.env.BOOSTER_ROLE):
highest = "booster";
break;
}
if(highest != user.USRROLE){
interaction.deferReply()
collection.updateOne(
{ DISCORDID: `${user.DISCORDID}` }, // Query parameter
{ $set: { // Replacement document
USRROLE: `${highest}`
}},
{ upsert: true } // Options
)
console.log(`updated user in database to highest role`)
const rcon = new Rcon({
host: `${process.env.MCHOST}`,
port: `${process.env.RCON_PORT}`,
password: `${process.env.RCONPASS}`,
});
let connected = true
let error
try{
await rcon.connect();
} catch(e){
console.log(e)
connected = false
error = e
dig.query({
type: 'minecraft',
host: process.env.MCHOST,
port: process.env.MCHOSTPORT,
}).then((state) => {
//console.log(state);
//console.log(state.players)
serverofflinefor = 0;
}).catch((error) => {
console.log("Server is offline" + error);
serverofflinefor += 10
channelSend(`Server offline for ~${serverofflinefor}`)
if(serverofflinefor > 60){
const mc = spawn("screen", ['-dmS', 'minecraft', '/bin/bash', `${process.env.SERSTARTLOC}`])
}
});
runevery30seconds(++i)
}, 10000)
let clear = await rcon.send(`lp user ${mcusr} group remove ${user.USRROLE}`)
let res = await rcon.send(`lp user ${user.MCUSR} group add ${highest}`);
}
})
})
}
}
};

View file

@ -1,8 +1,6 @@
console.time('Time to online');
require("dotenv").config();
const Discord = require("discord.js");
const { dbclient } = require("./mongo");
const client = new Discord.Client({ intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMembers], partials: [Discord.Partials.Channel] });
@ -10,11 +8,6 @@ require('console-stamp')(console, {
format: ':date(dd mmmm yyyy HH:MM:ss) :label'
});
client.setMaxListeners(0);
client.commands = new Discord.Collection();
client.events = new Discord.Collection();
@ -22,10 +15,7 @@ client.events = new Discord.Collection();
require(`./handlers/${handler}`)(client, Discord);
});
process.on('SIGINT', (code) => {
dbclient.close()
console.log("DB closed Terminating")
process.on('SIGINT', (code) => {
process.exit()
});

View file

@ -1,6 +1,6 @@
module.exports = {
apps: [{
name: "NTCMCbot",
name: "MCbot",
script: "./index.js",
watch: true,
ignore_watch: ["./res/temp"],

View file

@ -1,9 +0,0 @@
const path = require('path');
const { MongoClient, ServerApiVersion } = require('mongodb');
// Remove dep warning
const dbclient = new MongoClient(process.env.DB_PATH, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 })
dbclient.connect()
// const dbTwo = mongoose.createConnection(process.env.DB_PATH2, { useNewUrlParser: true, useUnifiedTopology: true });
module.exports = { dbclient }

7922
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,7 @@
{
"name": "4dc",
"name": "mcbot",
"version": "1.0.0",
"description": "",
"description": "Boilerplate taken from 4DC bot, with some added features for a minecraft bot basically only used it for its layout barebones functionality",
"main": "index.js",
"scripts": {
"lint": "eslint --ignore-path .gitignore .",
@ -19,16 +19,21 @@
"discord-api-types": "^0.20.2",
"discord.js": "^14.7.0",
"dotenv": "^9.0.2",
"gamedig": "^4.3.0",
"gifencoder": "^2.0.1",
"glob": "^7.2.0",
"mongoose": "^6.3.4",
"nodemon": "^2.0.19",
"os": "^0.1.2",
"pm2": "^5.1.2",
"rcon-client": "^4.2.3",
"rss-parser": "^3.12.0",
"sharp": "^0.31.3",
"uuid": "^8.3.2",
"winston": "^3.8.2"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/Expand-sys/mcbot.git"
},
"bugs": {
"url": "https://github.com/Expand-sys/mcbot/issues"
},
"homepage": "https://github.com/Expand-sys/mcbot#readme"
}

View file

@ -1,6 +1,5 @@
const { CommandInteraction } = require('discord.js');
const attachmentMap = new Map();
const winston = require('winston');
const { client } = require("../index")
/**