From a7f214bbc35cc6afb141603357763d37729633ff Mon Sep 17 00:00:00 2001 From: Bluefox Date: Fri, 9 Dec 2022 15:08:47 +0100 Subject: [PATCH 1/6] Ingame Commands 1 --- Minecraft/IngameCommands.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Minecraft/IngameCommands.ts diff --git a/Minecraft/IngameCommands.ts b/Minecraft/IngameCommands.ts new file mode 100644 index 0000000..d242c40 --- /dev/null +++ b/Minecraft/IngameCommands.ts @@ -0,0 +1,31 @@ +import { DiscordClient } from '../Utils/DiscordClient'; +import { MinecraftClient } from '../Utils/MinecraftClient'; + +export function handleIngameCommands(client: DiscordClient) { + const { bot } = client; + bot.on("player_chat", data =>{ + var message = String(data.plainMessage); + if (message.includes("?seed")){ + writeChat(bot,"Test") + } + if (message.includes("?discord")){ + writeChat(bot,"Invite: https://discord.gg/paCcuuP964") + } + + + }) +} + +function writeChat(bot: MinecraftClient,message:String){ + bot.write('chat_message', { + message: message, + timestamp: BigInt(Date.now()), + salt: 0, + signature: Buffer.alloc(0), + signedPreview: false, + previousMessages: [], + lastMessage: null + }); + + +} \ No newline at end of file From ca3f25de1469a6811cde113674a651e2fd1d772a Mon Sep 17 00:00:00 2001 From: Bluefox Date: Sat, 17 Dec 2022 16:23:54 +0100 Subject: [PATCH 2/6] added Ingame Verification v1 --- Commands/general/verify.ts | 56 +++++++++++++++++++++++++++ Commands/general/whois.ts | 51 ++++++++++++++++++++++++ Minecraft/Bot.ts | 4 ++ Minecraft/IngameCommands.ts | 37 +++++++++++------- Utils/Hashing.ts | 15 +++++++ Utils/IngameChat.ts | 30 ++++++++++++++ Verification/doneVerification.json | 1 + Verification/pendingVerification.json | 1 + 8 files changed, 182 insertions(+), 13 deletions(-) create mode 100644 Commands/general/verify.ts create mode 100644 Commands/general/whois.ts create mode 100644 Utils/Hashing.ts create mode 100644 Utils/IngameChat.ts create mode 100644 Verification/doneVerification.json create mode 100644 Verification/pendingVerification.json diff --git a/Commands/general/verify.ts b/Commands/general/verify.ts new file mode 100644 index 0000000..aad551e --- /dev/null +++ b/Commands/general/verify.ts @@ -0,0 +1,56 @@ +import { SlashCommandBuilder, EmbedBuilder, ChatInputCommandInteraction, User, Colors } from 'discord.js'; +import { DiscordClient } from '../../Utils/DiscordClient'; +import fs from 'fs'; +import { whisperChat } from '../../Utils/IngameChat'; +export const data = new SlashCommandBuilder() + .setName('verify') + .setDescription('Yerify yourself with the ingame bot') + .addStringOption((o) => o.setName(`code`).setDescription(`Put the code you recieved here.`).setRequired(true)) + +export function execute(interaction: ChatInputCommandInteraction, client: DiscordClient) { + const code = interaction.options.getString("code") || "null" + let pending = fs.readFileSync(__dirname + "/../../Verification/pendingVerification.json","utf-8"); + let done = fs.readFileSync(__dirname + "/../../Verification/doneVerification.json","utf-8"); + var pendingData = JSON.parse(pending); + var doneData = JSON.parse(done) + var user = `${interaction.user.username}#${interaction.user.discriminator}` + + if (pendingData.hasOwnProperty(code)){ + const embed = new EmbedBuilder() + embed.setColor(Colors.Green) + embed.setTitle(`Ok. The account ${user} is now linked to ${pendingData[code]}`) + interaction.reply({ + embeds: [embed], + ephemeral: true + }) + if (interaction.guild === null){ + return + } + let verifiedRole =interaction.guild.roles.cache.find(r => r.id === client.config.guild.roles.verified) + if (verifiedRole === undefined){ + console.error("The verfied role doesnt exits or is not defined in the config") + return + } + const usr = interaction.guild?.members.cache.find(m => m.id === interaction.user.username); + usr?.roles.add(verifiedRole).catch(console.error) + + //Ingame Verification Message + const {bot} = client + whisperChat(bot,"Your Verification Process is done. You are now verified as " + user, pendingData[code]) + + doneData[user] = pendingData[code] + delete pendingData[code] + pendingData = JSON.stringify(pendingData) + doneData = JSON.stringify(doneData) + fs.writeFileSync(__dirname + "/../../Verification/pendingVerification.json",pendingData, { encoding: "utf-8" }) + fs.writeFileSync(__dirname + "/../../Verification/doneVerification.json",doneData, { encoding: "utf-8" }) + } else { + const embed = new EmbedBuilder() + embed.setColor(Colors.Red) + embed.setTitle("This code doesnt exist.") + interaction.reply({ + embeds: [embed], + ephemeral: true + }) + } + } \ No newline at end of file diff --git a/Commands/general/whois.ts b/Commands/general/whois.ts new file mode 100644 index 0000000..e62e379 --- /dev/null +++ b/Commands/general/whois.ts @@ -0,0 +1,51 @@ +import { SlashCommandBuilder, EmbedBuilder, ChatInputCommandInteraction, User, Colors } from 'discord.js'; +import { DiscordClient } from '../../Utils/DiscordClient'; +import fs from 'fs'; +function getKeyByValue(object, value) { + return Object.keys(object).find(key => object[key] === value); +} +export const data = new SlashCommandBuilder() + .setName('whois') + .setDescription('Lookup a user by their IGN or Discord Name') + .addStringOption((o) => o.setName(`type`).setDescription(`Lookup by minecraft or discord`).setRequired(true).addChoices({name:"Minecraft",value:"minecraft"},{name:"Discord",value:"discord"})) + .addStringOption((o) => o.setName(`user`).setDescription(`If you chose minecraft: IGN; if you chose discord: Username#Discriminator`).setRequired(true)) +export function execute(interaction: ChatInputCommandInteraction, client: DiscordClient) { + const type = interaction.options.getString("type") || "null" + const user = interaction.options.getString("user") || "null" + let users = fs.readFileSync(__dirname + "/../../Verification/doneVerification.json","utf-8"); + var usersData = JSON.parse(users) + const embed = new EmbedBuilder() + switch (type.toLowerCase()){ + case "minecraft":{ + let discord = getKeyByValue(usersData,user) + if (discord == undefined){ + embed.setColor(Colors.Red) + embed.setTitle(`The Minecraft Account ${user} has not been verified yet or doesnt exist`) + } else { + embed.setColor(Colors.Green) + embed.setTitle(`The Minecraft Account ${user} is linked to ${discord} on Discord.`) + } + break + } + case "discord":{ + if (usersData.hasOwnProperty(user)){ + let ingame = usersData[user] + embed.setColor(Colors.Green) + embed.setTitle(`The Account ${user} is linked to ${ingame} Ingame.`) + } else { + embed.setColor(Colors.Red) + embed.setTitle(`The Account ${user} has not been verified yet or doesnt exist`) + } + break + } + default:{ + embed.setColor(Colors.Red) + embed.setTitle("Please choose between the minecraft or the discord option when defining the type!") + break + } + } + interaction.reply({ + embeds: [embed], + ephemeral: true + }) +} \ No newline at end of file diff --git a/Minecraft/Bot.ts b/Minecraft/Bot.ts index 52f32da..608b72e 100644 --- a/Minecraft/Bot.ts +++ b/Minecraft/Bot.ts @@ -3,6 +3,8 @@ import { DiscordClient } from '../Utils/DiscordClient'; import { handleChat } from './Chat'; import { PlayerManager } from './PlayerManager'; import { ProfileCache } from './ProfileCache'; +import { handleIngameCommands } from './IngameCommands'; +import { registerTwitchAPI } from "../Twitch/TwitchAPI" export function handleMinecraft(client: DiscordClient) { if (!client.config['in-game-bot'].enabled) return; // Returns if there is no bot config @@ -37,6 +39,8 @@ export function handleMinecraft(client: DiscordClient) { client.bot.on('connect', () => { console.log(`Connected to Server...`); handleChat(client); + handleIngameCommands(client); + //registerTwitchAPI(client); }); // Sends log message when the bot connects to the server } diff --git a/Minecraft/IngameCommands.ts b/Minecraft/IngameCommands.ts index d242c40..58210c2 100644 --- a/Minecraft/IngameCommands.ts +++ b/Minecraft/IngameCommands.ts @@ -1,6 +1,8 @@ import { DiscordClient } from '../Utils/DiscordClient'; import { MinecraftClient } from '../Utils/MinecraftClient'; - +import {hash, makeid} from "../Utils/Hashing" +import {writeChat, whisperChat} from "../Utils/IngameChat" +import fs from 'fs'; export function handleIngameCommands(client: DiscordClient) { const { bot } = client; bot.on("player_chat", data =>{ @@ -11,21 +13,30 @@ export function handleIngameCommands(client: DiscordClient) { if (message.includes("?discord")){ writeChat(bot,"Invite: https://discord.gg/paCcuuP964") } - + if (message.includes("?verify")){ + var username = JSON.parse(data.networkName).text + //console.log(JSON.parse(data.networkName).text) + //var code = hash(username).substring(0,10) + var code = makeid(10) + whisperChat(bot,"Your verification code is " + code + ". Use the /verify command in the discord to complete the verification.","Techfox") + //writeChat(bot,"Your verification code is " + code + ". Use the /verify command in the discord to complete the verification.") + verification(username,code) + } }) } -function writeChat(bot: MinecraftClient,message:String){ - bot.write('chat_message', { - message: message, - timestamp: BigInt(Date.now()), - salt: 0, - signature: Buffer.alloc(0), - signedPreview: false, - previousMessages: [], - lastMessage: null - }); + +function verification(username:string,hash:string){ + let datajson = fs.readFileSync(__dirname + "/../Verification/pendingVerification.json","utf-8"); + let data = JSON.parse(datajson); + //var j = {hash:username} + data[hash] = username + //dataj.push(j) + data = JSON.stringify(data) + fs.writeFileSync(__dirname + "/../Verification/pendingVerification.json",data, { encoding: "utf-8" }) + //fs.writeFileSync("/../Verification/pendingVerification.json",data, { encoding: "utf-8" }) +} + -} \ No newline at end of file diff --git a/Utils/Hashing.ts b/Utils/Hashing.ts new file mode 100644 index 0000000..ec2efe3 --- /dev/null +++ b/Utils/Hashing.ts @@ -0,0 +1,15 @@ +const { createHash } = require('crypto'); + +export function hash(string) { + return createHash('sha256').update(string).digest('hex'); +} + +export function makeid(length) { + var result = ''; + var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + var charactersLength = characters.length; + for ( var i = 0; i < length; i++ ) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +} \ No newline at end of file diff --git a/Utils/IngameChat.ts b/Utils/IngameChat.ts new file mode 100644 index 0000000..37098ab --- /dev/null +++ b/Utils/IngameChat.ts @@ -0,0 +1,30 @@ +import { MinecraftClient } from "./MinecraftClient"; + +export function writeChat(bot: MinecraftClient,message:String){ + bot.write('chat_message', { + message: message, + timestamp: BigInt(Date.now()), + salt: 0, + signature: Buffer.alloc(0), + signedPreview: false, + previousMessages: [], + lastMessage: null + }); + + +} +export function whisperChat(bot: MinecraftClient,message:String,user:String){ + const command = `tell ${user} ${message}` + bot.write('chat_command', { + command: command, + timestamp: BigInt(Date.now()), + salt: 0, + argumentSignatures: [ + {argumentName:"targets",signature:Buffer.alloc(0)}, + {argumentName:"message",signature:Buffer.alloc(0)} + ], + signedPreview: false, + previousMessages: [], + lastMessage: null + }); +} \ No newline at end of file diff --git a/Verification/doneVerification.json b/Verification/doneVerification.json new file mode 100644 index 0000000..9872c3d --- /dev/null +++ b/Verification/doneVerification.json @@ -0,0 +1 @@ +{"Bluefox#3699":"Techfox"} \ No newline at end of file diff --git a/Verification/pendingVerification.json b/Verification/pendingVerification.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/Verification/pendingVerification.json @@ -0,0 +1 @@ +{} \ No newline at end of file From 8b8f071b83d3e91ee701b0483efd6c364cf1ab9a Mon Sep 17 00:00:00 2001 From: Bluefox Date: Sat, 17 Dec 2022 17:23:43 +0100 Subject: [PATCH 3/6] added Ingame Verification v1 --- Verification/doneVerification.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Verification/doneVerification.json b/Verification/doneVerification.json index 9872c3d..9e26dfe 100644 --- a/Verification/doneVerification.json +++ b/Verification/doneVerification.json @@ -1 +1 @@ -{"Bluefox#3699":"Techfox"} \ No newline at end of file +{} \ No newline at end of file From 1ae139d4b93fba3764034410f5f9c590a1908b8a Mon Sep 17 00:00:00 2001 From: Bluefox Date: Sat, 17 Dec 2022 17:25:05 +0100 Subject: [PATCH 4/6] added Ingame Verification v1 --- Minecraft/IngameCommands.ts | 10 ---------- Utils/Config.d.ts | 3 ++- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/Minecraft/IngameCommands.ts b/Minecraft/IngameCommands.ts index 58210c2..41bb4b6 100644 --- a/Minecraft/IngameCommands.ts +++ b/Minecraft/IngameCommands.ts @@ -1,5 +1,4 @@ import { DiscordClient } from '../Utils/DiscordClient'; -import { MinecraftClient } from '../Utils/MinecraftClient'; import {hash, makeid} from "../Utils/Hashing" import {writeChat, whisperChat} from "../Utils/IngameChat" import fs from 'fs'; @@ -7,19 +6,10 @@ export function handleIngameCommands(client: DiscordClient) { const { bot } = client; bot.on("player_chat", data =>{ var message = String(data.plainMessage); - if (message.includes("?seed")){ - writeChat(bot,"Test") - } - if (message.includes("?discord")){ - writeChat(bot,"Invite: https://discord.gg/paCcuuP964") - } if (message.includes("?verify")){ var username = JSON.parse(data.networkName).text - //console.log(JSON.parse(data.networkName).text) - //var code = hash(username).substring(0,10) var code = makeid(10) whisperChat(bot,"Your verification code is " + code + ". Use the /verify command in the discord to complete the verification.","Techfox") - //writeChat(bot,"Your verification code is " + code + ". Use the /verify command in the discord to complete the verification.") verification(username,code) } diff --git a/Utils/Config.d.ts b/Utils/Config.d.ts index d8e8bb0..8143752 100644 --- a/Utils/Config.d.ts +++ b/Utils/Config.d.ts @@ -17,7 +17,8 @@ export type Config = { }, roles: { rwx: string, - rw: string + rw: string, + verified: string } }, "in-game-bot": { From fdf593f16ebd56d3c7b5f63e1a7a87877036357b Mon Sep 17 00:00:00 2001 From: Bluefox Date: Sat, 17 Dec 2022 17:28:24 +0100 Subject: [PATCH 5/6] added Ingame Verification v1 --- Minecraft/Bot.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/Minecraft/Bot.ts b/Minecraft/Bot.ts index 608b72e..2388d26 100644 --- a/Minecraft/Bot.ts +++ b/Minecraft/Bot.ts @@ -4,7 +4,6 @@ import { handleChat } from './Chat'; import { PlayerManager } from './PlayerManager'; import { ProfileCache } from './ProfileCache'; import { handleIngameCommands } from './IngameCommands'; -import { registerTwitchAPI } from "../Twitch/TwitchAPI" export function handleMinecraft(client: DiscordClient) { if (!client.config['in-game-bot'].enabled) return; // Returns if there is no bot config @@ -40,7 +39,6 @@ export function handleMinecraft(client: DiscordClient) { console.log(`Connected to Server...`); handleChat(client); handleIngameCommands(client); - //registerTwitchAPI(client); }); // Sends log message when the bot connects to the server } From 80f43636fc694d877d333e98bbdb04d636016c55 Mon Sep 17 00:00:00 2001 From: Bluefox Date: Sat, 17 Dec 2022 17:30:45 +0100 Subject: [PATCH 6/6] added Ingame Verification v1 --- Commands/general/verify.ts | 14 +++++++------- Minecraft/IngameCommands.ts | 7 ++----- Utils/Config.d.ts | 1 - 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Commands/general/verify.ts b/Commands/general/verify.ts index aad551e..a16deab 100644 --- a/Commands/general/verify.ts +++ b/Commands/general/verify.ts @@ -26,13 +26,13 @@ export function execute(interaction: ChatInputCommandInteraction, client: Discor if (interaction.guild === null){ return } - let verifiedRole =interaction.guild.roles.cache.find(r => r.id === client.config.guild.roles.verified) - if (verifiedRole === undefined){ - console.error("The verfied role doesnt exits or is not defined in the config") - return - } - const usr = interaction.guild?.members.cache.find(m => m.id === interaction.user.username); - usr?.roles.add(verifiedRole).catch(console.error) + // let verifiedRole =interaction.guild.roles.cache.find(r => r.id === client.config.guild.roles.verified) + // if (verifiedRole === undefined){ + // console.error("The verfied role doesnt exits or is not defined in the config") + // return + // } + // const usr = interaction.guild?.members.cache.find(m => m.id === interaction.user.username); + // usr?.roles.add(verifiedRole).catch(console.error) //Ingame Verification Message const {bot} = client diff --git a/Minecraft/IngameCommands.ts b/Minecraft/IngameCommands.ts index 41bb4b6..d332903 100644 --- a/Minecraft/IngameCommands.ts +++ b/Minecraft/IngameCommands.ts @@ -1,6 +1,6 @@ import { DiscordClient } from '../Utils/DiscordClient'; -import {hash, makeid} from "../Utils/Hashing" -import {writeChat, whisperChat} from "../Utils/IngameChat" +import { makeid } from "../Utils/Hashing" +import { whisperChat } from "../Utils/IngameChat" import fs from 'fs'; export function handleIngameCommands(client: DiscordClient) { const { bot } = client; @@ -20,12 +20,9 @@ export function handleIngameCommands(client: DiscordClient) { function verification(username:string,hash:string){ let datajson = fs.readFileSync(__dirname + "/../Verification/pendingVerification.json","utf-8"); let data = JSON.parse(datajson); - //var j = {hash:username} data[hash] = username - //dataj.push(j) data = JSON.stringify(data) fs.writeFileSync(__dirname + "/../Verification/pendingVerification.json",data, { encoding: "utf-8" }) - //fs.writeFileSync("/../Verification/pendingVerification.json",data, { encoding: "utf-8" }) } diff --git a/Utils/Config.d.ts b/Utils/Config.d.ts index 8143752..16a21a8 100644 --- a/Utils/Config.d.ts +++ b/Utils/Config.d.ts @@ -18,7 +18,6 @@ export type Config = { roles: { rwx: string, rw: string, - verified: string } }, "in-game-bot": {