Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckySoLucky authored Aug 22, 2022
2 parents 72ec47b + 9b10f4e commit 44bc022
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
30 changes: 22 additions & 8 deletions src/discord/handlers/MessageHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const config = require('../../../config.json')
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
const { generateID } = require('../../contracts/helperFunctions')

class MessageHandler {
constructor(discord, command) {
Expand All @@ -7,8 +9,11 @@ class MessageHandler {
}

async onMessage(message) {
if (message.author.id == config.discord.clientID) return
if (!this.shouldBroadcastMessage(message)) return

const [attachments] = message.attachments.values();
const url = attachments ? attachments.url : null;
const content = this.stripDiscordContent(message.content).trim()
if (content.length == 0) return

Expand All @@ -19,29 +24,38 @@ class MessageHandler {
message: this.stripDiscordContent(message.content),
replyingTo: await this.fetchReply(message),
})
if (!url) return
await delay (100)
this.discord.broadcastMessage({
member: message.member.user,
channel: message.channel.id,
username: message.member.displayName,
message: `${url} - ${generateID(config.minecraft.messageRepeatBypassLength)}`,
replyingTo: await this.fetchReply(message),
})
}

async fetchReply(message) {
try {
if (!message.reference) return null

const [attachment] = (await (client.channels.cache.get(message.reference.channelId)).messages.fetch(message.reference.messageId)).attachments.values() ?? null
const reference = await message.channel.messages.fetch(message.reference.messageId) ?? null
if (config.discord.messageMode == "minecraft") {
const [attachment] = (await (client.channels.cache.get(message.reference.channelId)).messages.fetch(message.reference.messageId)).attachments.values()
return attachment.name.slice(0, -4)
return attachment?.name.slice(0, -4) ?? reference.member.displayName ?? reference.author.username
} else {
const reference = await message.channel.messages.fetch(message.reference.messageId)
return reference.member ? reference.member.displayName : reference.author.username
}


} catch (error) {
return null
}
}

stripDiscordContent(message) {
return message.replace(/<[@|#|!|&]{1,2}(\d+){16,}>/g, '\n').replace(/<:\w+:(\d+){16,}>/g, '\n').replace(/[^\p{L}\p{N}\p{P}\p{Z}]/gu, '\n').split('\n').map(part => {part = part.trim(); return part.length == 0 ? '' : part + ' '}).join('')
}
return message.split('\n').map(part => {
part = part.trim();
return part.length == 0 ? '' : part + ' '
}).join('')
}

shouldBroadcastMessage(message) {
return !message.author.bot && message.channel.id == config.discord.officerChannel && message.content && message.content.length > 0 || !message.author.bot && message.channel.id == config.discord.guildChatChannel && message.content && message.content.length > 0
Expand Down
20 changes: 10 additions & 10 deletions src/minecraft/handlers/ChatHandler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const { replaceAllRanks, toFixed, addCommas } = require('../../contracts/helperFunctions')
const { getSenitherWeightUsername } = require('../../contracts/weight/senitherWeight')
const { getLilyWeightUsername } = require('../../contracts/weight/lilyWeight')
const { getUsername, getUUID } = require('../../contracts/API/PlayerDBAPI')
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
let guildInfo = [], guildRanks = [], members = [], guildTop = []
const hypixel = require('../../contracts/API/HypixelRebornAPI')
const { getUUID } = require('../../contracts/API/PlayerDBAPI')
const EventHandler = require('../../contracts/EventHandler')
const messages = require('../../../messages.json')
const linked = require('../../../data/minecraftLinked.json')
const config = require('../../../config.json')
const Logger = require('../../Logger')
const fs = require('fs')
Expand Down Expand Up @@ -174,6 +173,8 @@ class StateHandler extends EventHandler {

if (this.isJoinMessage(message)) {
let user = message.replace(/\[(.*?)\]/g, '').trim().split(/ +/g)[0]
await delay(1000)
bot.chat('/gc Welcome to the guild! Make sure to join our discord /g discord! To view my commands run !help, Have a nice day :D')
return [this.minecraft.broadcastHeadedEmbed({
message: `${user} ${messages.joinMessage}`,
title: `Member Joined`,
Expand All @@ -187,17 +188,16 @@ class StateHandler extends EventHandler {
color: '47F049',
channel: 'Guild'
})]
await delay(1500)
bot.chat('/gc Welcome to the guild! Make sure to join our discord /g discord! To view my commands run !help, Have a nice day :D')
}

if (this.isLeaveMessage(message)) {
const linked = require('../../../data/minecraftLinked.json')
let user = message.replace(/\[(.*?)\]/g, '').trim().split(/ +/g)[0]
const uuid = await getUUID(user)
if (linked?.[uuid]?.data[0]) {
const member = await guild.members.fetch(linked?.[uuid]?.data[0])
member.roles.remove(config.discord.guildMemberRole)
}
if (linked?.[uuid]?.data[0]) {
const member = await guild.members.fetch(linked?.[uuid]?.data[0])
member.roles.remove(config.discord.guildMemberRole)
}

return [this.minecraft.broadcastHeadedEmbed({
message: `${user} ${messages.leaveMessage}`,
Expand Down Expand Up @@ -534,7 +534,7 @@ class StateHandler extends EventHandler {
this.minecraft.broadcastMessage({
fullMessage: colouredMessage,
username: username,
message: playerMessage,
message: playerMessage.replaceAll('@everyone', '').replaceAll('@here', ''),
guildRank: guildRank,
chat: chatType,
})
Expand Down Expand Up @@ -692,4 +692,4 @@ class StateHandler extends EventHandler {
}
}

module.exports = StateHandler
module.exports = StateHandler

0 comments on commit 44bc022

Please sign in to comment.