From 592f770a35465e5bd3889700a4f277b6631e9e4d Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 13 Feb 2021 19:20:23 -0500 Subject: [PATCH] Split Phone into it's own manager --- commands/phone/admin-phone.js | 2 +- commands/phone/hang-up.js | 2 +- commands/phone/phone.js | 10 +++++----- structures/Client.js | 16 ++-------------- structures/phone/PhoneCall.js | 2 +- structures/phone/PhoneManager.js | 23 +++++++++++++++++++++++ 6 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 structures/phone/PhoneManager.js diff --git a/commands/phone/admin-phone.js b/commands/phone/admin-phone.js index 4f09a8aa..7236944a 100644 --- a/commands/phone/admin-phone.js +++ b/commands/phone/admin-phone.js @@ -23,7 +23,7 @@ module.exports = class AdminPhoneCommand extends Command { } async run(msg, { channelID }) { - if (this.client.inPhoneCall(msg.channel)) return msg.say('This channel is already in a phone call.'); + if (this.client.phone.inCall(msg.channel)) return msg.say('This channel is already in a phone call.'); const channel = this.client.channels.cache.get(channelID); if (!channel || !channel.guild) return msg.reply('This channel does not exist.'); try { diff --git a/commands/phone/hang-up.js b/commands/phone/hang-up.js index 52dd8c06..9d51a0fd 100644 --- a/commands/phone/hang-up.js +++ b/commands/phone/hang-up.js @@ -21,7 +21,7 @@ module.exports = class HangUpCommand extends Command { return msg.reply('☎️ You cannot hang up in an admin call.'); } const otherChannel = call.origin.id === msg.channel.id ? call.recipient : call.origin; - if (this.client.isBlockedFromPhone(msg.channel, otherChannel, msg.author)) { + if (this.client.phone.isBlocked(msg.channel, otherChannel, msg.author)) { return msg.reply('☎️ You are blocked from hanging up this phone call.'); } const nonQuitter = msg.channel.id === call.origin.id ? call.recipient : call.origin; diff --git a/commands/phone/phone.js b/commands/phone/phone.js index e402cf08..69709ce9 100644 --- a/commands/phone/phone.js +++ b/commands/phone/phone.js @@ -40,16 +40,16 @@ module.exports = class PhoneCommand extends Command { if (channelID !== 'count' && (msg.guild && (!msg.channel.topic || !msg.channel.topic.includes('')))) { return msg.say('You can only start a call in a channel with `` in the topic.'); } - if (channelID !== 'count' && this.client.inPhoneCall(msg.channel)) { + if (channelID !== 'count' && this.client.phone.inCall(msg.channel)) { return msg.say('This channel is already in a phone call.'); } const channels = this.client.channels.cache.filter(channel => channel.guild && channel.topic && channel.topic.includes('') && !channel.topic.includes('') - && !this.client.isBlockedFromPhone(msg.channel, channel, msg.author) + && !this.client.phone.isBlocked(msg.channel, channel, msg.author) && (msg.guild ? !msg.guild.channels.cache.has(channel.id) : true) - && (channelID ? true : !this.client.inPhoneCall(channel))); + && (channelID ? true : !this.client.phone.inCall(channel))); if (!channels.size) return msg.reply('No channels currently allow phone calls...'); let channel; if (channelID) { @@ -61,8 +61,8 @@ module.exports = class PhoneCommand extends Command { if (!channel.topic || !channel.topic.includes('')) { return msg.reply('That channel does not allow phone calls.'); } - if (this.client.inPhoneCall(channel)) return msg.reply('That channel is already in a call.'); - if (this.client.isBlockedFromPhone(msg.channel, channel, msg.author)) { + if (this.client.phone.inCall(channel)) return msg.reply('That channel is already in a call.'); + if (this.client.phone.isBlocked(msg.channel, channel, msg.author)) { return msg.reply('That channel has blocked this channel from calling them.'); } } else { diff --git a/structures/Client.js b/structures/Client.js index ed03eef5..426f7899 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -5,6 +5,7 @@ const winston = require('winston'); const fs = require('fs'); const path = require('path'); const Redis = require('./Redis'); +const PhoneManager = require('./phone/PhoneManager'); const TimerManager = require('./remind/TimerManager'); const PokemonStore = require('./pokemon/PokemonStore'); const MemePosterClient = require('./MemePoster'); @@ -45,24 +46,11 @@ module.exports = class XiaoClient extends CommandoClient { }) : null; this.games = new Collection(); this.dispatchers = new Map(); - this.phone = new Collection(); + this.phone = new PhoneManager(this); this.activities = activities; this.leaveMessages = leaveMsgs; } - inPhoneCall(channel) { - return this.phone.some(call => call.origin.id === channel.id || call.recipient.id === channel.id); - } - - isBlockedFromPhone(origin, recipient, caller) { - return (recipient.guild && recipient.topic.includes(``)) - || (recipient.guild && recipient.topic.includes(``)) - || (origin.guild && recipient.guild && recipient.topic.includes(``)) - || (origin.guild && origin.topic.includes(``)) - || (origin.guild && recipient.guild && origin.topic.includes(``)) - || (origin.guild && origin.topic.includes(``)); - } - importBlacklist() { const read = fs.readFileSync(path.join(__dirname, '..', 'blacklist.json'), { encoding: 'utf8' }); const file = JSON.parse(read); diff --git a/structures/phone/PhoneCall.js b/structures/phone/PhoneCall.js index d8a2989b..21482785 100644 --- a/structures/phone/PhoneCall.js +++ b/structures/phone/PhoneCall.js @@ -118,7 +118,7 @@ module.exports = class PhoneCall { if (this.cooldown.has(msg.author.id) && !this.client.isOwner(msg.author)) { return otherChannel.send(`☎️ ${msg.author}, slow down! You're sending messages too fast!`); } - if (this.client.isBlockedFromPhone(otherChannel, channel, msg.author)) { + if (this.client.phone.isBlocked(otherChannel, channel, msg.author)) { return otherChannel.send(`☎️ ${msg.author}, you are blocked from sending messages to this channel!`); } this.setTimeout(); diff --git a/structures/phone/PhoneManager.js b/structures/phone/PhoneManager.js new file mode 100644 index 00000000..952cdc4f --- /dev/null +++ b/structures/phone/PhoneManager.js @@ -0,0 +1,23 @@ +const Collection = require('@discordjs/collection'); + +module.exports = class PhoneManager extends Collection { + constructor(client, options) { + Object.defineProperty(this, 'client', { value: client }); + + super(options); + } + + inCall(channel) { + return this.some(call => call.origin.id === channel.id || call.recipient.id === channel.id); + } + + isBlocked(origin, recipient, caller) { + return (recipient.guild && recipient.topic.includes(``)) + || (recipient.guild && recipient.topic.includes(``)) + || (origin.guild && recipient.guild && recipient.topic.includes(``)) + || (origin.guild && origin.topic.includes(``)) + || (origin.guild && recipient.guild && origin.topic.includes(``)) + || (origin.guild && origin.topic.includes(``)); + } + +};