From 869231d2bd429b135bd2528fc13a7ba0dfaacd06 Mon Sep 17 00:00:00 2001 From: Tolfx Date: Wed, 18 Jan 2023 11:42:10 +0100 Subject: [PATCH] feat: created message command (#1475) --- .../Commands/commands/information/Message.ts | 189 ++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 src/classes/Commands/commands/information/Message.ts diff --git a/src/classes/Commands/commands/information/Message.ts b/src/classes/Commands/commands/information/Message.ts new file mode 100644 index 000000000..61ef52e3a --- /dev/null +++ b/src/classes/Commands/commands/information/Message.ts @@ -0,0 +1,189 @@ +import SteamID from 'steamid'; +import CommandHandler, { ICommand } from '../../CommandHandler'; +import Bot from '../../../Bot'; +import IPricer from '../../../IPricer'; +import CommandParser from '../../../CommandParser'; +import log from '../../../../lib/logger'; +import sendAdminMessage from '../../../../lib/DiscordWebhook/sendAdminMessage'; +import generateLinks from '../../../../lib/tools/links'; +import { timeNow } from '../../../../lib/tools/time'; +import sendPartnerMessage from '../../../../lib/DiscordWebhook/sendPartnerMessage'; + +export default class MessageCommand implements ICommand { + name = 'message'; + + description = 'Send a message to the owner of the bot šŸ’¬'; + + allowInvalidTypes = false; + + constructor( + public readonly bot: Bot, + public readonly pricer: IPricer, + public readonly commandHandler: CommandHandler + ) { + this.bot = bot; + this.pricer = pricer; + this.commandHandler = commandHandler; + } + + execute = (steamID: SteamID, message: string) => { + const prefix = this.bot.getPrefix(steamID); + + const isAdmin = this.bot.isAdmin(steamID); + const optComm = this.bot.options.commands.message; + const custom = optComm.customReply; + + if (!optComm.enable) { + if (isAdmin) { + this.bot.sendMessage( + steamID, + 'āŒ The message command is disabled. Enable it by sending `!config commands.message.enable=true`.' + ); + } else { + this.bot.sendMessage( + steamID, + custom.disabled ? custom.disabled : 'āŒ The owner has disabled messages.' + ); + } + return; + } + + const senderDetails = this.bot.friends.getFriend(steamID); + const optDW = this.bot.options.discordWebhook.messages; + + if (isAdmin) { + const steamIdAndMessage = CommandParser.removeCommand(message); + const parts = steamIdAndMessage.split(' '); + let recipientSteamID: SteamID; + + try { + recipientSteamID = new SteamID(parts[0]); + } catch (err) { + log.error('Wrong input (SteamID): ', err); + return this.bot.sendMessage( + steamID, + `āŒ Your syntax is wrong or the SteamID is incorrectly formatted. Here's an example: "${prefix}message 76561198120070906 Hi"` + + "\n\nHow to get the targeted user's SteamID?" + + '\n1. Go to his/her profile page.' + + '\n2. Go to https://steamrep.com/' + + '\n3. View this gif: https://user-images.githubusercontent.com/47635037/96715154-be80b580-13d5-11eb-9bd5-39613f600f6d.gif' + ); + } + + const steamIDString = recipientSteamID.getSteamID64(); + + if (!recipientSteamID.isValid()) { + return this.bot.sendMessage( + steamID, + `āŒ "${steamIDString}" is not a valid SteamID.` + + "\n\nHow to get the targeted user's SteamID?" + + '\n1. Go to his/her profile page.' + + '\n2. Go to https://steamrep.com/' + + '\n3. View this gif: https://user-images.githubusercontent.com/47635037/96715154-be80b580-13d5-11eb-9bd5-39613f600f6d.gif' + ); + } else if (!this.bot.friends.isFriend(recipientSteamID)) { + return this.bot.sendMessage(steamID, `āŒ I am not friends with the user.`); + } + + const recipientDetails = this.bot.friends.getFriend(recipientSteamID); + const adminDetails = this.bot.friends.getFriend(steamID); + const reply = steamIdAndMessage.substring(steamIDString.length).trim(); + const isShowOwner = optComm.showOwnerName; + + // Send message to recipient + this.bot.sendMessage( + recipientSteamID, + custom.fromOwner + ? custom.fromOwner.replace(/%reply%/g, reply) + : `/quote šŸ’¬ Message from ${ + isShowOwner && adminDetails ? adminDetails.player_name : 'the owner' + }: ${reply}` + + '\n\nā” Hint: You can use the !message command to respond to the owner of this bot.' + + '\nExample: !message Hi Thanks!' + ); + + // Send a notification to the admin with message contents & details + if (optDW.enable && optDW.url !== '') { + sendAdminMessage( + recipientSteamID.toString(), + reply, + recipientDetails, + generateLinks(recipientSteamID.toString()), + timeNow(this.bot.options).time, + this.bot + ); + } else { + const customInitializer = this.bot.options.steamChat.customInitializer.message.toOtherAdmins; + this.bot.messageAdmins( + `${ + customInitializer ? customInitializer : '/quote' + } šŸ’¬ Message sent to #${recipientSteamID.toString()}${ + recipientDetails ? ` (${recipientDetails.player_name})` : '' + }: "${reply}". `, + [] + ); + } + + this.bot.sendMessage(steamID, custom.success ? custom.success : 'āœ… Your message has been sent.'); + + // Send message to all other admins that an admin replied + return this.bot.messageAdmins( + `${ + senderDetails ? `${senderDetails.player_name} (${steamID.toString()})` : steamID.toString() + } sent a message to ${ + recipientDetails + ? recipientDetails.player_name + ` (${recipientSteamID.toString()})` + : recipientSteamID.toString() + } with "${reply}".`, + [steamID] + ); + } else { + const admins = this.bot.getAdmins; + if (!admins || admins.length === 0) { + // Just default to same message as if it was disabled + return this.bot.sendMessage( + steamID, + custom.disabled ? custom.disabled : 'āŒ The owner has disabled messages.' + ); + } + + const msg = message.substring(8).trim(); // "message" + if (!msg) { + return this.bot.sendMessage( + steamID, + custom.wrongSyntax + ? custom.wrongSyntax + : `āŒ Please include a message. Here's an example: "${prefix}message Hi"` + ); + } + + const links = generateLinks(steamID.toString()); + if (optDW.enable && optDW.url !== '') { + sendPartnerMessage( + steamID.toString(), + msg, + senderDetails, + links, + timeNow(this.bot.options).time, + this.bot + ); + } else { + const customInitializer = this.bot.options.steamChat.customInitializer.message.onReceive; + this.bot.messageAdmins( + `${ + customInitializer ? customInitializer : '/quote' + } šŸ’¬ You've got a message from #${steamID.toString()}${ + senderDetails ? ` (${senderDetails.player_name})` : '' + }:` + + `"${msg}". ` + + `\nSteam: ${links.steam}` + + `\nBackpack.tf: ${links.bptf}` + + `\nSteamREP: ${links.steamrep}`, + [] + ); + } + + this.bot.sendMessage(steamID, custom.success ? custom.success : 'āœ… Your message has been sent.'); + } + }; +}