© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
Nelson

✅ Is there a way to run a commands method inside of another command? (DSharpPlus)

Title explains it.
        #region RandomImage
        [SlashCommand("randomimage", "Visa en random bild från Örsk Army")]
        public async Task RandomImageAsync(InteractionContext ctx, [Option("mängd", "Ange antal bilder du vill visa")] long amount = 1)
        {
            await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
                .WithContent("..."));

            var rand = new Random();
            for (int i = 0; i < amount; i++)
            {
                var files = Directory.GetFiles("H:\\My Drive\\Discord Bot\\Images");
                FileStream file = new FileStream(files[rand.Next(0, files.Length)].ToString(), FileMode.Open);
                var builder = new DiscordMessageBuilder();
                builder.AddFile(file);
                await ctx.Channel.SendMessageAsync(builder);
                await Task.Delay(1000);
            }
        }
        #endregion

        [SlashCommand("test", "test")]
        public async Task Test(InteractionContext ctx)
        {
            await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
                .WithContent("..."));

            
            var builder = new DiscordMessageBuilder()
                .WithContent("This message has buttons!")
                .AddComponents(new DiscordComponent[]
                {
                    new DiscordButtonComponent(ButtonStyle.Primary, "1", "This is a button")
                });

            await ctx.Channel.SendMessageAsync(builder);

            Program.client.ComponentInteractionCreated += async (sender, e) =>
            {
                // Run 'RandomImageAsync' Here
            };
        }
        #region RandomImage
        [SlashCommand("randomimage", "Visa en random bild från Örsk Army")]
        public async Task RandomImageAsync(InteractionContext ctx, [Option("mängd", "Ange antal bilder du vill visa")] long amount = 1)
        {
            await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
                .WithContent("..."));

            var rand = new Random();
            for (int i = 0; i < amount; i++)
            {
                var files = Directory.GetFiles("H:\\My Drive\\Discord Bot\\Images");
                FileStream file = new FileStream(files[rand.Next(0, files.Length)].ToString(), FileMode.Open);
                var builder = new DiscordMessageBuilder();
                builder.AddFile(file);
                await ctx.Channel.SendMessageAsync(builder);
                await Task.Delay(1000);
            }
        }
        #endregion

        [SlashCommand("test", "test")]
        public async Task Test(InteractionContext ctx)
        {
            await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
                .WithContent("..."));

            
            var builder = new DiscordMessageBuilder()
                .WithContent("This message has buttons!")
                .AddComponents(new DiscordComponent[]
                {
                    new DiscordButtonComponent(ButtonStyle.Primary, "1", "This is a button")
                });

            await ctx.Channel.SendMessageAsync(builder);

            Program.client.ComponentInteractionCreated += async (sender, e) =>
            {
                // Run 'RandomImageAsync' Here
            };
        }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Is there a way to block the calling of public method from abstract class inside a derived one?
C#CC# / help
2y ago
❔ is there a way for a method to have interchangeable parameter?
C#CC# / help
3y ago
✅ is there a way to get the method name from Task?
C#CC# / help
3y ago
✅ vscode c# dev tools is there a way to run faster
C#CC# / help
9mo ago