I’m working on a Discord bot and I want it to send a welcome message whenever someone enters a voice channel. The problem is that I can’t figure out which event to use for this. I tried using botClient.GuildMemberUpdated and similar events but they don’t seem to work the way I expected.
I need this to work for all users, not just new server members. So when someone who’s already in the server joins a voice channel, the bot should detect it and respond.
Here’s what I have so far:
public class DiscordBot
{
private DiscordSocketClient botClient;
private CommandService commandHandler;
private IServiceProvider serviceProvider;
static void Main(string[] args)
=> new DiscordBot().StartBot().GetAwaiter().GetResult();
public async Task StartBot()
{
botClient = new DiscordSocketClient();
commandHandler = new CommandService();
serviceProvider = new ServiceCollection()
.AddSingleton(botClient)
.AddSingleton(commandHandler)
.BuildServiceProvider();
string token = // hidden
botClient.Log += LogEvents;
await SetupCommands();
await botClient.LoginAsync(TokenType.Bot, token);
await botClient.StartAsync();
await Task.Delay(-1);
}
private Task LogEvents(LogMessage message)
{
Console.WriteLine(message);
return Task.CompletedTask;
}
public async Task SetupCommands()
{
botClient.MessageReceived += ProcessMessage;
botClient.GuildMemberUpdated += UserEnteredChannel; // This doesn't work as expected
await commandHandler.AddModulesAsync(Assembly.GetEntryAssembly());
}
private Task UserEnteredChannel(SocketGuildUser before, SocketGuildUser after)
{
throw new NotImplementedException();
}
private async Task ProcessMessage(SocketMessage msg)
{
var userMessage = msg as SocketUserMessage;
if(userMessage is null || userMessage.Author.IsBot)
{
return;
}
int position = 0;
if (userMessage.HasStringPrefix("?", ref position))
{
var context = new SocketCommandContext(botClient, userMessage);
await commandHandler.ExecuteAsync(context, position);
}
}
}
What event should I be using to detect when users join voice channels? Any help would be great.