How to capture user input for Discord bot commands in C#

I just migrated my Discord bot to discord.NET version 1.0 and I’m working on creating a basic repeat command. The bot should take whatever text the user types after the command and send it back to the same channel.

I can’t figure out how to properly capture the user’s input as a string parameter. When I try to access the message content, I get errors. Here’s what I have so far:

[Command("repeat")]
private async Task RepeatMessage()
{
    string userText = // Not sure what goes here
    await Context.Channel.SendMessageAsync(userText);
}

What’s the correct way to get the message text that comes after the command? I’ve been searching through the documentation but I’m still confused about the proper syntax for capturing command parameters.

Your command method needs a parameter to grab the text after the command. Just add a string parameter and Discord.NET will automatically parse whatever’s left in the message.

Here’s the fix:

[Command("repeat")]
private async Task RepeatMessage([Remainder] string userText)
{
    await Context.Channel.SendMessageAsync(userText);
}

The [Remainder] attribute is key - it grabs all the remaining text as one string, spaces included. Without it, you’d only get the first word. I hit this exact problem when I started with Discord.NET and wasted hours debugging before finding the Remainder attribute. Also make sure to handle cases where no text is provided so you don’t get null reference exceptions.

there’s actually a simpler way if you just want the raw message text. skip the parsing and grab context.message.content directly, then strip the command prefix yourself. try string userText = context.message.content.Substring(8); where 8 covers the length of "!repeat " with the space. more manual but works great for basic commands.

Here’s another approach if you need more control over the input. Use multiple parameters instead of the Remainder attribute - gives you more flexibility for complex commands.

[Command("repeat")]
private async Task RepeatMessage(params string[] words)
{
    string userText = string.Join(" ", words);
    await Context.Channel.SendMessageAsync(userText);
}

This captures each word separately and rebuilds the message. Really useful when you want to process individual words or validate input first. I use this pattern for commands that need word counting or filtering. Don’t forget null checking though - users might send the command without any text.