C
C#•6mo ago
slowly losing it

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
slowly losing it
slowly losing itOP•6mo ago
is this the C# equivalent of a python wrapper? and what would be the use cases for such a thing?
jcotton42
jcotton42•6mo ago
It's an attribute, not a decorator.
Angius
Angius•6mo ago
$structure
MODiX
MODiX•6mo 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
jcotton42•6mo ago
It's basically custom metadata you can add onto types, methods, properties, fields. They can be looked up with reflection.
Angius
Angius•6mo ago
Or source generators
Jimmacle
Jimmacle•6mo ago
yeah, in this case those attributes give discord.NET more information about what to do with your code
reflectronic
reflectronic•6mo 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
Jimmacle•6mo 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]
slowly losing it
slowly losing itOP•6mo ago
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
slowly losing it
slowly losing itOP•6mo ago
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
slowly losing it
slowly losing itOP•6mo ago
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
slowly losing it
slowly losing itOP•6mo ago
: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
slowly losing it
slowly losing itOP•6mo ago
yeah i know that 👌

Did you find this page helpful?