Giter VIP home page Giter VIP logo

Comments (17)

codyphobe avatar codyphobe commented on May 29, 2024 2

This actually seems to be an issue not specific to textalk/websocket, but has been discussed in Textalk/websocket-php#50, and seems a solution would be to disable verify_peer and verify_peer_name on the context. Possible solution:

// SetupCommand.php

public function getSocket($gateway)
{
    $context = stream_context_create();

    stream_context_set_option($context, 'ssl', 'verify_peer', false);
    stream_context_set_option($context, 'ssl', 'verify_peer_name', false);

    return new Client($gateway, ['context' => $context]);
}

from discord.

codyphobe avatar codyphobe commented on May 29, 2024 1

Okay, so that's why it's returning a 400. You can get your Discord user ID by turning on developer mode in the Advanced section of the Appearance settings window and then right-clicking your username in the user list of a server and selecting Copy ID. I'll go ahead and add some clarification to the README as well as the method documentation on what exactly should be passed in.

from discord.

ricardoboss avatar ricardoboss commented on May 29, 2024

Turns out if fsockopen fails by returning false AND the errno is 0, it means something went wrong while creating the socket, before connect is even called. From the php docs:

If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket.

I can't say if it has to do with my environment or not? Are there any special dependencies? Does this work on Windows?

from discord.

codyphobe avatar codyphobe commented on May 29, 2024

Just tagged v1.0.7 which should fix this issue.

from discord.

ricardoboss avatar ricardoboss commented on May 29, 2024

I updated to the latest version but the same error still occurs.

Maybe you should update the textalk/websocket dependency to ^1.2. They made changes on the socket creation and fixed a few issues. I'll create a pull request and look for errors for you.

from discord.

ricardoboss avatar ricardoboss commented on May 29, 2024

Okay so after doing some local tests I found out that executing the command in WSL (Ubuntu) works perfectly fine. This problem seems to be Windows specific.

Anyway I still updated and tested the textalk/websocket dependency. Related MR: #25

from discord.

codyphobe avatar codyphobe commented on May 29, 2024

Interesting. It may be worth switching to another library to handle the websocket connection, the main reason I chose textalk/websocket was because of how small it is since it's only needed during the initial setup. I'll start looking into alternative websocket libraries and I'll leave this issue open in the meantime.

from discord.

ricardoboss avatar ricardoboss commented on May 29, 2024

This seems to have fixed the problem! I suppose we can use the .env's variables to conditionally add those options to the stream context like this:

// SetupCommand.php

public function getSocket($gateway)
{
    $context = stream_context_create();

    if (env('APP_ENV') == "local") {
        stream_context_set_option($context, 'ssl', 'verify_peer', false);
        stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
    }

    return new Client($gateway, ['context' => $context]);
}

Another variable which could be used is APP_DEBUG.

I could also think of a command option for discord:setup to toggle this behaviour. What do you think is best @codyphobe ?

from discord.

ricardoboss avatar ricardoboss commented on May 29, 2024

Looking at this line:

Could not get a websocket gateway address, defaulting to 'wss://gateway.discord.gg'.

There is also an error connecting to https://discordapp.com/api/gateway using guzzle. The method used for getSocket() to disable peer verification could also apply here:

// SetupCommand.php

public function getGateway() {
    ...
    $response = $this->guzzle->get(
        'https://discordapp.com/api/gateway',
        [
            'verify' => env('APP_ENV') != "local"
        ]
    );
    ...
}

Adding this and the code from my last comment results in the following output:

 Is the bot already added to your server? (yes/no) [no]:
 > y

Attempting to identify the bot with Discord's websocket gateway...
Connecting to 'wss://gateway.discord.gg'...
Your bot has been identified by Discord and can now send API requests!

If I now change my .env file to another environment, I get the same errors because SSL verification is active again.

from discord.

codyphobe avatar codyphobe commented on May 29, 2024

In your local environment, after the library has been successfully set up with discord:setup, does Guzzle complain about SSL verification with normal use of the library without disabling SSL verification?

I would prefer adding an option on the command with a note in the README and printing a feedback message in the terminal if a WebSocket\ConnectionException is thrown without the option present, but if Guzzle also errors out with normal use of the library then maybe a config flag (e.g. ssl.verify) that defaults to true would be best, that way users can choose to disable SSL verification without modifying their environment.

Either way I think it's necessary to add a footnote in the README about making sure that PHP is configured properly on Windows for users that experience this problem [1, 2]

from discord.

ricardoboss avatar ricardoboss commented on May 29, 2024

No, it does not work:

NotificationChannels\Discord\Exceptions\CouldNotSendNotification : Communication with Discord failed: 0: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

I'd say add an option to the command to disable it once but if users want to use it that way it should still fail. Maybe check if ssl is set up and warn the user to avoid cryptic error messages.

from discord.

ricardoboss avatar ricardoboss commented on May 29, 2024

Just tried it again with the certificate in place and got error 400:
NotificationChannels\Discord\Exceptions\CouldNotSendNotification : Discord responded with an HTTP error: 400

It occured while getting the private channel to a Discord Username. Probably not related to the ssl error but do you know what could have caused this?

from discord.

Felli avatar Felli commented on May 29, 2024

Out of curiosity, why is baseUrl not requesting v6?

protected $baseUrl = 'https://discordapp.com/api';

$url = rtrim($this->baseUrl, '/').'/'.ltrim($endpoint, '/');

See Connecting to the Gateway from the Discord Docs.

from discord.

codyphobe avatar codyphobe commented on May 29, 2024

The API version isn't specified so that it will default to the latest version.

As for the 400 error, I'm unsure as there are a number of things that can go wrong. A DM could fail because it's too long, the user has blocked the bot, the user has public DMs disabled, the bot account has them disabled, or the message is malformed. It's worth noting that while trying to replicate the 400 error, I could only get a 403 when the user had public DMs disabled.

from discord.

ricardoboss avatar ricardoboss commented on May 29, 2024

It didn't even get to sending a DM. The error occured while getting the private channel for my username.

At least the ssl error is fixed now. For me, this issue can be closed now. As for the 400 error, I'll look into it tomorrow and open a new issue for it's own discussions if I find anything related to it.

from discord.

codyphobe avatar codyphobe commented on May 29, 2024

The error occured while getting the private channel for my username.

Did you pass a username into getPrivateChannel? If so, that method and API route expects the snowflake ID for a user. That's my bad for not being specific in that method's documentation; all it says is "user", I wouldn't expect you to know that it needed an ID without having to look at the source.

I don't see any non-OAuth Discord API for getting a user's id from the username and tag, otherwise I would just add that to the method. The best bet seems to be just asking users to copy & paste their ID manually or allow them to login with Discord and store their ID from the OAuth flow.

from discord.

ricardoboss avatar ricardoboss commented on May 29, 2024

I used my username in the form of "Mizzle#8643".

from discord.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.