discord-php / DiscordPHP

An API to interact with the popular messaging app Discord
MIT License
1.01k stars 237 forks source link

Creating a formatted message with 2way bot #764

Closed m00nm00nm00n closed 2 years ago

m00nm00nm00n commented 2 years ago

For the life of me I can't figure out how to create a formatted message similar to this:

message using message builder

` foreach ($result->asset_events as $asset) {

    $x++;

    if ($x <= 8) {

        $created_date = strtotime($asset->created_date);
        $created_date = date('m/d/Y g:ia', $created_date);

        $message_response .= $created_date . "\n";
        $message_response .= $asset->asset->name . "\n";

        if ($asset->event_type == 'successful') {
            $message_response .= 'sale' . "\n";
        } else {
            $message_response .= $asset->event_type . "\n";
        }

        $message_response .= $asset->asset->permalink . "\n";
        $message_response .= "\n";

    }

}

if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
 } else {
    $match_found++;
    $message->reply($message_response);
}

`

May I please ask a huge favor - Can someone please show me how to use Message Builder to construct a response with the code above that is similar to the attached picture?

Been pulling my hair out trying to figure this out

Thank you for the help !!!!

SQKo commented 2 years ago
$embed = new Embed($discord);
$embed
    ->setAuthor('Whale Watchers Bot', 'https://cdn.discordapp.com/app-icons/380557100906184716/1e785e9964b7e72fb401f09708d5bc1d.webp?size=128')
    ->setColor('#32a854')
    ->setTitle('Dingaling Bought Cosmic Zxdiac - Sagittarius - Open Sea Collection')
    ->setUrl('https://opensea.io/assets/0x495f947276749ce646f68ac8c248420045cb7b5e/80494307024529346018053650490912529916739680814770830097664395980124115173670')
    ->addFieldValues('Price', '0.0Ξ')
    ->setImage('https://lh3.googleusercontent.com/Dl16SwK9HwhYI0nV-sb1fTvM78uupzwrV-MBF044XGhqYpTFZd8agt_ZOoAIDnnZ2UjN4gSlKvRL6Ueeq6mOaZYxYDNwNOnLuRAwmA=s0')
    ->setFooter('Sold on OpenSea', 'https://storage.googleapis.com/opensea-static/Logomark/Logomark-White.png')
    ->setTimestamp(strtotime('01/17/2022'));
$builder = MessageBuilder::new()->addEmbed($embed);
$channel->sendMessage($builder);
m00nm00nm00n commented 2 years ago
$embed = new Embed($discord);
$embed
    ->setAuthor('Whale Watchers Bot', 'https://cdn.discordapp.com/app-icons/380557100906184716/1e785e9964b7e72fb401f09708d5bc1d.webp?size=128')
    ->setColor('#32a854')
    ->setTitle('Dingaling Bought Cosmic Zxdiac - Sagittarius - Open Sea Collection')
    ->setUrl('https://opensea.io/assets/0x495f947276749ce646f68ac8c248420045cb7b5e/80494307024529346018053650490912529916739680814770830097664395980124115173670')
    ->addFieldValues('Price', '0.0Ξ')
    ->setImage('https://lh3.googleusercontent.com/Dl16SwK9HwhYI0nV-sb1fTvM78uupzwrV-MBF044XGhqYpTFZd8agt_ZOoAIDnnZ2UjN4gSlKvRL6Ueeq6mOaZYxYDNwNOnLuRAwmA=s0')
    ->setFooter('Sold on OpenSea', 'https://storage.googleapis.com/opensea-static/Logomark/Logomark-White.png')
    ->setTimestamp(strtotime('01/17/2022'));
$builder = MessageBuilder::new()->addEmbed($embed);
$channel->sendMessage($builder);

Thanks for taking the time to write this - my question though is how do you incorporate this into a 2 way messaging bot?

with the 2 way bot I just use $message->reply() but I am not sure how to pass this message builder into it.

SQKo commented 2 years ago

2 way bot what? Direct Message? Reply? latest version allows you to use $message->reply() with message builder, try to update your DiscordPHP, if you haven't then add:

$builder->setReplyTo($message);
m00nm00nm00n commented 2 years ago

I went to update DiscordPHP but im using dev-master and it says its all up to date. However, when I use embed function it crashes so something seems off

key2peace commented 2 years ago

show me your composer.json please

m00nm00nm00n commented 2 years ago

show me your composer.json please

{ "require": { "team-reflex/discord-php": "dev-master" } }

key2peace commented 2 years ago

do you have

use Discord\Parts\Embed\Embed;
use Discord\Builders\MessageBuilder;

on top of your script?

m00nm00nm00n commented 2 years ago

do you have

use Discord\Parts\Embed\Embed;
use Discord\Builders\MessageBuilder;

on top of your script?

edit: thank you! that fixed everything except for the line:

$channel->sendMessage($builder);

from there I added in the channel object

thanks again!

key2peace commented 2 years ago

well, how did you obtain $channel ?