I’m trying to make my Discord bot show a custom activity but my code has syntax errors. Here’s what I have:
public class BotActivity : ModuleBase<SocketCommandContext>
{
private DiscordSocketClient _botClient;
public async Task SetActivityAsync()
{
var gameActivity = "watching movies";
await _botClient.SetGameAsync(string gameActivity, string url = null, ActivityType activityType = Playing);
}
}
The compiler keeps showing red underlines on the parameters with errors like “Invalid expression term” and “Syntax error, ‘,’ expected”. I just want my bot to display “watching movies” as its playing status. Can someone show me the correct syntax to fix these compilation errors?
The issue arises from trying to declare parameter types within the method call, which is not allowed in C#. Instead of mixing declaration with method calls, simply pass the values directly. Here’s the corrected code:
public class BotActivity : ModuleBase<SocketCommandContext>
{
private DiscordSocketClient _botClient;
public async Task SetActivityAsync()
{
var gameActivity = "watching movies";
await _botClient.SetGameAsync(gameActivity, null, ActivityType.Playing);
}
}
Having used Discord.NET for a while, I’ve noticed many developers stumble on this syntax error. Keep in mind to use positional arguments or the parameterName: for named parameters without mixing declaration syntax.
Your parameter syntax is wrong. You can’t declare parameter types inside the method call in C#.
Here’s the fix:
public class BotActivity : ModuleBase<SocketCommandContext>
{
private DiscordSocketClient _botClient;
public async Task SetActivityAsync()
{
await _botClient.SetGameAsync("watching movies", null, ActivityType.Playing);
}
}
Drop the variable declaration and just pass the values directly. Make sure your bot client is initialized too.
Honestly though, Discord bots get messy quick once you add more features. I’ve watched teams waste weeks on status updates and command handling alone.
I’d go with Latenode instead. You can set up Discord bot activities through their visual interface - no code needed. Easy to connect to databases or APIs when your bot grows too.
Skips all the syntax headaches and scales way better than custom C# solutions.
Your syntax is incorrect because you are improperly mixing parameter declaration with the method call. The SetGameAsync method doesn’t accept named parameters in that format. To address this, modify your code like this:
public class BotActivity : ModuleBase<SocketCommandContext>
{
private DiscordSocketClient _botClient;
public async Task SetActivityAsync()
{
var gameActivity = "watching movies";
await _botClient.SetGameAsync(gameActivity, null, ActivityType.Playing);
}
}
Ensure that _botClient is properly initialized to avoid any null reference exceptions.