Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            // register to unhandled exceptions handling
            // this allows logging exceptions that were not handled with a try-catch block
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

            // optional: create a logger
            ILogger <WolfClient> log = CreateLogger <WolfClient>();

            // create client and listen to events we're interested in
            _client = new WolfClient(log);
            _client.MessageReceived += OnMessageReceived;               // This event is raised when client receives and parses any message type.
            _client.AddMessageListener <WelcomeEvent>(OnWelcome);       // these 2 callbacks are invoked if received message is a WolfEvent (first callback)
            _client.AddMessageListener <ChatMessage>(OnChatMessage);    // or a chat message (second callback).

            // reconnector is part of Wolfringo.Utilities package
            WolfClientReconnector reconnector = new WolfClientReconnector(_client);

            reconnector.FailedToReconnect += OnFailedToReconnect;

            // start connection and prevent the application from closing
            await _client.ConnectAsync();

            await Task.Delay(-1);
        }
        static async Task Main(string[] args)
        {
            // register to unhandled exceptions handling
            // this allows logging exceptions that were not handled with a try-catch block
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

            // optional: create a logger factory
            ILoggerFactory logFactory = CreateLoggerFactory();
            // optional: enable DI - add logger factory to it as well
            IServiceCollection services = new ServiceCollection()
                                          .AddSingleton <ILoggerFactory>(logFactory);

            // create client and listen to events we're interested in
            _client = new WolfClient(logFactory.CreateLogger <WolfClient>());
            _client.AddMessageListener <WelcomeEvent>(OnWelcome);        // these 2 callbacks are invoked if received message is a WolfEvent (first callback)

            // initialize commands system
            CommandsOptions options = new CommandsOptions()
            {
                Prefix          = "!",                      // set prefix
                RequirePrefix   = PrefixRequirement.Always, // make prefix always required - can also for example require it in group only
                CaseSensitivity = false                     // make commands case insensitive
            };
            CommandsService commands = new CommandsService(_client, options, logFactory.CreateLogger <CommandsService>(), services.BuildServiceProvider());
            await commands.StartAsync();                    // calling StartAsync causes reload of all commands

            // start connection and prevent the application from closing
            await _client.ConnectAsync();

            await Task.Delay(-1);
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public async Task <T> AwaitNextAsync(IWolfClient client, CancellationToken cancellationToken = default)
        {
            TaskCompletionSource <T> tcs = new TaskCompletionSource <T>();

            using (cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken)))
            {
                Action <T> callback = null;
                callback = message =>
                {
                    if (!this._conditions(message))
                    {
                        return;
                    }
                    client.RemoveMessageListener <T>(callback);
                    tcs.TrySetResult(message);
                };

                client.AddMessageListener(callback);
                return(await tcs.Task);
            }
        }
Ejemplo n.º 4
0
 /// <summary>Adds event listener, invoking when received message is of correct type and has matching command.</summary>
 /// <typeparam name="T">Type of received message to invoke callback for.</typeparam>
 /// <param name="command">Message command that has to match for callback to be invoked.</param>
 /// <param name="callback">Callback to invoke on event.</param>
 public static void AddMessageListener <T>(this IWolfClient client, string command, Action <T> callback) where T : IWolfMessage
 => client.AddMessageListener(new CommandMessageCallback <T>(command, callback));
Ejemplo n.º 5
0
 /// <summary>Adds event listener, invoking when received message is of correct type.</summary>
 /// <typeparam name="T">Type of received message to invoke callback for.</typeparam>
 /// <param name="callback">Callback to invoke on event.</param>
 public static void AddMessageListener <T>(this IWolfClient client, Action <T> callback) where T : IWolfMessage
 => client.AddMessageListener(new TypedMessageCallback <T>(callback));