1. 關(guān)注對應(yīng)頻道
首先要創(chuàng)建自己的頻道, 然后到對應(yīng)的公告頻道中關(guān)注這個頻道(這時
Discord
會讓你選擇頻道, 選擇之前創(chuàng)建的頻道就可以了)
2. 添加機器人
https://discord.com/developers/applications
到Discord
開發(fā)者地址, 然后創(chuàng)建一個自己的機器人即可
3. 配置機器人權(quán)限
進入設(shè)置后 選擇
OAuth2
然后 選擇第一個子選項
如圖: 選擇bot
,Administrator
選擇Bot
,上傳頭像,填寫名稱
配置機器人
下面MESSAGE CONTENT INTENT
(重點沒有選擇的話,后面獲取內(nèi)容都是空的)
4. 使用 DiscordPHP 類庫
文檔地址 https://packagist.org/packages/team-reflex/discord-php
按照類庫composer require team-reflex/discord-php
5. 代碼示例 (Laravel 框架)
先在自己的頻道發(fā)消息, 然后在日志中查看
$message->content
如果為空 (看第三步配置)文章來源:http://www.zghlxwxcb.cn/news/detail-727228.html
<?php
/**
* Discord
*/
use App\Models\DiscordMessage;
use Discord\Discord;
use Discord\Exceptions\IntentException;
use Discord\Parts\Channel\Message;
class DiscordUtils
{
// 配置
public $config = [
'token' => 'xxx',
];
// 頻道ID
public $channelId = 'xxx';
// 官方ID
public $userId = 'xxx';
/**
* @throws IntentException
*/
public function __construct()
{
$this->init();
}
/**
* 初始化
* @throws IntentException
*/
public function init()
{
$discord = new Discord($this->config);
$discord->on('ready', function (Discord $discord) {
logger("Bot is ready!");
$discord->on('message', function (Message $message, Discord $discord) {
// 在這里處理收到的消息
logger("Received Message :" . $message->content);
// 這里判斷只記錄 公告頻道的官方發(fā)布的消息
// 指定頻道的
$channel = $message->channel_id === $this->channelId;
// 指定官方
// $official = $message->user_id == $this->userId;
// 消息ID 不為空, 是指定頻道, 消息ID是不存在的
if ($channel) {
$data = [
'message_id' => $message->id,
'channel_id' => $message->channel_id,
'user_id' => $message->user_id,
'username' => $message->author->username,
'content_en' => $message->content,
'content' => $message->content,
'timestamp' => $message->timestamp->toDateTimeString(),
];
logger('write: ', $data);
$this->write($data);
}
});
});
$discord->run();
}
/**
* @param $data
*/
public function write($data)
{
try {
if (!DiscordMessage::query()->where('message_id', $data['message_id'])->exists()) {
logger('寫入: ', $data);
DiscordMessage::query()->insertGetId($data);
} else {
// 重復(fù)寫入
logger('Repeat Write Records');
}
} catch (\Exception $e) {
logger('write error');
}
}
}
6. 服務(wù)器部署
這里建議使用 進程守護 保持這個命令執(zhí)行后的進程一直都在
然后在 進程守護 中去管理和重啟這個命令(業(yè)務(wù)邏輯發(fā)生修改后需要重啟)
注意: 這里不適合使用定時器, 這樣會導(dǎo)致服務(wù)器或者數(shù)據(jù)庫壓力巨大,從而導(dǎo)致宕機文章來源地址http://www.zghlxwxcb.cn/news/detail-727228.html
<?php
namespace App\Console\Commands;
use App\Library\Api\DiscordUtils;
use Illuminate\Console\Command;
class GetDiscordMessage extends Command
{
/**
* php artisan discord:message >> /dev/null 2>&1
* php artisan discord:message --option -d
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'discord:message';
/**
* The console command description.
*
* @var string
*/
protected $description = '獲取Discord消息';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
logger('執(zhí)行 - 獲取Discord消息');
new DiscordUtils();
}
}
到了這里,關(guān)于PHP Discord獲取頻道消息功能實現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!