-
Notifications
You must be signed in to change notification settings - Fork 4
Ingame Verification with Discord Bot #16
base: main
Are you sure you want to change the base?
Changes from all commits
a7f214b
ca3f25d
8b8f071
1ae139d
fdf593f
80f4363
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}` | ||
|
Comment on lines
+12
to
+16
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of these could be constants except for pendingData but I will talk about a potential solution to that. Additionally instead of making more separate variables you could just do
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will probably just change this and... |
||
|
|
||
| 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] | ||
ATXLtheAxolotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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" }) | ||
|
Comment on lines
+43
to
+46
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if you were to make
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... And this |
||
| } else { | ||
| const embed = new EmbedBuilder() | ||
| embed.setColor(Colors.Red) | ||
| embed.setTitle("This code doesnt exist.") | ||
| interaction.reply({ | ||
| embeds: [embed], | ||
| ephemeral: true | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
|
Comment on lines
+1
to
+12
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another not so necessary but for readability and good practice, maybe add a new line in-between the imports, the |
||
| 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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable isn't being reassigned maybe it could be a const
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No |
||
| const embed = new EmbedBuilder() | ||
| switch (type.toLowerCase()){ | ||
| case "minecraft":{ | ||
| let discord = getKeyByValue(usersData,user) | ||
| if (discord == undefined){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be compacted to |
||
| 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 | ||
| } | ||
|
Comment on lines
+30
to
+45
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't a necessary change but to help out with readability for future revisions you can remove the {} in switches and it would still be valid JavaScript |
||
| } | ||
| interaction.reply({ | ||
| embeds: [embed], | ||
| ephemeral: true | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { DiscordClient } from '../Utils/DiscordClient'; | ||
| import { makeid } from "../Utils/Hashing" | ||
| import { whisperChat } from "../Utils/IngameChat" | ||
| import fs from 'fs'; | ||
| export function handleIngameCommands(client: DiscordClient) { | ||
| const { bot } = client; | ||
| bot.on("player_chat", data =>{ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There doesn't appear to be a space between |
||
| var message = String(data.plainMessage); | ||
| if (message.includes("?verify")){ | ||
| var username = JSON.parse(data.networkName).text | ||
| var code = makeid(10) | ||
|
Comment on lines
+8
to
+11
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These variables won't be reassigned, they can be constants. |
||
| whisperChat(bot,"Your verification code is " + code + ". Use the /verify command in the discord to complete the verification.","Techfox") | ||
| verification(username,code) | ||
| } | ||
|
|
||
| }) | ||
| } | ||
|
|
||
|
|
||
| function verification(username:string,hash:string){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no spacing in the function parameters and the |
||
| let datajson = fs.readFileSync(__dirname + "/../Verification/pendingVerification.json","utf-8"); | ||
| let data = JSON.parse(datajson); | ||
| data[hash] = username | ||
| data = JSON.stringify(data) | ||
| fs.writeFileSync(__dirname + "/../Verification/pendingVerification.json",data, { encoding: "utf-8" }) | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
Comment on lines
+27
to
+29
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a few unnecessary newlines here. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ export type Config = { | |
| }, | ||
| roles: { | ||
| rwx: string, | ||
| rw: string | ||
| rw: string, | ||
| } | ||
| }, | ||
| "in-game-bot": { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const { createHash } = require('crypto'); | ||
|
|
||
| export function hash(string) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No type here either |
||
| return createHash('sha256').update(string).digest('hex'); | ||
| } | ||
|
|
||
| export function makeid(length) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type isn't defined, from what I can tell it is meant to be a |
||
| 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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new line in-between pls