Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- registerPlugin({
- name: 'unusedChannelDeleter',
- version: '0.2.2',
- backends: ['ts3'],
- description: 'Deletes Channels, unused for a specific amount of time.',
- author: 'Everlike <everlikezz@gmail.com>',
- vars: [
- //0 = all channels | 1 = specific channels | 2 = all channels with the same parentchannel
- {
- name: 'listenType',
- title: 'Channels to check',
- type: 'select',
- options: [
- "All Channels",
- "Specific Channels",
- "All subchannels with the same parentchannel"
- ]
- },
- //channels to ignore
- {
- name: 'ignoreChannels',
- title: 'Ignore channels',
- type: 'array',
- vars: [
- {
- name: 'channel',
- title: 'channel',
- type: 'channel'
- }
- ],
- conditions: [
- {field: 'listenType', value: 0,}
- ]
- },
- //choose channels to check, only shown when listentype is 1 (specific channels)
- {
- name: 'chooseChannels',
- title: 'choose Channels',
- type: 'array',
- vars: [
- {
- name: 'channel',
- title: 'channel',
- type: 'channel'
- }
- ],
- conditions: [
- { field: 'listenType', value: 1}
- ]
- },
- //parentchannel, only shown when listentype is 2 (all channels with the same parentchannel)
- {
- name: 'parentChannels',
- title: 'Parent Channel',
- type: 'array',
- vars: [
- {
- name: 'channel',
- title: 'channel',
- type: 'channel'
- }
- ],
- conditions: [
- { field: 'listenType', value: 2}
- ]
- },
- //time, a channel can be unused until it will be deleted.
- {
- name: 'timeTillDeletion',
- title: 'Time, how long a channel can stay unused till it will be deleted. (in minutes)',
- type: 'number',
- placeholder: 20160
- },
- //Descriptionsettings
- //check if user want to set a description
- {
- name: 'enableDescription',
- title: 'Do you want to set a list of channels and their time until deletion as description of a channel?',
- type: 'select',
- options: [
- "no",
- "yes"
- ]
- },
- //channel, the user want to set the description to
- {
- name: 'descriptionChannel',
- title: 'Set a list of channels and their time until deletion as description of this channel.',
- type: 'channel',
- conditions: [
- { field: 'enableDescription', value: 1}
- ]
- },
- //how often the description should be updated
- {
- name: 'descriptionUpdateTime',
- title: 'Update the description every ... minutes',
- type: 'number',
- conditions: [
- { field: 'enableDescription', value: 1}
- ]
- },
- {
- name: 'descriptionRaw',
- title: 'Enter the description. %channelList% will be replaced with a list of all channels and their time until deletetion.',
- type: 'multiline',
- conditions: [
- { field: 'enableDescription', value: 1}
- ]
- },
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- {
- name: 'enableWarning',
- title: 'Do you want the bot to warn the users before the channel will be deleted, by adding text to the channelname?',
- type: 'select',
- options: [
- "no",
- "yes"
- ]
- },
- {
- name: 'minutesBeforeWarning',
- title: 'How many minutes before channel-deletion do you want the bot to edit the channel?',
- type: 'number',
- conditions: [
- { field: 'enableWarning', value: 1}
- ]
- },
- {
- name: 'warningString',
- title: 'What should the bot add to the channelname?',
- type: 'string',
- conditions: [
- { field: 'enableWarning', value: 1}
- ]
- },
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- {
- name: 'logStatus',
- title: 'enable logging?',
- type: 'checkbox'
- },
- ],
- },
- function (sinusbot, config) {
- var engine = require ('engine');
- var backend = require ('backend');
- var event = require ('event');
- var store = require('store');
- /* setting variables */
- var listenType = config.listenType; //0 = all channels | 1 = specific channels | 2 = all channels with the same parentchannel
- var channelsToIgnore = config.ignoreChannels; //array of channels to ignore
- var timeTillDeletion = config.timeTillDeletion; //time, a channel can stay unused until it will be deleted (in minutes)
- var setDescription = config.setDescription; //0 = no | 1 = yes
- var specificChannels = config.chooseChannels; //array of channels to check if user only wants specific channels to check
- var parentChannels = config.parentChannels; //parent-channel, which subchannels should be checked
- var enableWarning = config.enableWarning; //0 = dont want | 1 = enable warning
- var minutesBeforeWarning = config.minutesBeforeWarning; //minutes before a channel will be deleted, the bot should warn
- var warningString = config.warningString; //string of what the bot should add to the channelname when warning
- var enableDescription = config.enableDescription;//0 =disable | 1 = enable
- var descriptionChannel = config.descriptionChannel; //channel, the users want to set the description at
- var descriptionUpdateTime = config.descriptionUpdateTime; //time in minutes, how often a user want to refresh the description
- var descriptionRaw = config.descriptionRaw; //Is the text the user want to have as description...
- var logStatus = config.logStatus; //true if user want to logs, false if not.
- var allparentchannels = allParentChannels(parentChannels);
- /* end setting variables */
- /* main */
- /* Debug Area*/
- event.on('chat', function(ev) {
- if (ev.text.indexOf("!printDebug") >= 0) {
- var AllChannelIdsToCheck = listOfAllChannelsToCheck();
- engine.log(allParentChannels(parentChannels));
- }
- else if (ev.text.indexOf("!deleteChannelDB") >= 0) {
- store.set('unusedChannelsDB', [{ channelID: "-1", channelName: "nothing", timeStamp: 100}]);
- engine.log("Main-/Channel-Datebase has been reset successfully!");
- }
- else if (ev.text.indexOf("!deleteChannelNamesDB") >= 0) {
- store.set('channelNames', [{ channelID: "-1", channelName: "nothing", timeStamp: 100}]);
- engine.log("Channel-Name-Datebase has been reset successfully!");
- }
- else if (ev.text.indexOf("!channelNameDB") >= 0) {
- var channelNames = store.get('channelNames');
- engine.log(channelNames);
- }
- else if (ev.text.indexOf("!printMainDB") >= 0) {
- //defining datebase
- var unusedChannelsDB = definingDatebase();
- engine.log('There are ' + unusedChannelsDB.length + ' channels in the datebase.');
- engine.log(unusedChannelsDB);
- }
- else if (ev.text.indexOf("!recheckAllChannels") >= 0) {
- getAllChannelsToCheck();
- engine.log("rechecked all channels (function getAllChannelsToCheck()");
- }
- });
- /* End Debug Area */
- /////////////////////////Unused-Channel-Warning/////////////////////////
- setInterval(function() {
- if (logStatus) {engine.log("running function: 'warningSystem()'");}
- warningSystem();
- }, 300000); //5 min.
- /////////////////////////Unused-Channel-Deletion/////////////////////////
- setInterval(function() {
- if (logStatus) {engine.log("running function: 'checkForChannelToDelete()'");}
- checkForChannelToDelete();
- }, 300000); //5 min.
- /////////////////////////Synchronizing Main-Datebase/////////////////////////
- setInterval(function() {
- if (logStatus) {engine.log("running function: 'syncUnusedChannelDatebase()'");}
- if (backend.isConnected()) {
- syncUnusedChannelDatebase();
- }
- }, 600000); //10 min.
- /////////////////////////Updating Description/////////////////////////
- setInterval(function() {
- if (logStatus) {engine.log("running function: 'updateDescription()'");}
- updateDescription();
- }, descriptionUpdateTime*60000);
- event.on('clientMove', function (ev) {
- if (ev.toChannel != undefined) {
- if (channelIsToCheck(ev.toChannel.id())) {
- /////////////////////////Updating Main-Datebase/////////////////////////
- if (logStatus) {engine.log("running function: 'updateDatebase()'");}
- updateDatebase(ev.toChannel.id());
- }
- }
- /////////////////////////Revoke-Warning/////////////////////////
- if (ev.toChannel != undefined) {
- if (logStatus) {engine.log("running function: 'reviewWarningStatus()'");}
- reviewWarningStatus(ev.toChannel.id());
- }
- });
- /* end main */
- /* main functions */
- //returns an array containing all channel ids to check
- function listOfAllChannelsToCheck() {
- var listOfAllChannelsToCheck = [];
- //all channels
- if (listenType == 0) {
- if(channelsToIgnore == undefined) {
- var listOfAllChannelsToCheck = allChannelIds();
- }
- else {
- allChannelIds().forEach(function(channel) {
- if(channelBlacklist().indexOf(channel) < 0) {
- listOfAllChannelsToCheck.push(channel);
- }
- })
- }
- }
- //specific channels
- if (listenType == 1) {
- for (i = 0; i < specificChannels.length; i++) {
- listOfAllChannelsToCheck.push(backend.getChannelByID(specificChannels[i].channel).id());
- }
- }
- //channels with the same parent-channel
- if (listenType == 2) {
- var allChannelIDs = allChannelIds();
- var allparentchannels = allParentChannels(parentChannels);
- for (i = 0; i < allChannelIDs.length; i++) {
- if (backend.getChannelByID(allChannelIDs[i]).parent() != undefined && allparentchannels.indexOf(backend.getChannelByID(allChannelIDs[i]).parent())) {
- listOfAllChannelsToCheck.push(allChannelIDs[i]);
- }
- else {
- }
- }
- }
- return listOfAllChannelsToCheck;
- };
- //updating the datebase
- function updateDatebase(channelID) {
- /* defining the datebase */
- //defining datebase
- var unusedChannelsDB = definingDatebase();
- /* Defining parameters of the channel currently worked on */
- var currChannelID = channelID;
- if (logStatus) {engine.log("Incoming ChannelID: " + currChannelID);}
- var currChannelName = backend.getChannelByID(currChannelID).name();
- if (logStatus) {engine.log("name of the channel: " + currChannelName);}
- var currTime = Math.floor(Date.now() / 1000);
- //If channel is already in the datebase
- if (allSafedChannelIDs().indexOf(channelID) >= 0) {
- for (i = 0; i < unusedChannelsDB.length; i++) {
- if (unusedChannelsDB[i].channelID == currChannelID) {
- unusedChannelsDB[i].channelName = currChannelName;
- unusedChannelsDB[i].timeStamp = currTime;
- }
- }
- store.set('unusedChannelsDB', unusedChannelsDB);
- }
- //Add channel to the datebase
- else {
- unusedChannelsDB.push({ channelID: currChannelID, channelName: currChannelName, timeStamp: currTime});
- store.set('unusedChannelsDB', unusedChannelsDB);
- }
- };
- //whole process of warning by editing channel-names & icons
- function warningSystem() {
- //check if the warningsystem is enabled
- if (enableWarning == 1) {
- //log common errors
- if (minutesBeforeWarning == undefined) {
- engine.log("you need to set a time, the bot should warn the users before deletion in your webinterface! | contact <admin@everlike.de>");
- }
- else if (warningString == undefined && warningIcon == undefined) {
- engine.log("You need to define at least a text the bot should add to the channelname before deletion or enter a iconID, if you dont want to enable the warning system, just disable it in the webinterface. | contact <admin@everlike.de>");
- }
- else if (warningString != undefined && warningString.length > 40) {
- engine.log ("A channelname can´t be longer than 40 characters, please change the warning text in your webinterface. | contact <admin@everlike.de>");
- }
- //defining datebase
- var unusedChannelsDB = definingDatebase();
- //get actual time
- var now = Math.floor(Date.now() / 1000);
- if (warningString != undefined) {
- for (i = 0; i < unusedChannelsDB.length; i++) {
- //checking if its time to warn and or if the warning message has already been set
- if ((backend.getChannelByID(unusedChannelsDB[i].channelID) != undefined) && (timeTillDeletion - ((now - unusedChannelsDB[i].timeStamp) /60)) <= minutesBeforeWarning && backend.getChannelByID(unusedChannelsDB[i].channelID).name().indexOf(warningString) < 0) {
- //safe original channelname
- safeChannelname(unusedChannelsDB[i].channelID);
- //set warning-name
- backend.getChannelByID(unusedChannelsDB[i].channelID).setName(defineWarnChannelname(unusedChannelsDB[i].channelID));
- }
- else {
- }
- }
- }
- }
- };
- //changes back the channelname to the original one (Only use when a user joins the specific channel)
- function reviewWarningStatus(channelID) {
- if (backend.getChannelByID(channelID) != undefined && backend.getChannelByID(channelID).name().indexOf(warningString) >= 0) {
- setOriginalChannelname(channelID);
- }
- };
- //checks if channel is unused and propably deletes it
- function checkForChannelToDelete() {
- //defining datebase
- var unusedChannelsDB = definingDatebase();
- //get actual time
- var now = Math.floor(Date.now() / 1000);
- //in case this channeldata is incorrect or the channel does not exist anymore
- for (i = 0; i < unusedChannelsDB.length; i++) {
- if (backend.getChannelByID(unusedChannelsDB[i].channelID) == undefined) {
- engine.log("Seems like the channel: " + unusedChannelsDB[i].channelName + " with the channel-id: " + unusedChannelsDB[i].channelID + " does not exist anymore");
- }
- //if channel is inactive for too long - delete it
- else if ((backend.getChannelByID(unusedChannelsDB[i].channelID) != undefined) && (now - unusedChannelsDB[i].timeStamp) /60 >= timeTillDeletion) {
- //delete channel
- backend.getChannelByID(unusedChannelsDB[i].channelID).delete();
- //remove server from the datebase
- unusedChannelsDB.splice(i, 1);
- i--;
- }
- }
- //safe the datebase
- store.set('unusedChannelsDB', unusedChannelsDB);
- };
- //checks each datebase entry if channel is still active, if not, deletes it from the db.
- function syncUnusedChannelDatebase() {
- //defining datebase
- var unusedChannelsDB = definingDatebase();
- //get all channelids
- var allChannelIDs = allChannelIds();
- //get all channelids to check
- var AllChannelIdsToCheck = listOfAllChannelsToCheck();
- for (i = 0; i < unusedChannelsDB.length; i++) {
- if (allChannelIDs.indexOf(unusedChannelsDB[i].channelID) < 0 || AllChannelIdsToCheck.indexOf(unusedChannelsDB[i].channelID) < 0) {
- unusedChannelsDB.splice(i, 1);
- i--;
- }
- }
- store.set('unusedChannelsDB', unusedChannelsDB);
- };
- //sets/updates all channels and their time until deletion as description of a specific channel
- function updateDescription() {
- if (enableDescription == 1) {
- if (descriptionChannel == undefined || descriptionUpdateTime == undefined) {
- engine.log("Seems like you want the bot to set the channels and their time until deletion as description of a channel, but you haven´t entered the channel or update-time right...");
- }
- //generating the description-string
- var descriptionString = definingDescription();
- //set the description
- backend.getChannelByID(descriptionChannel).setDescription(descriptionString);
- }
- };
- //return true if channel has to be checked, false if not. (Made to safe ressourcces)
- function channelIsToCheck(channelID) {
- if (listenType == 0) {
- if(listOfAllChannelsToCheck().indexOf(channelID) >= 0) {
- return true;
- }
- else {
- return false;
- }
- }
- else if (listenType == 1) {
- if (listOfAllChannelsToCheck().indexOf(channelID) >= 0) {
- return true;
- }
- else {
- return false;
- }
- }
- else if (listenType == 2) {
- if (backend.getChannelByID(channelID).parent() != undefined) {
- if (allParentChannels(parentChannels).indexOf(backend.getChannelByID(channelID).parent().id()) >= 0) {
- return true;
- }
- else {
- return false;
- }
- }
- }
- };
- //get and set all channels to check
- function getAllChannelsToCheck() {
- //defining datebase
- var unusedChannelsDB = definingDatebase();
- //getting all channel ids already safed in the main db
- var allCurrentSafedChannelIDs = allSafedChannelIDs()
- //getting all channel ids
- var allCurrExistingChannelIDs = allChannelIds();
- allCurrExistingChannelIDs.forEach(function(channelid) {
- if (channelIsToCheck(channelid)) {
- updateDatebase(channelid);
- }
- });
- };
- /* end main functions */
- /* helper functions */
- //returns an array of ids of blacklisted channels
- function channelBlacklist() {
- var channelBlacklist = [];
- if (channelsToIgnore != undefined) {
- for (i = 0; i < channelsToIgnore.length; i++) {
- channelBlacklist.push(backend.getChannelByID(channelsToIgnore[i].channel).id());
- }
- }
- return channelBlacklist;
- };
- //returns an array of all channel ids
- function allChannelIds() {
- if(logStatus) {engine.log("now executing funcion 'allChannelIds()'");}
- var allChannelIds = [];
- var allChannels = backend.getChannels();
- if(logStatus) {engine.log("backend.getChannels returned: " + allChannels.length + " channels");}
- for (i = 0; i < allChannels.length; i++) {
- allChannelIds.push(allChannels[i].id());
- }
- return allChannelIds;
- };
- //returns an array of ids of channels existing in the datebase
- function allSafedChannelIDs() {
- var unusedChannelsDB = store.get('unusedChannelsDB');
- var allSafedChannelIDs = new Array;
- for (i = 0; i < unusedChannelsDB.length; i++) {
- allSafedChannelIDs.push(unusedChannelsDB[i].channelID);
- }
- return allSafedChannelIDs
- };
- //adds something to a channelname to warn users that their channel will be deleted soon.
- function warnChannelName(channelID) {
- var channelName = backend.getChannelByID(channelID).name();
- };
- function definingDatebase() {
- //defining datebase
- if (logStatus) {engine.log("fired 'definingDatebase()' function");}
- if(backend.isConnected()) {
- if (logStatus) {engine.log("backend is connected.");}
- var currentuucdb = store.get('unusedChannelsDB');
- if (logStatus) {engine.log("store.get('unusedChannelsDB') returned: " + currentuucdb);}
- if ((currentuucdb == undefined) || (currentuucdb == "") || (currentuucdb == [])) {
- if (logStatus) {engine.log("store.get('unusedChannelsDB') is undefined, an empty string or an empty array.");}
- var unusedChannelsDB = [{ channelID: "-1", channelName: "nothing", timeStamp: 100}];
- if (logStatus) {engine.log("created a new Datebase");}
- store.set('unusedChannelsDB', unusedChannelsDB);
- }
- //get the datebase
- var unusedChannelsDB = store.get('unusedChannelsDB');
- if (logStatus) {engine.log("getting datebase in function definingDatebase() returns: " + unusedChannelsDB.length + " channels");}
- return unusedChannelsDB;
- }
- else {
- if (logStatus) {engine.log("backend is not connected");}
- }
- };
- function safeChannelname(channelID) {
- //defining datebase
- if (store.get('channelNames') == undefined) {
- var channelNames = [];
- store.set('channelNames', channelNames);
- }
- //get the datebase
- var channelNames = store.get('channelNames');
- if (backend.getChannelByID(channelID) != undefined) {
- var channelName = backend.getChannelByID(channelID).name();
- //add channelinformation to the datebase
- channelNames.push({ channelID: channelID, channelName: channelName});
- }
- //store the updated datebase
- store.set('channelNames', channelNames);
- };
- //generating the new channelname including the warningmessage
- function defineWarnChannelname(channelID) {
- var channelName = backend.getChannelByID(channelID).name();
- if ((channelName.length + warningString.length) > 40) {
- var charsTooMuch = (channelName.length + warningString.length) - 40;
- var updatedChannelName = channelName.substring(0,channelName.length-charsTooMuch) + config.warningString;
- }
- else {
- var updatedChannelName = channelName + warningString;
- }
- return updatedChannelName;
- };
- function setOriginalChannelname (channelID) {
- //defining datebase
- if (store.get('channelNames') == undefined) {
- var channelNames = [];
- store.set('channelNames', channelNames);
- }
- //get the datebase
- var channelNames = store.get('channelNames');
- //get original channel name
- for (i = 0; i < channelNames.length; i++) {
- if (channelNames[i].channelID == channelID) {
- //set channelname back to the original one
- backend.getChannelByID(channelID).setName(channelNames[i].channelName);
- //remove original name entry from datebase
- channelNames.splice(i, 1);
- i--;
- }
- }
- //safe datebase
- store.set('channelNames', channelNames);
- };
- //defines a string, containing the updated description of a channel that shows all channels in the db and their time until deletion.
- function definingDescription() {
- //defining Datebase
- var unusedChannelsDB = definingDatebase();
- //defining now
- var now = Math.floor(Date.now() / 1000);
- //create var for the list of channels and their time until deleteion
- var listOfChannelsDeletionTime = [];
- for (i = 0; i < unusedChannelsDB.length; i++) {
- listOfChannelsDeletionTime.push("\n" + 'Channel "' + "[b]" + unusedChannelsDB[i].channelName + "[/b]"+ '"' + " will be deleted in " + convertTime((timeTillDeletion*60) - (now - unusedChannelsDB[i].timeStamp)));
- };
- var description = (descriptionRaw.replace("%channelList%", listOfChannelsDeletionTime)) + "[center][size=7]made by Everlike <[url]everlike.de[/url]>[/size][/center]";
- //checking if description is too long
- if (description.length > 8192) {
- var varsTooMuch = (description + "not all channels are shown due to less charspace" + "[center][size=7]made by Everlike <[url]everlike.de[/url]>[/size][/center]").length - 8192;
- var description = description.substring(0,description.length-varsTooMuch) + "not all channels are shown due to less charspace" + "[center][size=7]made by Everlike <[url]everlike.de[/url]>[/size][/center]";
- }
- //descriptionRaw
- return description;
- };
- function convertTime(seconds) {
- var days = Math.floor(seconds / (3600*24));
- seconds -= days*3600*24;
- var hrs = Math.floor(seconds / 3600);
- seconds -= hrs*3600;
- var mnts = Math.floor(seconds / 60);
- seconds -= mnts*60;
- return days+" days, "+hrs+" Hrs, "+mnts+" Minutes, "+seconds+" Seconds";
- };
- function allParentChannels(parentChannelsToCheck) {
- allParentChannelIDs = [];
- for (i = 0; i < parentChannelsToCheck.length; i++) {
- allParentChannelIDs.push(parentChannelsToCheck[i].channel);
- }
- return allParentChannelIDs;
- };
- /* end helper functions */
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement