首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >缺少Discord.Net依赖映射

缺少Discord.Net依赖映射
EN

Stack Overflow用户
提问于 2020-11-17 17:55:20
回答 1查看 234关注 0票数 0

我想用GitHub回购来制造一个机器人,但是当我导入它时,它说没有找到Depenency Map。我确信这是Discord.Commands的一部分,这是机器人的第一部分:

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-17 18:22:04

听起来,您提到的GitHub回购使用的是较早版本的Discord.Net。

Discord.Net上有一个遗留文件描述了这个- legacy.md

我来自Discord.Net 1.0的早期版本,而DependencyMap似乎在以后的版本中不再存在了?它怎么了?DependencyMap已经被微软的DependencyInjection抽象所取代。一个示例用法可以看到这里

Program.cs获取的示例用法

代码语言:javascript
复制
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();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64880700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档