我正在尝试使用网络钩子和curl 自动将游戏服务器中的玩家列出到Discord。
一些变量是:
$PREPLAY
定义为 PREPLAY=__Currently Connected:__
这会添加带下划线的文本
$MESSAGE
定义为 MESSAGE="当前在线计数:\"$NOWCOUNT\"
(这有效,但必须针对 stackoverflow 标记进行编辑)
由其他脚本定义的 $NOWCOUNT
(这也有效)
$PLAYERS
(玩家列表) - 我稍后会详细介绍
我正在使用的行是:
curl -H "Content-Type: application/json" - X POST -d "{\"嵌入\": [{ \"颜色\": \"45044\", \"描述\": \"$PREPLAY$PLAYERS\", \"标题\": \"$信息\” }]}" $URL
当我使用:
PLAYERS=$(cat /tmp/players.list | sed '/Players linked/q' | egrep -v '^$' | egrep -v " ^LOG" | sed -e 's/^-/\\\\\\n/')
Discord 中的输出为

注意多余的 "\"
或者如果,我从上面的变量赋值中删除ONE“\”,将其变为:
PLAYERS=$(cat /tmp/players.list | sed '/玩家已连接/q' | egrep -v '^$'| egrep -v“^LOG”| sed -e 's/^-/\\\\\n/')
我收到 JSON 错误:

好的,删除两个“/” s 和新行只是不起作用:

I'm trying to automate listing players in a game server to Discord using webhooks and curl.
Some variables are:
$PREPLAY
defined as PREPLAY=__Currently Connected:__
this adds the text underlined
$MESSAGE
defined as MESSAGE="Current Online Count: \"$NOWCOUNT\"
(this works, but had to edit for stackoverflow markup)
$NOWCOUNT
defined by other script (This also works)
$PLAYERS
(the list of players) - I'll get more into this in a second
The line I'm using is:
curl -H "Content-Type: application/json" -X POST -d "{\"embeds\": [{ \"color\": \"45044\", \"description\": \"$PREPLAY$PLAYERS\", \"title\": \"$MESSAGE\" }]}" $URL
when I use:
PLAYERS=$(cat /tmp/players.list | sed '/Players connected/q' | egrep -v '^$' | egrep -v "^LOG" | sed -e 's/^-/\\\\\\n/')
The output in Discord comes out as

Notice the extra "\"
Or if, I remove ONE "\" from the variable assignment above to be:
PLAYERS=$(cat /tmp/players.list | sed '/Players connected/q' | egrep -v '^$' | egrep -v "^LOG" | sed -e 's/^-/\\\\\n/')
I get a JSON error:

OK, removing TWO "/"s and the new-lines just don't work:

发布评论
评论(1)
我了解
玩家
变量生成的问题。建议使用
set -x
和set +x
进行调试以检查变量:命令逐步:
建议尝试测试
players
connected/这应该打印/tmp/players.list list lines,直到找到Regexp Pattern
/players connect/
。滤除空线,并以
log
开头的行应该打印/tmp/players.list所有行,直到找到Regexp Pattern
/players connected/
。从
log
开始过滤空线,并以
-
替换为 \ code> \\ ,然后是新行。例如:
将变换:
如果要将行与
\\ n
contine contect:例如:
will transform:
可以简化/折叠
sed
命令和egrep
命令命令到单个awk
命令中。一旦您获得了所需的
播放器
列表。确保以
JSON
格式正确喂食它们。最后。
建议使用
JQ
命令准备有效的JSON
单线字符串或来自$ players
的对象。I understand the problem with
PLAYERS
variable generation.Suggesting to debug with
set -x
and andset +x
to inspect the variable:Suggesting to try test the
PLAYERS
command step by step:This should print /tmp/players.list all lines until found RegExp pattern
/Players connected/
This should print /tmp/players.list all lines until found RegExp pattern
/Players connected/
.Filtering out empty lines, and Lines starting with
LOG
This should print /tmp/players.list all lines until found RegExp pattern
/Players connected/
.Filtering out empty lines, and Lines starting with
LOG
And replacing lines starting with
-
to\\
followed by new line.For example:
Will transform:
If you want to concat the lines with
\\n
the command is:For example:
Will transform:
It is possible to simplify/fold the
sed
commands andegrep
command into singleawk
command.Once you have the desired
PLAYERS
list.Make sure you feed them correctly in
JSON
format.Finally.
Suggesting to use
jq
command to prepare a validJSON
single-line string or an object from$PLAYERS
.