I’m trying to make my Discord bot show a custom activity but I keep getting syntax errors. Here’s what I have:
public class ActivityModule : ModuleBase<SocketCommandContext>
{
private DiscordSocketClient _botClient;
public async Task SetActivityAsync()
{
var activityText = "watching movies";
await _botClient.SetGameAsync(string activityText, string url = null, ActivityType activity = Playing);
}
}
The compiler is throwing errors like “Invalid expression term” and “Syntax error, ‘,’ expected” on the SetGameAsync line. The red underlines are showing up everywhere in the method parameters. I just want my bot to display “watching movies” as its status. What’s wrong with my syntax here? I’ve been stuck on this for hours and can’t figure out the correct way to call this method.
You’re mixing named and positional parameters incorrectly. In C#, you can’t declare parameter types when calling a method like SetGameAsync(string activityText, string url = null, ActivityType activity = Playing). Use all positional parameters: await _botClient.SetGameAsync(activityText, null, ActivityType.Watching), or switch to the newer SetActivityAsync method as DancingBird suggested. Also, change ActivityType.Playing to ActivityType.Watching since you want “watching movies”. I encountered a similar issue while upgrading Discord.NET versions - the old syntax examples mix declarations with actual method calls.
Your method signature’s wrong. SetGameAsync doesn’t take named parameters like that. Try this instead:
public async Task SetActivityAsync()
{
var activityText = "watching movies";
await _botClient.SetActivityAsync(new Game(activityText, ActivityType.Watching));
}
Make sure _botClient is initialized properly or you’ll get null reference exceptions. I ran into the same thing when I started - you need to create a Game object first, then pass it to SetActivityAsync. SetGameAsync is deprecated in newer Discord.NET versions anyway. This approach has worked great for me across multiple bot projects.
you’re mixing up method declaration with method calls. Don’t use string activityText when calling the method - that’s only for declaring params. Just call it like this: await _botClient.SetGameAsync(activityText, null, ActivityType.Watching); without the type annotations. also double-check that your client’s properly injected in the module constructor.