question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Command sends Embed message in a different server rather than where it was called from

See original GitHub issue

Summary

I’m hosting my bot on a VPS since on every website I searched there are no services that host C# discord bots. Anyways, whenever I use my help command in my bot, whenever I press a button it doesn’t send the message in the channel where the command was called and instead sends it to my Test server which is weird.

Details

Basically, I have a help command which consists of some buttons for each section of my command list like Utility/Fun Commands/Games. When I was testing this bot, everything was working well but when I started up the bot on my VPS, and then used the same help command and press a button, it just doesn’t work properly anymore. The first half of the code executes perfectly but it doesn’t send the embedded message of the command list to the right channel. Instead, it sends it to my Test server which is so weird.

This is my Help command Code:

[Command("help")]
        public async Task HelpMenu(CommandContext ctx) 
        {
            var mainMenuBuilder = new DiscordMessageBuilder()
                .AddEmbed(
                new DiscordEmbedBuilder()
                .WithTitle("Cooler Is Gay Bot | Made by Samuel J \n ©SamuelJesuthas \n\n Help Menu")
                .WithDescription("This is a multi-utility bot which just features random stuff \n Click on a category to view its list of commands \n The prefix is '>' ")
                )
                .AddComponents(new DiscordComponent[] 
                {
                    new DiscordButtonComponent(ButtonStyle.Primary, "calculatorFunction", "Calculator"),
                    new DiscordButtonComponent(ButtonStyle.Primary, "funFunction", "Fun Commands"),
                    new DiscordButtonComponent(ButtonStyle.Primary, "gamesFunction", "Games"),
                    new DiscordButtonComponent(ButtonStyle.Primary, "toolsFunction", "Tools"),
                    new DiscordButtonComponent(ButtonStyle.Danger, "exitFunction", "Exit")
                }
                );

            await ctx.Channel.SendMessageAsync(mainMenuBuilder);

            ctx.Client.ComponentInteractionCreated += async (a, b) =>
            {
                if (b.Interaction.Data.CustomId == "calculatorFunction") 
                {
                    await b.Interaction.CreateResponseAsync(
                    InteractionResponseType.UpdateMessage,
                    new DiscordInteractionResponseBuilder()
                    .WithContent("Opening Calculator Commands")
                    );

                    var basicFunctionMessage = new DiscordMessageBuilder()
                        .AddEmbed(
                        new DiscordEmbedBuilder()
                        .WithTitle("Basic Calculator Functions")
                        .WithDescription(">add -> Add 2 numbers together | E.g: >add 2 2, returns 4 \n\n " +
                                        ">subtract -> Subtract 2 numbers together | E.g: >subtract 4 3, returns 1 \n\n " +
                                        ">multiply -> Multiply 2 numbers together | E.g: >multiply 6 4, returns 24 \n\n " +
                                        ">divide -> Divide 2 numbers together | E.g: >divide 5 2, returns 2.5 \n\n " +
                                        ">circlearea -> Gives you the area of a circle with any radius | E.g: >circlearea 2, returns 12.57")
                        );
                    await ctx.Channel.SendMessageAsync(basicFunctionMessage);
                }

                if (b.Interaction.Data.CustomId == "funFunction") 
                {
                    await b.Interaction.CreateResponseAsync(
                    InteractionResponseType.UpdateMessage,
                    new DiscordInteractionResponseBuilder()
                    .WithContent("Opening Fun Commands")
                    );

                    var funFunctionMessage = new DiscordMessageBuilder()
                        .AddEmbed(
                        new DiscordEmbedBuilder()
                        .WithTitle("Fun Commands")
                        .WithDescription(">ducky -> Tell Ducky to shut the fuck up \n\n" +                                      
                                        ">tory -> Slander the torys \n\n" +
                                        ">delet -> Prove that everything is mid with this one command \n\n " +
                                        ">question -> Answer a random question about cooler \n\n " +
                                        ">dialogue -> The bot will send you a DM, type anything random to send it back to the channel where you used the command \n\n" +
                                        ">watchyourtone -> Tell someone to watch their tone | Syntax: >watchyourtone @User \n\n" +
                                        ">fortune -> See if your fate is lucky or will it be hell")
                        );
                    await ctx.Channel.SendMessageAsync(funFunctionMessage);
                }

                if (b.Interaction.Data.CustomId == "gamesFunction") 
                {
                    await b.Interaction.CreateResponseAsync(
                    InteractionResponseType.UpdateMessage,
                    new DiscordInteractionResponseBuilder()
                    .WithContent("Opening Game Commands")
                    );

                    var gamesFunctionMessage = new DiscordMessageBuilder()
                        .AddEmbed(
                        new DiscordEmbedBuilder()
                        .WithTitle("Game Commands")
                        .WithDescription(">cardgame -> Play a simple card game. Press the button to draw a card \n " +
                                                       "If your card is higher than what the bot draws, You Win \n\n" +
                                        ">lottery -> Play the lottery, pick 5 numbers from 1-50 and test your luck | " +
                                        "Syntax: >lottery num1 num2 num3 num4 num5 \n\n " +
                                        ">lottorules -> Displays information on how to play the '>lottery' command")
                        );
                    await ctx.Channel.SendMessageAsync(gamesFunctionMessage);
                }

                if (b.Interaction.Data.CustomId == "toolsFunction") 
                {
                    await b.Interaction.CreateResponseAsync(
                    InteractionResponseType.UpdateMessage,
                    new DiscordInteractionResponseBuilder()
                    .WithContent("Opening Tools Commands")
                    );

                    var toolsFunctionMessage = new DiscordMessageBuilder()
                        .AddEmbed(
                        new DiscordEmbedBuilder()
                        .WithTitle("Tools Commands")
                        .WithDescription(">timestamp -> After using this command, the next message you send the bot will return the exact time and date you sent it \n\n " +
                        ">status -> Only Sam and Delet can use this command. Sets the 'Playing' status of the bot to any text. There cannot be any spaces \n\n " +
                        ">invite -> Generates an invite link for the bot, use it to add it to other servers of your choice")
                        );
                    await ctx.Channel.SendMessageAsync(toolsFunctionMessage);
                }

                if (b.Interaction.Data.CustomId == "exitFunction")
                {
                    await b.Interaction.CreateResponseAsync(
                    InteractionResponseType.UpdateMessage,
                    new DiscordInteractionResponseBuilder()
                    .WithContent("You have exit the menu. Type in >help to use it again")
                    );
                }
            };
        }

When this issue happened I got this error in the console: image

From my experience, it looks like a timing issue but honestly the server is running well, there’s no errors with it, its just no executing the embed message commands to show the list of commands. It just executes the “InteractionResponseType.UpdateMessage” section of the command and sends the embed in my Test server

Calling the command on my public server: image

After pressing a button: image

Embed appears in a different server (My Test one): image

Steps to reproduce

  1. You can use this exact same code in a Command class as you would normally do and try running the command in a VPS to see if you also have the same issue

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
OoLunarcommented, Feb 18, 2023

I’ll be sure to add a button example. Thank you for posting this for others to see!

0reactions
samjesus8commented, Feb 18, 2023

Ok, I finally managed to figure it out. With the limited help/articles/documentation being an issue I will post a solution here for anyone else to use since there are no YouTube videos, Stack Overflow issues, Documentation that addresses this issue directly. I know its all down to skill but we all started from nothing so hopefully this will help anyone in the future

Example Code for making buttons

        [Command("button")]
        public async Task ButtonExample(CommandContext ctx) 
        {
            //Declaring my buttons

            DiscordButtonComponent button1 = new DiscordButtonComponent(ButtonStyle.Primary, "1", "Button 1");
            DiscordButtonComponent button2 = new DiscordButtonComponent(ButtonStyle.Primary, "2", "Button 2");

            //Making the embed message to attach the buttons to
            var message = new DiscordMessageBuilder()
                .AddEmbed(new DiscordEmbedBuilder()

                .WithColor(DiscordColor.Azure)
                .WithTitle("This is a message with buttons")
                .WithDescription("Please select a button")
                )
                .AddComponents(button1)
                .AddComponents(button2);

            await ctx.Channel.SendMessageAsync(message);
        }

In your Bot.cs file register a ComponentInteractionCreated after creating your client and generate a method for it (VS 2022 users just press TAB after doing +=)

Client.ComponentInteractionCreated += ButtonPressResponse;

After generating your method, you can pass in your CustomIDs and execute different code based on what button it is

        private async Task ButtonPressResponse(DiscordClient sender, DSharpPlus.EventArgs.ComponentInteractionCreateEventArgs e)
        {
            if (e.Interaction.Data.CustomId == "1") 
            {
                await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent("You pressed the 1st Button"));
            }
            else if (e.Interaction.Data.CustomId == "2") 
            {
                await e.Interaction.CreateResponseAsync(InteractionResponseType.UpdateMessage, new DiscordInteractionResponseBuilder().WithContent("You pressed the 2nd Button"));
            }
        }

It might seem weird to newbies that you have to manage the Buttons from the Bot.cs file rather than in the command class but I now understand why registering the eventhandler in the command is a bad thing

Read more comments on GitHub >

github_iconTop Results From Across the Web

The warning command sends the Embedded message to a ...
The warning command sends the Embedded message to a different server instead of the Discord server that the command was executed from.
Read more >
Setup Discord Message Embeds! (2022) - YouTube
Discord Embeds are a great way to take your discord server to the next level. Rather than just displaying information in the boring...
Read more >
EASY Styled Message/Custom Embeds | Discord - YouTube
Looking to create advanced Discord embeds or posts with embedded text, images, previews, links, buttons, dropdown options and more ?
Read more >
Discord Embed Messages - Discord.js v14 - YouTube
Your browser can't play this video. Learn more.
Read more >
How to Create Stylised Embed Messages in Discord with ...
We use discord py 2 embeds to achieve all of this URLs ... bot command structure - Automatically load bot commands from separate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found