• If you need help or want to discuss things, you now can also join us on our Discord Server!
  • A first preview of the unlimited version of SinusBot can be found in the Upcoming Changes thread. A version for Windows will follow, but we don't have a release date, yet.

EN [SOLVED] Check if channel already exists with the same name

DrWarpMan

Well-Known Member
Contributor
Insider
How can I check in the "createChannel" event, if there's already a channel with the same name as this created channel's name?
 
Last edited:

Lala Sabathil

Donor
is awesome!
Contributor
Insider
Wenn du einen normalen Channel erstellst ohne spacer values, dann hast du keine unique id, wodurch du den channel dann nicht so benennen kannst.
Ts würde es ablehnen, bevor der bot es merkt
 

DrWarpMan

Well-Known Member
Contributor
Insider
You can create two same named channels, just try it - create one temporary channel named "kappa" and then create a permanent channel that is a subchannel of any channel, named "kappa" as well.

SS: 1537692284991.png
 

DrWarpMan

Well-Known Member
Contributor
Insider
I just found an annoying bug..

Code:

Code:
registerPlugin({
    name: 'notSame',
    version: '0.1',
    backends: ['ts3'],
    description: 'Block creating channels with the same name',
    author: 'DrWarpMan',
    vars: []
}, function(sinusbot, config) {
    var engine = require('engine');
    var backend = require('backend');
    var event = require('event');
    
    event.on('channelCreate', function(ev) {
        var CH = backend.getChannelByID(ev.id());
        CH.setName("Hello");
    });

    }
});

I think that this code works good, BUT, whenever I start the bot / script, this happens:


<11:03:22> Channel "XXXX" was renamed to "Hello" by "Admin [BOT]"
<11:03:22> Channel "XXXX" was renamed to "Hello" by "Admin [BOT]"
<11:03:23> Channel "XXXX" was renamed to "Hello" by "Admin [BOT]"
<11:03:23> Channel "XXXX" was renamed to "Hello" by "Admin [BOT]"
<11:03:23> Channel "XXXX" was renamed to "Hello" by "Admin [BOT]"

Where "XXXX" are random channels on my server, so, whenever I start the bot or the script, it renames like 10 channels on my server to "Hello".
 

Everlike

Well-Known Member
Contributor
Gern geschehen :)
Ist ´ne Grundlage und Erfüllt den Zweck.
Die ev.invoker.chat() funkt leider nicht, keine Ahnung was ich da falsch gemacht habe oder ob mein Sinusbot das noch nicht kann.
Das Script funktioniert aber, sobald ein Channel erstellt wird, mit einem Namen der bereits existiert, wird er sofort wieder gelöscht.
JavaScript:
registerPlugin({
    name: 'channelNameChecker',
    version: '0.1',
    description: 'Checks if a channelname already exists, if so, the new channel will be deleted.',
    author: 'Everlike <[email protected]>'
        vars: []
       
    },  function (sinusbot, config) {
       
    var engine = require ('engine');
    var backend = require ('backend');
    var event = require ('event');
               
               
                setTimeout(function(){
               
                event.on('channelCreate', function (ev) {
                if (getAllChannelNames().indexOf(ev.name()) >= 0) {
                    ev.delete();
            //        ev.invoker.chat("Sorry, this channelname already exists, choose another oneplease");
                    }
                });
                   
                }, 20000);
                   
                    function getAllChannelNames() {
                        var allChannelNames = [];
                        var allChannels = backend.getChannels();
                        for (i = 0; i < allChannels.length; i++) {  
                        allChannelNames.push(allChannels[i].name());
                        }
                        return allChannelNames;
                    };
                });
 

DrWarpMan

Well-Known Member
Contributor
Insider
I'm going to try it, but before that, I want to understand the code, am I right, that the setTimeout is there, because of the "bug" that I told you right?
 

Everlike

Well-Known Member
Contributor
I don´t know what bug you mean, but when I tested, when the bot joins, every channel that already exists will be "created" for the bot for some reason... that would cause bugs, so I set a 20 sec. timeout.
 

DrWarpMan

Well-Known Member
Contributor
Insider
Yeah, thats the bug.

+ If I want to test scripts, do I need to restart the whole bot everytime to load the script again?
 

DrWarpMan

Well-Known Member
Contributor
Insider
Oh, I found out, that when I create channel, it checks if it already exists, but in the function getAllChannelNames, there is already the channel I created, and it deletes the channel because the created channel is already in the getAllChannelNames, so I have to remove it from the array, and check it then.
 

Everlike

Well-Known Member
Contributor
Ohhh, Iam stupid, I see... You can fix this yourself or should I have a look on it later?
 

DrWarpMan

Well-Known Member
Contributor
Insider
Code:
registerPlugin({
    name: 'channelNameChecker',
    version: '0.1',
    description: 'Checks if a channelname already exists, if so, the new channel will be deleted.',
    author: 'Everlike <[email protected]> & DrWarpMan',
    vars: []
       
},  function (sinusbot, config) {
       
    var engine = require ('engine');
    var backend = require ('backend');
    var event = require ('event');
               
               
    setTimeout(function(){
               
        event.on('channelCreate', function (ev) {

            var x = getAllChannelNames(); // all channels

            // remove the created channel from array
            for (var i=x.length-1; i>=0; i--) {
                if (x[i] === ev.name()) {
                    x.splice(i, 1);
                    break; // remove only the first time
                }
            };

            // if channel with the same name exists
            if (x.indexOf(ev.name()) >= 0)
            {
                // do this
                var newName = ev.name().slice(0, ev.name().length-1); // remove last character from the name 
                ev.setName(newName); // set the name
            }
        });
           
    }, 20000);
    
    // getting all channels' names
    function getAllChannelNames() {
        var allChannelNames = [];
        var allChannels = backend.getChannels();
        for (i = 0; i < allChannels.length; i++) {  
            allChannelNames.push(allChannels[i].name());
        }
        return allChannelNames;
    };

});

Thanks a lot @Everlike !
 
Top