whats this at the top?

i was doing some tutorial code on the discord.NET introduction section of the docs and i found they have this:
public class InfoMdoule : ...
{
[Command("Say")] // < what's this?
[Summary("Echoes a message.")] // < what's this?
public async Task Say([Remainder] [Summary("The text to echo back.")] string echo)
{
await ReplyAsync(echo);
}
}
public class InfoMdoule : ...
{
[Command("Say")] // < what's this?
[Summary("Echoes a message.")] // < what's this?
public async Task Say([Remainder] [Summary("The text to echo back.")] string echo)
{
await ReplyAsync(echo);
}
}
20 Replies
it’s raining outside
is this the C# equivalent of a python wrapper? and what would be the use cases for such a thing?
jcotton42
jcotton424mo ago
It's an attribute, not a decorator.
Angius
Angius4mo ago
$structure
MODiX
MODiX4mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;

int LocalMethod(string param) { return 3; }
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;

int LocalMethod(string param) { return 3; }
}
}
jcotton42
jcotton424mo ago
It's basically custom metadata you can add onto types, methods, properties, fields. They can be looked up with reflection.
Angius
Angius4mo ago
Or source generators
Jimmacle
Jimmacle4mo ago
yeah, in this case those attributes give discord.NET more information about what to do with your code
reflectronic
reflectronic4mo ago
Tutorial: Define and read custom attributes. - C#
Learn how attributes work in C#. You define custom attributes to add metadata to your code. You read those attributes to learn about the code at runtime
Jimmacle
Jimmacle4mo ago
in this case, treat your method as a command called "say" with a summary, and to use the remainder of the message as the argument to the method
The Fog from Human Resources
:SCshocked: btw the tutorial youre watching is outdated [Command] has long been replaced with [SlashCommand]
it’s raining outside
i know the difference between prefix and slash commands ah ok i think i understand now
The Fog from Human Resources
its more than that
it’s raining outside
oh?
The Fog from Human Resources
it introduces the Interaction framework which also opens the way to [ComponentInteraction], [ModalInteraction], proper params and some other stuff the probably easiest way to make a proper discord bot
it’s raining outside
ah ok so just do slash commands cuz they have more features?
The Fog from Human Resources
You do slash commands because they are more user friendly and have been accepted as the new default At the time bot verification still meant something discord also stopped verifying bots that used text commands
it’s raining outside
:prettythumbsup: slash command does it still face the syncing problem in dpy where you have to refresh your discord client for new things to appear?
The Fog from Human Resources
only on rare occasions but thats more of a discord issue a thing i like to do is make a help command that lists all commands as clickable then i dont need to wait for discord to update my cache but its overkill so btw you dont need to force close discord you can just do CTRL + R
it’s raining outside
yeah i know that 👌
Want results from more Discord servers?
Add your server