Discord.Net 缺少依赖映射
Discord.Net Dependency Map missing
我想用 GitHub 仓库制作一个机器人,但是当我导入它时,它说找不到 Depenency Map
。我 99% 确定它是 Discord.Commands
的一部分,这是机器人的第一部分:
using System;
using System.Reflection;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Discord.Commands;
namespace DiscordBot
{
class Program
{
private CommandService commands;
private DiscordSocketClient client;
private DependencyMap map;
public static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
public async Task MainAsync()
{
client = new DiscordSocketClient();
commands = new CommandService();
map = new DependencyMap();
听起来您提到的 GitHub 存储库使用的是 Discord.Net 的旧版本。
Discord.Net 上有一个旧文件对此进行了描述 - legacy.md。
I came from an earlier version of Discord.Net 1.0, and DependencyMap doesn't seem to exist anymore in the later revision? What happened to it?
The DependencyMap has been replaced with Microsoft's DependencyInjection Abstractions. An example usage can be seen here.
示例用法取自 Program.cs
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using DiscordBot.Services;
namespace DiscordBot
{
class Program
{
static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
private DiscordSocketClient _client;
private IConfiguration _config;
public async Task MainAsync()
{
_client = new DiscordSocketClient();
_config = BuildConfig();
var services = ConfigureServices();
services.GetRequiredService<LogService>();
await services.GetRequiredService<CommandHandlingService>().InitializeAsync(services);
await _client.LoginAsync(TokenType.Bot, _config["token"]);
await _client.StartAsync();
await Task.Delay(-1);
}
private IServiceProvider ConfigureServices()
{
return new ServiceCollection()
// Base
.AddSingleton(_client)
.AddSingleton<CommandService>()
.AddSingleton<CommandHandlingService>()
// Logging
.AddLogging()
.AddSingleton<LogService>()
// Extra
.AddSingleton(_config)
// Add additional services here...
.BuildServiceProvider();
}
private IConfiguration BuildConfig()
{
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json")
.Build();
}
}
}
我想用 GitHub 仓库制作一个机器人,但是当我导入它时,它说找不到 Depenency Map
。我 99% 确定它是 Discord.Commands
的一部分,这是机器人的第一部分:
using System;
using System.Reflection;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Discord.Commands;
namespace DiscordBot
{
class Program
{
private CommandService commands;
private DiscordSocketClient client;
private DependencyMap map;
public static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
public async Task MainAsync()
{
client = new DiscordSocketClient();
commands = new CommandService();
map = new DependencyMap();
听起来您提到的 GitHub 存储库使用的是 Discord.Net 的旧版本。
Discord.Net 上有一个旧文件对此进行了描述 - legacy.md。
I came from an earlier version of Discord.Net 1.0, and DependencyMap doesn't seem to exist anymore in the later revision? What happened to it? The DependencyMap has been replaced with Microsoft's DependencyInjection Abstractions. An example usage can be seen here.
示例用法取自 Program.cs
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using DiscordBot.Services;
namespace DiscordBot
{
class Program
{
static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
private DiscordSocketClient _client;
private IConfiguration _config;
public async Task MainAsync()
{
_client = new DiscordSocketClient();
_config = BuildConfig();
var services = ConfigureServices();
services.GetRequiredService<LogService>();
await services.GetRequiredService<CommandHandlingService>().InitializeAsync(services);
await _client.LoginAsync(TokenType.Bot, _config["token"]);
await _client.StartAsync();
await Task.Delay(-1);
}
private IServiceProvider ConfigureServices()
{
return new ServiceCollection()
// Base
.AddSingleton(_client)
.AddSingleton<CommandService>()
.AddSingleton<CommandHandlingService>()
// Logging
.AddLogging()
.AddSingleton<LogService>()
// Extra
.AddSingleton(_config)
// Add additional services here...
.BuildServiceProvider();
}
private IConfiguration BuildConfig()
{
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json")
.Build();
}
}
}