Skip to content

chore: Add dotnet new templates #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,8 @@ FodyWeavers.xsd

# Settings file
appsettings.json
!Documentation/**/appsettings.json
!Documentation/**/appsettings.json
!ProjectTemplates/**/appsettings.json

# Rider settings folder
.idea/
3 changes: 2 additions & 1 deletion NetCord.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@
</Folder>
<Project Path="NetCord.Services/NetCord.Services.csproj" />
<Project Path="NetCord/NetCord.csproj" />
</Solution>
<Project Path="ProjectTemplates\NetCordTemplates.csproj" Type="Classic C#" />
</Solution>
31 changes: 31 additions & 0 deletions ProjectTemplates/NetCordTemplates.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageType>Template</PackageType>
<PackageVersion>1.0</PackageVersion>
<PackageId>NetCord.Templates</PackageId>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>

<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);NU5128</NoWarn>

<IncludeContentInPack>true</IncludeContentInPack>
<IncludeBuildOutput>false</IncludeBuildOutput>
<ContentTargetFolders>content</ContentTargetFolders>

<Title>NetCord Project Templates</Title>
<Authors>NetCord</Authors>
<Description>Starting templates to use when creating NetCord bot.</Description>
<RepositoryUrl>https://github.com/NetCordDev/NetCord</RepositoryUrl>
<PackageProjectUrl>https://netcord.dev</PackageProjectUrl>
<PackageTags>dotnet-new;templates;netcord;bot;discord;discord-api</PackageTags>
</PropertyGroup>

<ItemGroup>
<Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**;templates\**\.idea\**" />
<Compile Remove="**\*" />
</ItemGroup>

</Project>
62 changes: 62 additions & 0 deletions ProjectTemplates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
### Convenient TL;DR one-liner

> [!WARNING]
> TODO: Add actual command to install from nuget

Minimal template
```bash
dotnet new install NetCord.Templates && \
dotnet new netcord --name MyNewBot --framework net9.0
```
Or if you want complete template with all features
```bash
dotnet new install NetCord.Templates && \
dotnet new netcord --add-text-commands --add-application-commands --add-component-interactions --name MyNewBot --framework net9.0
```


> [!IMPORTANT]
> In case of local testing,
> all commands should be run from the root of the repository.

### Packing
```bash
dotnet pack ./ProjectTemplates/ -c Release -o ./nupkgs
```

### Installing
Nuget

> [!WARNING]
> TODO: Add actual command to install from nuget

```bash
dotnet new install NetCord.Templates
```
Local
```bash
dotnet new install ./nupkgs/*
```

### Uninstalling
```bash
dotnet new uninstall NetCord.Templates
```

### Updating
```bash
dotnet new update
```

### Usage
`dotnet new netcord [options] [template options]`
Use `dotnet new netcord --help` to see all available options.
Example command:
```bash
dotnet new netcord \
--add-text-commands \
--add-application-commands \
--add-component-interactions \
--name MyNewBot \
--framework net9.0
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "https://json.schemastore.org/dotnetcli.host",
"symbolInfo": {
"kestrelHttpPort": {
"isHidden": true
},
"kestrelHttpsPort": {
"isHidden": true
},
"framework": {
"longName": "framework"
},
"skipRestore": {
"longName": "no-restore",
"shortName": ""
},
"excludeLaunchSettings": {
"longName": "exclude-launch-settings",
"shortName": ""
},
"addApplicationCommands": {
"longName": "add-application-commands",
"shortName": ""
},
"addTextCommands": {
"longName": "add-text-commands",
"shortName": ""
},
"addComponentInteractions": {
"longName": "add-component-interactions",
"shortName": ""
}
},
"usageExamples": [
"--add-text-commands --add-interactions --add-application-commands --name MyNewBot --framework net9.0"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "NetCord",
"classifications": [
"Web",
"NetCord",
"AspNetCore"
],
"identity": "NetCord.Hosting.AspNetCore",
"groupIdentity": "NetCord",
"name": "NetCord Project",
"defaultName": "NetCordBot",
"sourceName": "NetCord.Template.Bot",
"shortName": "netcord",
"description": "NetCord Bot with module-based approach",
"preferNameDirectory": true,
"tags": {
"language": "C#",
"type": "project"
},
"primaryOutputs": [
{
"path": "NetCord.Template.Bot.csproj"
}
],
"sources": [
{
"source": "./",
"target": "./",
"exclude": [
".template.config/**",
"**/*.idea/**"
],
"modifiers": [
{
"condition": "(excludeLaunchSettings)",
"exclude": [ "Properties/launchSettings.json" ]
},
{
"condition": "(!addApplicationCommands)",
"exclude": [
"SlashCommands/HelloSlashModule.cs"
]
},
{
"condition": "(!addTextCommands)",
"exclude": [
"TextCommands/HelloTextModule.cs"
]
},
{
"condition": "(!addComponentInteractions)",
"exclude": [
"Interactions/ButtonInteractionModule.cs",
"SlashCommands/ButtonSlashModule.cs"
]
}
]
}
],
"symbols": {
"framework": {
"type": "parameter",
"description": "The target framework for the project.",
"datatype": "choice",
"choices": [
{
"choice": "net8.0",
"description": "Targets .NET 8"
},
{
"choice": "net9.0",
"description": "Targets .NET 9"
}
],
"defaultValue": "net8.0",
"replaces": "net8.0"
},
"addApplicationCommands": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"displayName": "Add Application Commands",
"description": "Whether to add application commands with example module"
"isRequired": false
},
"addTextCommands": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"displayName": "Add Text Commands",
"description": "Whether to add text commands with example module"
"isRequired": false
},
"addComponentInteractions": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"displayName": "Add Interactions",
"description": "Whether to add interactions with example module"
"isRequired": false
},
"hostIdentifier": {
"type": "bind",
"binding": "HostIdentifier"
},
"skipRestore": {
"type": "parameter",
"datatype": "bool",
"description": "Whether to skip automatic restore of the project on create.",
"defaultValue": "false",
"displayName": "Skip restore",
"isRequired": false
},
"excludeLaunchSettings": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"description": "Whether to exclude launchSettings.json from the generated template."
},
"kestrelHttpPort": {
"type": "parameter",
"datatype": "integer",
"description": "Port number to use for the HTTP endpoint in launchSettings.json."
},
"kestrelHttpPortGenerated": {
"type": "generated",
"generator": "port",
"parameters": {
"low": 5000,
"high": 5300
}
},
"kestrelHttpPortReplacer": {
"type": "generated",
"generator": "coalesce",
"parameters": {
"sourceVariableName": "kestrelHttpPort",
"fallbackVariableName": "kestrelHttpPortGenerated"
},
"replaces": "5500"
},
"kestrelHttpsPort": {
"type": "parameter",
"datatype": "integer",
"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)."
},
"kestrelHttpsPortGenerated": {
"type": "generated",
"generator": "port",
"parameters": {
"low": 7000,
"high": 7300
}
},
"kestrelHttpsPortReplacer": {
"type": "generated",
"generator": "coalesce",
"parameters": {
"sourceVariableName": "kestrelHttpsPort",
"fallbackVariableName": "kestrelHttpsPortGenerated"
},
"replaces": "5501"
}
},
"postActions": [
{
"id": "restore",
"condition": "(!skipRestore)",
"description": "Restore NuGet packages.",
"manualInstructions": [
{
"text": "Run 'dotnet restore'"
}
],
"actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025",
"continueOnError": true
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using NetCord.Rest;
using NetCord.Services.ComponentInteractions;

namespace NetCord.Template.Bot.Interactions;

/// <summary>
/// Example Button interaction module.
/// To learn more about interactions, check out <see href="https://netcord.dev/guides/services/component-interactions/introduction.html?tabs=generic-host">docs</see>.
/// </summary>
/// <param name="logger">Logger injected via DI</param>
public class ButtonModule(ILogger<ButtonModule> logger) : ComponentInteractionModule<ButtonInteractionContext>
{
/// <summary>
/// Replies with deferred message to the button click.
/// Don't forget that CustomId must match the one in the button.
/// </summary>
/// <returns></returns>
[ComponentInteraction("pong-interaction")]
public async Task PingButton()
{
logger.LogInformation("Received {Interaction} from {User}", Context.Interaction.Data.CustomId, Context.User.Username);
var received = DateTime.Now;
var callback = InteractionCallback.DeferredModifyMessage;
await Context.Interaction.SendResponseAsync(callback).ConfigureAwait(false);

await Task.Delay(10 * 1_000).ConfigureAwait(true); // Simulate some work

await Context.Interaction.ModifyResponseAsync(m =>
{
m.Components =
[
new ComponentContainerProperties
{
AccentColor = new Color(0, 255, 0),
Components =
[
new TextDisplayProperties("# Pong!"),
new TextDisplayProperties($"I've received your interaction at `{received:T}` and replied at `{DateTime.Now:T}`"),
new TextDisplayProperties("-# This was example of deferred component interaction response"),
new TextDisplayProperties("-# Which also used [ComponentsV2](https://discord.com/developers/docs/components/overview) over regular embeds."),
]
}
];
}).ConfigureAwait(false);
}
}
Loading