FAQ rework, replacing outdated info, better interaction FAQ (#2106)
* FAQ rework, replacing outdated info, better interaction faq * Update docs/faq/basics/getting-started.md Co-authored-by: Jared L <48422312+lhjt@users.noreply.github.com> * Update docs/faq/basics/getting-started.md Co-authored-by: Jared L <48422312+lhjt@users.noreply.github.com> * Update docs/faq/int_framework/general.md Co-authored-by: Jared L <48422312+lhjt@users.noreply.github.com> * fix TOC reference Co-authored-by: Jared L <48422312+lhjt@users.noreply.github.com> Co-authored-by: Quin Lynch <lynchquin@gmail.com>
This commit is contained in:
28
docs/faq/basics/samples/DI.cs
Normal file
28
docs/faq/basics/samples/DI.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
public class MyService
|
||||
{
|
||||
public string MyCoolString { get; set; }
|
||||
}
|
||||
public class Setup
|
||||
{
|
||||
public IServiceProvider BuildProvider() =>
|
||||
new ServiceCollection()
|
||||
.AddSingleton<MyService>()
|
||||
.BuildServiceProvider();
|
||||
}
|
||||
public class MyModule : ModuleBase<SocketCommandContext>
|
||||
{
|
||||
// Inject via public settable prop
|
||||
public MyService MyService { get; set; }
|
||||
|
||||
// ...or via the module's constructor
|
||||
|
||||
// private readonly MyService _myService;
|
||||
// public MyModule (MyService myService) => _myService = myService;
|
||||
|
||||
[Command("string")]
|
||||
public Task GetOrSetStringAsync(string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_myService.MyCoolString)) _myService.MyCoolString = input;
|
||||
return ReplyAsync(_myService.MyCoolString);
|
||||
}
|
||||
}
|
||||
32
docs/faq/basics/samples/missing-dep.cs
Normal file
32
docs/faq/basics/samples/missing-dep.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
public class MyModule : ModuleBase<SocketCommandContext>
|
||||
{
|
||||
private readonly DatabaseService _dbService;
|
||||
public MyModule(DatabaseService dbService)
|
||||
=> _dbService = dbService;
|
||||
}
|
||||
public class CommandHandler
|
||||
{
|
||||
private readonly CommandService _commands;
|
||||
private readonly IServiceProvider _services;
|
||||
public CommandHandler(DiscordSocketClient client)
|
||||
{
|
||||
_services = new ServiceCollection()
|
||||
.AddSingleton<CommandService>()
|
||||
.AddSingleton(client)
|
||||
// We are missing DatabaseService!
|
||||
.BuildServiceProvider();
|
||||
}
|
||||
public async Task RegisterCommandsAsync()
|
||||
{
|
||||
// ...
|
||||
// The method fails here because DatabaseService is a required
|
||||
// dependency and cannot be resolved by the dependency
|
||||
// injection service at runtime since the service is not
|
||||
// registered in this instance of _services.
|
||||
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
|
||||
// ...
|
||||
|
||||
// The same approach applies to the interaction service.
|
||||
// Make sure to resolve these issues!
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
private async Task ReadyAsync()
|
||||
{
|
||||
// pull your commands from some array, everyone has a different approach for this.
|
||||
var commands = _builders.ToArray();
|
||||
|
||||
// write your list of commands globally in one go.
|
||||
await _client.Rest.BulkOverwriteGlobalCommands(commands);
|
||||
|
||||
// write your array of commands to one guild in one go.
|
||||
// You can do a foreach (... in _client.Guilds) approach to write to all guilds.
|
||||
await _client.Rest.BulkOverwriteGuildCommands(commands, /* some guild ID */);
|
||||
|
||||
foreach (var c in commands)
|
||||
{
|
||||
// Create a global command, repeating usage for multiple commands.
|
||||
await _client.Rest.CreateGlobalCommand(c);
|
||||
|
||||
// Create a guild command, repeating usage for multiple commands.
|
||||
await _client.Rest.CreateGuildCommand(c, guildId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user