Now that your bot is connected, let's make it respond to messages by listening to gateway events.
The GatewayClient provides real-time events through a WebSocket connection. These events fire whenever something happens on Fluxer, such as:
MessageCreate)PresenceUpdate)GuildMemberAdd)Before your bot can start receiving events, it needs to wait for the Ready event, which signals that the initial connection is complete:
using Fluxer.Net;
using Serilog;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.CreateLogger();
const string token = "YOUR_TOKEN_HERE";
var config = new FluxerConfig
{
Serilog = Log.Logger
};
var apiClient = new ApiClient(token, config);
var gatewayClient = new GatewayClient(token, config);
// Subscribe to the Ready event
gatewayClient.Ready += (data) =>
{
Log.Information("Bot is ready! Logged in as {Username}", data.User.Username);
Log.Information("Connected to {GuildCount} guilds", data.Guilds.Count);
};
await gatewayClient.ConnectAsync();
Log.Information("Connecting to Fluxer...");
await Task.Delay(-1);
To respond to messages, subscribe to the MessageCreate event:
gatewayClient.MessageCreate += async (data) =>
{
var message = data.Message;
// Log the message
Log.Information("Message from {Author}: {Content}",
message.Author.Username,
message.Content);
// Don't respond to our own messages
if (message.Author.Id == data.User.Id)
return;
// Check if the message mentions the bot
if (message.Content.Contains("hello", StringComparison.OrdinalIgnoreCase))
{
// Send a reply
var reply = new Message
{
Content = $"Hello, {message.Author.Username}!"
};
await apiClient.SendMessage(message.ChannelId, reply);
}
};
Here's the complete code with both events:
using Fluxer.Net;
using Fluxer.Net.Data.Models;
using Serilog;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.CreateLogger();
const string token = "YOUR_TOKEN_HERE";
var config = new FluxerConfig
{
Serilog = Log.Logger
};
var apiClient = new ApiClient(token, config);
var gatewayClient = new GatewayClient(token, config);
gatewayClient.Ready += (data) =>
{
Log.Information("Bot ready! Logged in as {Username}", data.User.Username);
};
gatewayClient.MessageCreate += async (data) =>
{
var message = data.Message;
// Ignore our own messages
if (message.Author.Id == data.User.Id)
return;
if (message.Content.Contains("hello", StringComparison.OrdinalIgnoreCase))
{
var reply = new Message
{
Content = $"Hello, {message.Author.Username}!"
};
await apiClient.SendMessage(message.ChannelId, reply);
}
};
await gatewayClient.ConnectAsync();
await Task.Delay(-1);
dotnet runThe MessageCreate event shown in this tutorial is useful for:
However, for building a bot with commands (like /ping, /help, etc.), you should use the CommandService framework instead of manually parsing messages in the MessageCreate event. The CommandService provides:
In the next tutorial, you'll learn how to use the CommandService to build a proper command bot!
Now that your bot can receive and respond to messages, let's create a structured command system using the CommandService framework.