Skip to content

Switch to Commands for basic example #13

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 7 commits into
base: 5.0.0
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
2 changes: 1 addition & 1 deletion Example-Bots.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DSharpPlus.Examples.Compone
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DSharpPlus.Examples.Components.Modals", "src\DSharpPlus.Examples.Components.Modals\DSharpPlus.Examples.Components.Modals.csproj", "{C5197139-4026-4EE9-A301-136FC3E10DA3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DSharpPlus.Examples.CommandsNext.Basics", "src\DSharpPlus.Examples.CommandsNext.Basics\DSharpPlus.Examples.CommandsNext.Basics.csproj", "{7135C15F-A8FD-4CEB-BA07-ED45FFBFFFF5}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DSharpPlus.Examples.Commands.Basics", "src\DSharpPlus.Examples.Commands.Basics\DSharpPlus.Examples.Commands.Basics.csproj", "{7135C15F-A8FD-4CEB-BA07-ED45FFBFFFF5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DSharpPlus.Examples.CommandsNext.CommandArguments", "src\DSharpPlus.Examples.CommandsNext.CommandArguments\DSharpPlus.Examples.CommandsNext.CommandArguments.csproj", "{1159DF74-2834-4A7B-97C2-384C8578F6A6}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions src/DSharpPlus.Examples.Commands.Basics/Commands/PingCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;
using DSharpPlus.Commands.Processors.TextCommands.Attributes;
using DSharpPlus.Commands.Trees;
using DSharpPlus.Commands.Trees.Attributes;

namespace DSharpPlus.Examples.Commands.Basics
{
public sealed class PingCommand
{
[Command("ping"), TextAlias("pong")]
public static ValueTask ExecuteAsync(CommandContext context) => context.RespondAsync($"Pong! Latency is {context.Client.Ping}ms.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>DSharpPlus.Examples.Commands.Basics</RootNamespace>
</PropertyGroup>
<ItemGroup>
<None Include="./config.json" CopyToOutputDirectory="PreserveNewest" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01982" />
<PackageReference Include="DSharpPlus.Commands" Version="5.0.0-nightly-01982" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
</ItemGroup>
</Project>
57 changes: 57 additions & 0 deletions src/DSharpPlus.Examples.Commands.Basics/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using DSharpPlus.Commands;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Commands.Processors.TextCommands;
using DSharpPlus.Commands.Processors.TextCommands.Parsing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace DSharpPlus.Examples.Commands.Basics
{
public static class Program
{
public static async Task Main(string[] args)
{
ConfigurationBuilder configurationBuilder = new();
// you can also use environment vars, just uncomment this
// configurationBuilder.AddEnvironmentVariables("");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// right click on config.json, click properties, and in Copy to Output Directory select Copy if newer

configurationBuilder.AddJsonFile("config.json", true, true);
configurationBuilder.AddCommandLine(args);

IConfiguration configuration = configurationBuilder.Build();
ServiceCollection serviceCollection = new();
serviceCollection.AddSingleton(configuration);
Assembly currentAssembly = typeof(Program).Assembly;

serviceCollection.AddSingleton(async serviceProvider =>
{
DiscordClient client = new(new DiscordConfiguration()
{
Token = configuration.GetValue<string>("example_bot:token") ??
throw new InvalidOperationException("Missing Discord token."),
Intents = DiscordIntents.AllUnprivileged | DiscordIntents.MessageContents | TextCommandProcessor.RequiredIntents
});

CommandsExtension extension = client.UseCommands(new()
{
DebugGuildId = configuration.GetValue<ulong?>("example_bot:debug_guild_id", null),
ServiceProvider = serviceProvider,

});
await extension.AddProcessorAsync(new TextCommandProcessor());
await extension.AddProcessorAsync(new SlashCommandProcessor());
extension.AddCommands(currentAssembly);

return client;
});
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

DiscordClient client = await serviceProvider.GetRequiredService<Task<DiscordClient>>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this async

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asked by lunar in last review

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OoLunar why

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#13 (comment)
i think this comment is meant which was to a different part of the code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is intended. Adding processors is an asynchronous operation. Encouraging .GetAwaiter().GetResult() goes against everything I've worked on within the lib, especially when it's done on ValueTasks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do the whole setup outside the AddSinglton and then add the client

await client.ConnectAsync();

await Task.Delay(-1);
}
}
}
6 changes: 6 additions & 0 deletions src/DSharpPlus.Examples.Commands.Basics/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"example_bot": {
"token": "bottoken",
"debug_guild_id": ""
}
}

This file was deleted.

This file was deleted.

This file was deleted.

52 changes: 0 additions & 52 deletions src/DSharpPlus.Examples.CommandsNext.Basics/Program.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-01970" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-01970" />
<PackageReference Include="Ulid" Version="1.3.3" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-01970" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="5.0.0-nightly-01970" />
<PackageReference Include="Ulid" Version="1.3.3" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01788" />
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-01970" />
</ItemGroup>
</Project>