如何将IP接口返回的所有信息整合到Discord Webhook的content或Embed中
解决方案
方案1:将所有信息整合到content字段
直接把获取到的IP相关信息用Discord支持的Markdown格式整理后放入content字段,实现信息汇总展示:
let ip = ''; fetch('https://freeipapi.com/api/json') .then(function(response) { return response.json(); }) .then(function(data) { // 声明变量避免全局污染 const IPv = data.ipVersion; const ip = data.ipAddress; const latitude = data.latitude; const longitude = data.longitude; const country = data.countryName; const countryCode = data.countryCode; const timeZone = data.timeZone; const zipCode = data.zipCode; const city = data.cityName; const region = data.regionName; const webhook = 'https://canary.discord.com/api/webhooks/...'; // 用Markdown格式整理所有信息 const message = { "content": `**IP信息汇总** - IP版本:${IPv} - IP地址:${ip} - 经纬度:${latitude}, ${longitude} - 国家:${country} (${countryCode}) - 时区:${timeZone} - 邮政编码:${zipCode} - 城市:${city} - 地区:${region}` }; fetch(webhook, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(message) }); });
方案2:使用Discord Embed格式发送(更美观)
利用Discord Webhook支持的Embed消息结构,让信息排版更规整,匹配你提到的截图样式:
let ip = ''; fetch('https://freeipapi.com/api/json') .then(function(response) { return response.json(); }) .then(function(data) { const IPv = data.ipVersion; const ip = data.ipAddress; const latitude = data.latitude; const longitude = data.longitude; const country = data.countryName; const countryCode = data.countryCode; const timeZone = data.timeZone; const zipCode = data.zipCode; const city = data.cityName; const region = data.regionName; const webhook = 'https://canary.discord.com/api/webhooks/...'; // 构建Embed结构 const message = { "embeds": [ { "title": "IP地址信息", "color": 0x00ff00, // 绿色,可自行调整十六进制颜色值 "fields": [ { "name": "IP版本", "value": IPv, "inline": true }, { "name": "IP地址", "value": ip, "inline": true }, { "name": "经纬度", "value": `${latitude}, ${longitude}`, "inline": false }, { "name": "国家/地区", "value": `${country} (${countryCode})`, "inline": true }, { "name": "时区", "value": timeZone, "inline": true }, { "name": "邮政编码", "value": zipCode || "无", "inline": true }, { "name": "城市", "value": city || "无", "inline": true }, { "name": "地区", "value": region || "无", "inline": true } ] } ] }; fetch(webhook, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(message) }); });
注意事项
- 所有变量添加
const/let声明,避免全局变量污染; - 对可能为空的字段(如邮政编码、城市),用
|| "无"兜底显示,避免空白内容; - Embed的
color字段为十六进制颜色值,可根据需求自定义; - Discord对Embed的字段数量有上限,当前字段配置在允许范围内。
内容的提问来源于stack exchange,提问作者Rogusia
相关产品推荐
相关产品推荐

