Splamy / TS3AudioBot

Advanced Musicbot for Teamspeak 3
https://splamy.de/TSAudioBot/Home
Open Software License 3.0
692 stars 140 forks source link

Plugins #241

Closed dzinks2009 closed 6 years ago

dzinks2009 commented 6 years ago

Hello, I was wondering how I could add plugins to the developer version. I added the .cs file from Visual Studio to folder Plugins but it didn't work.

My code:

using System;
using System.Linq;
using TS3Client;
using TS3Client.Commands;
using TS3Client.Full;

public class NowPlaying : ITabPlugin
{
    public void Initialize(MainBot bot) {

    }

    public void Dispose() {

    }
}

Errors: The type or namespace name 'MainBot' could not be found (are you missing a using directive or an assembly reference?) The type or namespace name 'ITabPlugin' could not be found (are you missing a using directive or an assembly reference?)

Flakebi commented 6 years ago

It seems like the Plugin-API changed but the wiki article was not updated. This is how you can create a plugin in the new system:

using System;
using System.Linq;
using TS3AudioBot;
using TS3AudioBot.Plugins;
using TS3Client;
using TS3Client.Commands;
using TS3Client.Full;

public class NowPlaying : IBotPlugin
{
    public Bot bot { get; set; }

    public void Initialize()
    {
        Console.WriteLine("Bot: " + bot.GetInfo());
    }

    public void Dispose()
    {
    }
}

Public properties are automagically set when the plugin is loaded (here).

dzinks2009 commented 6 years ago

Thanks, that works. But my plugin still doesn't launch. What I did is add a new class to the Plugins folder. Is that the right way to do it or not?

Flakebi commented 6 years ago

Yes that’s right, then you have to write !plugin list to show all available plugins and !plugin load <plugin id, for example 0> (or unload) to load the plugin.

dzinks2009 commented 6 years ago

When I do !plugin list I get

"AudioBot": No plugins found!
Flakebi commented 6 years ago

Hm, it works for me, I have PluginManager::PluginPath=Plugins in configTS3AudioBot.cfg and the file is in Plugins/plugin.cs (relative to the config file).

dzinks2009 commented 6 years ago

Which version do you use?

Flakebi commented 6 years ago

I’m using the current develop branch (commit b529f8e)

dzinks2009 commented 6 years ago

PluginManager::PluginPath=Plugins My "solution" looks like this: https://scr.hu/4XvVmL

Am I doing this wrong?

Flakebi commented 6 years ago

Oh, now I understand. You do not need to add the plugin files to the solution, you even don’t need to recompile the bot. You can just put the raw .cs files into a Plugins folder beside the config. The bot will fetch them at runtime (when you say !plugin list) and compile them himself.

dzinks2009 commented 6 years ago

OMG! Thanks :D It works.

For others if they're wondering about this:

create a folder called Plugins in the built solution and add your plugins there. Then you can type !plugins list to the bot and load them.

dzinks2009 commented 6 years ago

@Flakebi Do you know how I can call this function?

        private void RegisterNotification(string target, ChannelIdT channel)
        {
            var ev = new CommandParameter("event", target.ToLowerInvariant());
            if (target == "channel")
                Send("servernotifyregister", ev, new CommandParameter("id", channel));
            else
                Send("servernotifyregister", ev);
        }

It's located in TS3Client/Query/Ts3QueryClient.cs

Flakebi commented 6 years ago

I guess this is only for the query client (which means the bot does not connect as a normal TS client but as a serverquery) so you probably want to have a look at TS3Client/Full/Ts3FullClient.cs (or e.g. TS3AudioBot/TeamspeakControl.cs.

You can use these classes by adding them as a property:

using System;
using System.Linq;
using TS3AudioBot;
using TS3AudioBot.Plugins;
using TS3Client;
using TS3Client.Commands;
using TS3Client.Full;

public class NowPlaying : IBotPlugin
{
    public Bot bot { get; set; }
    public TeamspeakControl ts { get; set; }

    public void Initialize()
    {
        Console.WriteLine("Bot: " + bot.GetInfo());
        ts.SendChannelMessage("Hi there");
    }

    public void Dispose()
    {
    }
}
dzinks2009 commented 6 years ago

Alright, also I found

        private void InvokeEvent(LazyNotification lazyNotification)
            case NotificationType.ServerGroupList: break;

in Ts3FullClient in TS3Client. Is there a way I can use this?

Flakebi commented 6 years ago

What exactly do you mean by using this? If you want to communicate with the server, you can either use any of the existing methods or you can send commands yourself and get the answer like it is done in many methods in Ts3FullClient.cs.

Splamy commented 6 years ago

Unfortunately I havent added anything to access incomming events yet. You can currently only receive events when you know that the server will respond with a specific notification, like so https://github.com/Splamy/TS3AudioBot/blob/master/TS3Client/Full/Ts3FullClient.cs#L646-L658 but this isn't particularly nice.

I will add events for all notifications soon, right now I am working on some reworks.

Splamy commented 6 years ago

@dzinks2009 I've added event handler for all declared events, it's on the current develop to test if you're still interested. I'll close the issue for now, if you encounter any problems feel free to reopen or open a new issue

dzinks2009 commented 5 years ago

Hi, I don't know if it's better to post here or create a new issue but I wanted to come back to this.

I know in the TeamSpeak3 Client there's a plugin called client query. It lets you listen to all the incoming calls(i guess) from the server by calling RegisterNotification=any(this will return anything that's happening on the server).

I've seen this plugin https://github.com/Bluscream/TS3AudioBotPlugins/tree/develop/ClientQuery but it's not updated and I don't know if it works like the TS3 ClientQuery plugin.

Splamy commented 5 years ago

Ts3 internally uses a protocol that's very similar to the query protocol anyway. We automatically generate c# event for all ts3 events that exist: https://github.com/Splamy/TS3AudioBot/blob/develop/TS3Client/Generated/Ts3FullEvents.cs#L35-L174 As you can see they are all simply in the Ts3FullClient client class where you can conveniently register them.

If you want a tcp server exposed the same way like the normal queryclient you'll have to update the plugin and wrap all the functionality. This is quite a chore, and even blue only wrapped a few selected command

Bluscream commented 5 years ago

Indeed