Skip to content

Commit 6ffa838

Browse files
committed
Squash
1 parent 01ad2e0 commit 6ffa838

File tree

15 files changed

+578
-2
lines changed

15 files changed

+578
-2
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,8 @@ FodyWeavers.xsd
364364

365365
# Settings file
366366
appsettings.json
367-
!Documentation/**/appsettings.json
367+
!Documentation/**/appsettings.json
368+
!ProjectTemplates/**/appsettings.json
369+
370+
# Rider settings folder
371+
.idea/

NetCord.slnx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,5 @@
8282
</Folder>
8383
<Project Path="NetCord.Services/NetCord.Services.csproj" />
8484
<Project Path="NetCord/NetCord.csproj" />
85-
</Solution>
85+
<Project Path="ProjectTemplates\NetCordTemplates.csproj" Type="Classic C#" />
86+
</Solution>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<PackageType>Template</PackageType>
5+
<PackageVersion>1.0</PackageVersion>
6+
<PackageId>NetCord.Templates</PackageId>
7+
<TargetFramework>net8.0</TargetFramework>
8+
<LangVersion>latest</LangVersion>
9+
10+
<ImplicitUsings>enable</ImplicitUsings>
11+
<Nullable>enable</Nullable>
12+
<NoWarn>$(NoWarn);NU5128</NoWarn>
13+
14+
<IncludeContentInPack>true</IncludeContentInPack>
15+
<IncludeBuildOutput>false</IncludeBuildOutput>
16+
<ContentTargetFolders>content</ContentTargetFolders>
17+
18+
<Title>NetCord Project Templates</Title>
19+
<Authors>NetCord</Authors>
20+
<Description>Starting templates to use when creating NetCord bot.</Description>
21+
<RepositoryUrl>https://github.com/NetCordDev/NetCord</RepositoryUrl>
22+
<PackageProjectUrl>https://netcord.dev</PackageProjectUrl>
23+
<PackageTags>dotnet-new;templates;netcord;bot;discord;discord-api</PackageTags>
24+
</PropertyGroup>
25+
26+
<ItemGroup>
27+
<Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**;templates\**\.idea\**" />
28+
<Compile Remove="**\*" />
29+
</ItemGroup>
30+
31+
</Project>

ProjectTemplates/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
### Convenient TL;DR one-liner
2+
3+
> [!WARNING]
4+
> TODO: Add actual command to install from nuget
5+
6+
Minimal template
7+
```bash
8+
dotnet new install NetCord.Templates && \
9+
dotnet new netcord --name MyNewBot --framework net9.0
10+
```
11+
Or if you want complete template with all features
12+
```bash
13+
dotnet new install NetCord.Templates && \
14+
dotnet new netcord --add-text-commands --add-application-commands --add-component-interactions --name MyNewBot --framework net9.0
15+
```
16+
17+
18+
> [!IMPORTANT]
19+
> In case of local testing,
20+
> all commands should be run from the root of the repository.
21+
22+
### Packing
23+
```bash
24+
dotnet pack ./ProjectTemplates/ -c Release -o ./nupkgs
25+
```
26+
27+
### Installing
28+
Nuget
29+
30+
> [!WARNING]
31+
> TODO: Add actual command to install from nuget
32+
33+
```bash
34+
dotnet new install NetCord.Templates
35+
```
36+
Local
37+
```bash
38+
dotnet new install ./nupkgs/*
39+
```
40+
41+
### Uninstalling
42+
```bash
43+
dotnet new uninstall NetCord.Templates
44+
```
45+
46+
### Updating
47+
```bash
48+
dotnet new update
49+
```
50+
51+
### Usage
52+
`dotnet new netcord [options] [template options]`
53+
Use `dotnet new netcord --help` to see all available options.
54+
Example command:
55+
```bash
56+
dotnet new netcord \
57+
--add-text-commands \
58+
--add-application-commands \
59+
--add-component-interactions \
60+
--name MyNewBot \
61+
--framework net9.0
62+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"$schema": "https://json.schemastore.org/dotnetcli.host",
3+
"symbolInfo": {
4+
"kestrelHttpPort": {
5+
"isHidden": true
6+
},
7+
"kestrelHttpsPort": {
8+
"isHidden": true
9+
},
10+
"framework": {
11+
"longName": "framework"
12+
},
13+
"skipRestore": {
14+
"longName": "no-restore",
15+
"shortName": ""
16+
},
17+
"excludeLaunchSettings": {
18+
"longName": "exclude-launch-settings",
19+
"shortName": ""
20+
},
21+
"addApplicationCommands": {
22+
"longName": "add-application-commands",
23+
"shortName": ""
24+
},
25+
"addTextCommands": {
26+
"longName": "add-text-commands",
27+
"shortName": ""
28+
},
29+
"addComponentInteractions": {
30+
"longName": "add-component-interactions",
31+
"shortName": ""
32+
}
33+
},
34+
"usageExamples": [
35+
"--add-text-commands --add-interactions --add-application-commands --name MyNewBot --framework net9.0"
36+
]
37+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
{
2+
"$schema": "http://json.schemastore.org/template",
3+
"author": "NetCord",
4+
"classifications": [
5+
"Web",
6+
"NetCord",
7+
"AspNetCore"
8+
],
9+
"identity": "NetCord.Hosting.AspNetCore",
10+
"groupIdentity": "NetCord",
11+
"name": "NetCord Project",
12+
"defaultName": "NetCordBot",
13+
"sourceName": "NetCord.Template.Bot",
14+
"shortName": "netcord",
15+
"description": "NetCord Bot with module-based approach",
16+
"preferNameDirectory": true,
17+
"tags": {
18+
"language": "C#",
19+
"type": "project"
20+
},
21+
"primaryOutputs": [
22+
{
23+
"path": "NetCord.Template.Bot.csproj"
24+
}
25+
],
26+
"sources": [
27+
{
28+
"source": "./",
29+
"target": "./",
30+
"exclude": [
31+
".template.config/**",
32+
"**/*.idea/**"
33+
],
34+
"modifiers": [
35+
{
36+
"condition": "(excludeLaunchSettings)",
37+
"exclude": [ "Properties/launchSettings.json" ]
38+
},
39+
{
40+
"condition": "(!addApplicationCommands)",
41+
"exclude": [
42+
"SlashCommands/HelloSlashModule.cs"
43+
]
44+
},
45+
{
46+
"condition": "(!addTextCommands)",
47+
"exclude": [
48+
"TextCommands/HelloTextModule.cs"
49+
]
50+
},
51+
{
52+
"condition": "(!addComponentInteractions)",
53+
"exclude": [
54+
"Interactions/ButtonInteractionModule.cs",
55+
"SlashCommands/ButtonSlashModule.cs"
56+
]
57+
}
58+
]
59+
}
60+
],
61+
"symbols": {
62+
"framework": {
63+
"type": "parameter",
64+
"description": "The target framework for the project.",
65+
"datatype": "choice",
66+
"choices": [
67+
{
68+
"choice": "net8.0",
69+
"description": "Targets .NET 8"
70+
},
71+
{
72+
"choice": "net9.0",
73+
"description": "Targets .NET 9"
74+
}
75+
],
76+
"defaultValue": "net8.0",
77+
"replaces": "net8.0"
78+
},
79+
"addApplicationCommands": {
80+
"type": "parameter",
81+
"datatype": "bool",
82+
"defaultValue": "false",
83+
"displayName": "Add Application Commands",
84+
"description": "Whether to add application commands with example module"
85+
"isRequired": false
86+
},
87+
"addTextCommands": {
88+
"type": "parameter",
89+
"datatype": "bool",
90+
"defaultValue": "false",
91+
"displayName": "Add Text Commands",
92+
"description": "Whether to add text commands with example module"
93+
"isRequired": false
94+
},
95+
"addComponentInteractions": {
96+
"type": "parameter",
97+
"datatype": "bool",
98+
"defaultValue": "false",
99+
"displayName": "Add Interactions",
100+
"description": "Whether to add interactions with example module"
101+
"isRequired": false
102+
},
103+
"hostIdentifier": {
104+
"type": "bind",
105+
"binding": "HostIdentifier"
106+
},
107+
"skipRestore": {
108+
"type": "parameter",
109+
"datatype": "bool",
110+
"description": "Whether to skip automatic restore of the project on create.",
111+
"defaultValue": "false",
112+
"displayName": "Skip restore",
113+
"isRequired": false
114+
},
115+
"excludeLaunchSettings": {
116+
"type": "parameter",
117+
"datatype": "bool",
118+
"defaultValue": "false",
119+
"description": "Whether to exclude launchSettings.json from the generated template."
120+
},
121+
"kestrelHttpPort": {
122+
"type": "parameter",
123+
"datatype": "integer",
124+
"description": "Port number to use for the HTTP endpoint in launchSettings.json."
125+
},
126+
"kestrelHttpPortGenerated": {
127+
"type": "generated",
128+
"generator": "port",
129+
"parameters": {
130+
"low": 5000,
131+
"high": 5300
132+
}
133+
},
134+
"kestrelHttpPortReplacer": {
135+
"type": "generated",
136+
"generator": "coalesce",
137+
"parameters": {
138+
"sourceVariableName": "kestrelHttpPort",
139+
"fallbackVariableName": "kestrelHttpPortGenerated"
140+
},
141+
"replaces": "5500"
142+
},
143+
"kestrelHttpsPort": {
144+
"type": "parameter",
145+
"datatype": "integer",
146+
"description": "Port number to use for the HTTPS endpoint in launchSettings.json. This option is only applicable when the parameter no-https is not used (no-https will be ignored if Individual auth is used)."
147+
},
148+
"kestrelHttpsPortGenerated": {
149+
"type": "generated",
150+
"generator": "port",
151+
"parameters": {
152+
"low": 7000,
153+
"high": 7300
154+
}
155+
},
156+
"kestrelHttpsPortReplacer": {
157+
"type": "generated",
158+
"generator": "coalesce",
159+
"parameters": {
160+
"sourceVariableName": "kestrelHttpsPort",
161+
"fallbackVariableName": "kestrelHttpsPortGenerated"
162+
},
163+
"replaces": "5501"
164+
}
165+
},
166+
"postActions": [
167+
{
168+
"id": "restore",
169+
"condition": "(!skipRestore)",
170+
"description": "Restore NuGet packages.",
171+
"manualInstructions": [
172+
{
173+
"text": "Run 'dotnet restore'"
174+
}
175+
],
176+
"actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025",
177+
"continueOnError": true
178+
}
179+
]
180+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using NetCord.Rest;
2+
using NetCord.Services.ComponentInteractions;
3+
4+
namespace NetCord.Template.Bot.Interactions;
5+
6+
/// <summary>
7+
/// Example Button interaction module.
8+
/// To learn more about interactions, check out <see href="https://netcord.dev/guides/services/component-interactions/introduction.html?tabs=generic-host">docs</see>.
9+
/// </summary>
10+
/// <param name="logger">Logger injected via DI</param>
11+
public class ButtonModule(ILogger<ButtonModule> logger) : ComponentInteractionModule<ButtonInteractionContext>
12+
{
13+
/// <summary>
14+
/// Replies with deferred message to the button click.
15+
/// Don't forget that CustomId must match the one in the button.
16+
/// </summary>
17+
/// <returns></returns>
18+
[ComponentInteraction("pong-interaction")]
19+
public async Task PingButton()
20+
{
21+
logger.LogInformation("Received {Interaction} from {User}", Context.Interaction.Data.CustomId, Context.User.Username);
22+
var received = DateTime.Now;
23+
var callback = InteractionCallback.DeferredModifyMessage;
24+
await Context.Interaction.SendResponseAsync(callback).ConfigureAwait(false);
25+
26+
await Task.Delay(10 * 1_000).ConfigureAwait(true); // Simulate some work
27+
28+
await Context.Interaction.ModifyResponseAsync(m =>
29+
{
30+
m.Components =
31+
[
32+
new ComponentContainerProperties
33+
{
34+
AccentColor = new Color(0, 255, 0),
35+
Components =
36+
[
37+
new TextDisplayProperties("# Pong!"),
38+
new TextDisplayProperties($"I've received your interaction at `{received:T}` and replied at `{DateTime.Now:T}`"),
39+
new TextDisplayProperties("-# This was example of deferred component interaction response"),
40+
new TextDisplayProperties("-# Which also used [ComponentsV2](https://discord.com/developers/docs/components/overview) over regular embeds."),
41+
]
42+
}
43+
];
44+
}).ConfigureAwait(false);
45+
}
46+
}

0 commit comments

Comments
 (0)