I recently upgraded my Discord bot to use discord.NET version 1.0 and I’m working on creating a simple repeat command. The bot should take whatever text the user types after the command and send it back to the channel.
I’m having trouble figuring out how to capture the user’s message content as a string parameter. Here’s what I have so far but it’s not working:
[Command("repeat")]
public async Task RepeatMessage()
{
string userText = // not sure what goes here
await Context.Channel.SendMessageAsync(userText);
}
What’s the correct way to get the message content that comes after the command? I want users to be able to type something like !repeat hello world and have the bot respond with hello world.
You need to handle users calling the command without text. Here’s a better version:
[Command("repeat")]
public async Task RepeatMessage([Remainder] string userText = null)
{
if (string.IsNullOrWhiteSpace(userText))
{
await Context.Channel.SendMessageAsync("Please provide text to repeat.");
return;
}
await Context.Channel.SendMessageAsync(userText);
}
I hit this same issue building my first Discord bot last year. Users kept triggering the command without arguments and it’d throw exceptions. The null check and default parameter stop crashes and make things smoother for users. [Remainder] is definitely the way to go for multi-word input.
Everyone’s right about [Remainder] but honestly, C# Discord bots get annoying fast once you move past basic commands.
I used to write loads of C# bot code until I realized I was just shuffling data around. Bot gets a message, processes it, maybe hits an API or database, then responds.
Now I use Latenode workflows for Discord interactions instead. Set up a webhook endpoint for Discord to call, build your logic visually in the workflow builder, send responses back through Discord’s API.
Way more flexible than command frameworks. Need to store user data? Connect a database node. External API calls? Drop in an HTTP request node. Complex logic for user roles or message content? Use conditional nodes.
No more fighting with command parameters, error handling, or deployment headaches. Build the workflow and let it run.
The [Remainder] attribute does the magic - it captures everything after the command as one string, spaces included.
Honestly though, Discord bots get messy fast once you start adding more commands and responses. Been down that road with multiple bots.
I switched to using Latenode for all the heavy lifting behind my Discord bots. You can build workflows that handle Discord webhooks, hit databases, call APIs, and send responses back to channels.
Best part? You don’t need tons of C# code for complex bot behaviors. Just connect Discord to your workflows and let Latenode do the work. Way cleaner than managing command handlers and state in your bot code.
To capture the user’s input correctly, modify your method to accept a string parameter decorated with the [Remainder] attribute. This attribute lets the bot capture all text after the command as a single string:
Without using [Remainder], you will only receive the first word after the command. I’ve encountered this issue before and it can be quite frustrating, so using this attribute is essential. Ensure to implement null checks to prevent potential crashes if users invoke the command without additional text.
yeah, [Remainder] is what u need, but just a heads up - if someone types !repeat with no text after, ur bot will crash. learned that the hard way haha. make sure to check if userText is null before sending it back or exceptions will pop up everywhere.