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
Add DiscordShardedClient sample project & Client FAQ entry. #1177
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a53dd4b
Add DiscordShardedClient sample project & Client FAQ entry.
AntiTcb c848667
Revise language, fix typo, add xrefs
AntiTcb 51b0914
Adjust placement of message handler.
AntiTcb fc7ac80
Resolve DI issue with initialized client; properly initialize command…
AntiTcb File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<RootNamespace>_03_sharded_client</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Discord.Net.Commands\Discord.Net.Commands.csproj" /> | ||
<ProjectReference Include="..\..\src\Discord.Net.WebSocket\Discord.Net.WebSocket.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Threading.Tasks; | ||
using Discord.Commands; | ||
|
||
namespace _03_sharded_client.Modules | ||
{ | ||
// Remember to make your module reference the ShardedCommandContext | ||
public class PublicModule : ModuleBase<ShardedCommandContext> | ||
{ | ||
[Command("info")] | ||
public async Task InfoAsync() | ||
{ | ||
var msg = $@"Hi {Context.User}! There are currently {Context.Client.Shards} shards! | ||
This guild is being served by shard number {Context.Client.GetShardFor(Context.Guild).ShardId}"; | ||
await ReplyAsync(msg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using _03_sharded_client.Services; | ||
using Discord; | ||
using Discord.Commands; | ||
using Discord.WebSocket; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace _03_sharded_client | ||
{ | ||
// This is a minimal example of using Discord.Net's Sharded Client | ||
// The provided DiscordShardedClient class simplifies having multiple | ||
// DiscordSocketClient instances (or shards) to serve a large number of guilds. | ||
class Program | ||
{ | ||
private DiscordShardedClient _client; | ||
|
||
static void Main(string[] args) | ||
=> new Program().MainAsync().GetAwaiter().GetResult(); | ||
public async Task MainAsync() | ||
{ | ||
// You specify the amount of shards you'd like to have with the | ||
// DiscordSocketConfig. Generally, it's recommended to | ||
// have 1 shard per 1500-2000 guilds your bot is in. | ||
var config = new DiscordSocketConfig | ||
{ | ||
TotalShards = 2 | ||
}; | ||
|
||
_client = new DiscordShardedClient(config); | ||
var services = ConfigureServices(); | ||
|
||
// The Sharded Client does not have a Ready event. | ||
// The ShardReady event is used instead, allowing for individual | ||
// control per shard. | ||
_client.ShardReady += ReadyAsync; | ||
_client.Log += LogAsync; | ||
|
||
await services.GetRequiredService<CommandHandlingService>().InitializeAsync(); | ||
|
||
await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); | ||
await _client.StartAsync(); | ||
|
||
await Task.Delay(-1); | ||
} | ||
|
||
private IServiceProvider ConfigureServices() | ||
{ | ||
return new ServiceCollection() | ||
.AddSingleton(_client) | ||
.AddSingleton<CommandService>() | ||
.AddSingleton<CommandHandlingService>() | ||
.BuildServiceProvider(); | ||
} | ||
|
||
|
||
private Task ReadyAsync(DiscordSocketClient shard) | ||
{ | ||
Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!"); | ||
return Task.CompletedTask; | ||
} | ||
|
||
private Task LogAsync(LogMessage log) | ||
{ | ||
Console.WriteLine(log.ToString()); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
samples/03_sharded_client/Services/CommandHandlingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Discord; | ||
using Discord.Commands; | ||
using Discord.WebSocket; | ||
|
||
namespace _03_sharded_client.Services | ||
{ | ||
public class CommandHandlingService | ||
{ | ||
private readonly CommandService _commands; | ||
private readonly DiscordShardedClient _discord; | ||
private readonly IServiceProvider _services; | ||
|
||
public CommandHandlingService(IServiceProvider services) | ||
{ | ||
_commands = services.GetRequiredService<CommandService>(); | ||
_discord = services.GetRequiredService<DiscordShardedClient>(); | ||
_services = services; | ||
} | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services); | ||
_discord.MessageReceived += MessageReceivedAsync; | ||
} | ||
|
||
public async Task MessageReceivedAsync(SocketMessage rawMessage) | ||
{ | ||
// Ignore system messages, or messages from other bots | ||
if (!(rawMessage is SocketUserMessage message)) | ||
return; | ||
if (message.Source != MessageSource.User) | ||
return; | ||
|
||
// This value holds the offset where the prefix ends | ||
var argPos = 0; | ||
if (!message.HasMentionPrefix(_discord.CurrentUser, ref argPos)) | ||
return; | ||
|
||
// A new kind of command context, ShardedCommandContext can be utilized with the commands framework | ||
var context = new ShardedCommandContext(_discord, message); | ||
var result = await _commands.ExecuteAsync(context, argPos, _services); | ||
|
||
if (result.Error.HasValue && | ||
result.Error.Value != CommandError.UnknownCommand) // it's bad practice to send 'unknown command' errors | ||
await context.Channel.SendMessageAsync(result.ToString()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All
[]
references are not linked correctly. Add corresponding xrefs at the bottom of the section.e.g.,
Current Snapshot