Table of Contents

Chat Module

The chat module offers a quick and easy implementation to handle chat messages in your alt:V server.

Requirements

This module requires you to have some kind of dependency injection set up in your project that uses the Microsoft's Dependency Injection.

Not sure how to implement that? We've got you covered! Our boilerplate has everything you need to get started.


Getting started

Start by installing the latest version from Nuget.

nuget

Initialization

A quick and simple example:

var builder = Host.CreateDefaultBuilder( );

builder.ConfigureServices( (context, services) =>
{
    services.RegisterChatModule( ); // <--- Register the chat module
} );
    
var host = builder.UseConsoleLifetime( ).Build( );
host.Services.InitializeChatModule( ); // <-- Initialize the chat module

await host.RunAsync();

Modifications

By default the event for sending/receiving chat messages is set to the default chat:message. However, this can easily be changed by passing your own event name as second parameter to RegisterChatModule.

The command prefix is set to / by default, but this can be changed as well by passing it as the third parameter to RegisterChatModule. NOTE: This module does NOT handle commands. The only reason it has the command prefix parameter is so it can properly verify whether the incoming message is a command or a message.

Example usage

All that is left now is to hook into the chat events. For reference, the following example is taken directly from our AltV.Atlas.Boilerplate project.

[Injectable(InstantiateOnBoot = true)] // Ensure this class is instantiated on startup (part of AltV.Atlas.IoC)
public class OnChatMessageEvent
{
    private readonly IChat _chat; // The chat instance

    public OnChatMessageEvent( IChat chat ) // Inject chat instance through DI
    {
        _chat = chat;
        _chat.OnChatMessage += OnChatMessage;
        _chat.OnChatMessageAsync += OnChatMessageAsync; // for async stuff
    }

    // Triggered whenever any player has sent a message in the chat
    private void OnChatMessage( IPlayer player, string message )
    {
        player.SendChatMessageToAll( message ); // Send the message to everyone in the server (global chat example)
    }

    private Task OnChatMessageAsync(IPlayer player, string message)
    {
        // do async stuff here.
    }
}

Extending the library

Note: For a better understanding of how our library works behind the scenes, please have a look at our class diagram below.

Not what you were looking for? Our boilerplate contains a lot of examples that may help you further.

Class Diagram

For a better understanding of how our library works behind the scenes, please have a look at our class diagram below and/or check out the repository.

Class Diagram