#Help for making a command

1 messages · Page 1 of 1 (latest)

royal widget
#

Hey guys I'm trying to make an /help command for my bot but I have few errors this is my code:

        [SlashCommand("help", "Help Command")]
        [EnabledInDm(false)]
        public async Task HelpAsync()
        {
            string prefix = "/"; // komutlar için kullanılacak ön ek
            var builder = new EmbedBuilder()
                .WithTitle("Commands")
                .WithColor(Color.DarkBlue);

            foreach (var module in _service.Modules)
            {
                string description = "";
                foreach (var command in module.Commands)
                {
                    var result = await command.CheckPreconditionsAsync(Context);
                    if (result.IsSuccess)
                    {
                        description += $"{prefix}{command.Aliases.First()} - {command.Summary}\n";
                    }
                }

                if (!string.IsNullOrWhiteSpace(description))
                {
                    builder.AddField(x =>
                    {
                        x.Name = module.Name;
                        x.Value = description;
                        x.IsInline = false;
                    });
                }
            }

            await ReplyAsync("", false, builder.Build());
        }
#

Cannot resolve symbol '_service'

#

I need acsess to service

raven cloak
#

Use DI in the module class constructor

royal widget
raven cloak
#

Dependency Injection

#

The Microsoft docs have a pretty good explanation of it

royal widget
#

can u send the link please? thanks

raven cloak
#

Here’s a broad overview:

royal widget
#

Thanks

royal widget
#

@misty canyon

misty canyon
#

What is the problem?

misty canyon
#

Yeah KeyamI sent a link

royal widget
#

in this case I don't have issues in code

royal widget
#
Unhandled exception. System.InvalidOperationException: Failed to create "DNet_V3_Bot.Modules.AddID", dependency "CommandService" was not found.
   at Discord.Interactions.ReflectionUtils`1.GetMember(InteractionService commandService, IServiceProvider services, Type memberType, TypeInfo ownerType)
   at Discord.Interactions.ReflectionUtils`1.<>c__DisplayClass2_0.<CreateBuilder>b__0(IServiceProvider services)
   at Discord.Interactions.ReflectionUtils`1.CreateObject(TypeInfo typeInfo, InteractionService commandService, IServiceProvider services)
   at Discord.Interactions.Builders.ModuleBuilder.Build(InteractionService interactionService, IServiceProvider services, ModuleInfo parent)
   at Discord.Interactions.Builders.ModuleClassBuilder.BuildAsync(IEnumerable`1 validTypes, InteractionService commandService, IServiceProvider services)
   at Discord.Interactions.InteractionService.AddModulesAsync(Assembly assembly, IServiceProvider services)
   at DNet_V3_Tutorial.InteractionHandler.InitializeAsync() in C:\Users\arsal\Documents\GitHub\BruhWalker-ZEDithAIO\Discord Bot\InteractionHandler.cs:line 22
   at DNet_V3_Tutorial.program.RunAsync(IHost host) in C:\Users\arsal\Documents\GitHub\BruhWalker-ZEDithAIO\Discord Bot\Program.cs:line 56
   at DNet_V3_Tutorial.program.MainAsync() in C:\Users\arsal\Documents\GitHub\BruhWalker-ZEDithAIO\Discord Bot\Program.cs:line 43
   at DNet_V3_Tutorial.program.<Main>(String[] args)

#

it's saying not found? I didn't understand

#

I have the libary as using too

using Discord.Commands;
#

@misty canyon

royal widget
#

@raven cloak maybe uknow

raven cloak
#

Not enough code shown, to help.

#

What does your main cs file look like? (Hide the bot token, if it’s there)

#

The error is stating that the command service was never added to the DI

#

Which would be in the program.cs file, most likely.

royal widget
#

I have a AddID.cs

#

this code inside the addID.cs

#
 public class program
    {
        private DiscordSocketClient _client;        
        
        // Program entry point
        public static Task Main(string[] args) => new program().MainAsync();

        public async Task MainAsync()
        {
            var config = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
            .AddYamlFile("config.yml")
            .Build();
            
            using IHost host = Host.CreateDefaultBuilder()
                .ConfigureServices((_, services) =>
            services
                .AddSingleton(config)
                .AddSingleton(x => new DiscordSocketClient(new DiscordSocketConfig
            {
                GatewayIntents = Discord.GatewayIntents.AllUnprivileged & ~GatewayIntents.GuildScheduledEvents & ~GatewayIntents.GuildInvites,
                AlwaysDownloadUsers = true,
                LogLevel = Discord.LogSeverity.Debug
            }))
                .AddTransient<ConsoleLogger>()
                .AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordSocketClient>()))
                .AddSingleton<InteractionHandler>())
            .Build();

            await RunAsync(host);
        }

        public async Task RunAsync(IHost host)
        {
            using IServiceScope serviceScope = host.Services.CreateScope();
            IServiceProvider provider = serviceScope.ServiceProvider;


            var commands = provider.GetRequiredService<InteractionService>();
            _client = provider.GetRequiredService<DiscordSocketClient>();
            var config = provider.GetRequiredService<IConfigurationRoot>();

            await provider.GetRequiredService<InteractionHandler>().InitializeAsync();

            _client.Log += _ => provider.GetRequiredService<ConsoleLogger>().Log(_);
            commands.Log += _ => provider.GetRequiredService<ConsoleLogger>().Log(_);

            _client.Ready += async () =>
            {
                await commands.RegisterCommandsGloballyAsync(true);
            };


            // status 
            AddID.StartTimeClass._startTime = DateTime.Now;

            await _client.LoginAsync(Discord.TokenType.Bot, config["tokens:discord"]);
            await _client.StartAsync();
            await Task.Delay(-1);
        }
    }
#

my program.cs like that

#

@raven cloak

raven cloak
#

You added the InteractionService, but not the commandservice

royal widget
#

now it loads

#

thank u

raven cloak
#

Mark this as solved then? @royal widget