imranbarbhuiya/TagScript

EmbedParser doesn't work

Closed this issue · 20 comments

Code:

const { Interpreter, StringTransformer } = require("tagscript")

const ts = new Interpreter();

let result = await ts.run('{args}', { args: new StringTransformer('Hi, How are you?') });

console.log(result)

Returns:

Response {
  raw: '{args}',
  body: '{args}',
  variables: {
    args: StringTransformer { str: 'Hi, How are you?', escape: false }
  },
  actions: {},
  keyValues: {}
}

Shouldn't the output be:

Response {
  raw: '{args}',
  body: 'Hi, How are you?',
  variables: {
    args: StringTransformer { str: 'Hi, How are you?', escape: false }
  },
  actions: {},
  keyValues: {}
}

Plugins such as tagscript-plugin-discord doesn't work as well, when using transformers.

Package Versions:

"discord.js": "^14.5.0",
"tagscript": "^1.2.11",
"tagscript-plugin-discord": "^2.0.4"

I missed the parser in https://github.com/imranbarbhuiya/TagScript/tree/main/packages/tagscript#transformers example. You need to add StrictVarsParser too. I'll fix that examples and will add some more detailed documentations.

const { Interpreter, StringTransformer,  StrictVarsParser } = require("tagscript")

const ts = new Interpreter(new StrictVarsParser());

let result = await ts.run('{args}', { args: new StringTransformer('Hi, How are you?') });

console.log(result)

examples are updated now

Is the documents for parsers for the module tagscript-plugin-discord also outdated?

I can't seem to get the RequiredParser to work

const { Interpreter } = require("tagscript")
const { RequiredParser } = require("tagscript-plugin-discord")

let ts = new Interpreter(new RequiredParser())

let result = await ts.run("{require(757425366209134764, 668713062186090506, 737961895356792882):You aren't allowed to use this tag.}")

console.log(result)

Result:

Response {
  raw: "{require(757425366209134764, 668713062186090506, 737961895356792882):You aren't allowed to use this tag.}",
  body: ''",
  variables: {},
  actions: {
    require: { ids: [Array], message: "You aren't allowed to use this tag." }
  },
  keyValues: {}
}

Is there anything I need to parse in for this to work?

It's working as expected. In the actions property, you are getting the ids and messages. So in your code, you need to add a check like this

const { Interpreter } = require("tagscript")
const { RequiredParser } = require("tagscript-plugin-discord")

let ts = new Interpreter(new RequiredParser())

let result = await ts.run("{require(757425366209134764, 668713062186090506, 737961895356792882):You aren't allowed to use this tag.}")

console.log(result)

if (!result.actions.require.ids.includes(interaction.user.id)) {
// u can have channel, role, too 
return interaction.reply(result.actions.require.message)
}

Adding a guide with all these uses is planned. Till then, I'll try to add as much documentation as possible in my free time.

Last question, how do I use EmbedParser? Really appreciate you helping me.

let embed
if (result.actions.embed) {
   embed = new EmbedBuilder(response.actions.embed);
}

I can't seem to find the embed property in the actions object

const { Interpreter } = require("tagscript")
const { EmbedParser } = require("tagscript-plugin-discord")

let ts = new Interpreter(new EmbedParser())
let result = await ts.run('{ embed: { "title": "Hello!", "description": "This is a test embed." } }')

console.log(result)

output:

Response {
  raw: '{ embed: { "title": "Hello!", "description": "This is a test embed." } }',
  body: '{ embed: { "title": "Hello!", "description": "This is a test embed." } }',
  variables: {},
  actions: {},
  keyValues: {}
}

The embed property is not found in the actions object

space before embed isn't allowed. You need to enter {embed: { "title": "Hello!", "description": "This is a test embed." } }

Is there any way to parse in values into the EmberParser?

{ embed: {
    "title": "Here's a random duck!",
    "image": { "url": {user.avatar} },
    "color": 15194415
} }

This returns an JSON error:

Unexpected token h in JSON at position 63

Is there any way to parse in the user's avatar into the embed?

I am also unable to parse in images or thumbnails using the properties method.

{embed(title):Rules}
{embed(description):Follow these rules to ensure a good experience in our server!}
{embed(thumbnail):{user.avatar}}

The embed output:

{
   "title":"Rules",
   "description":"Follow these rules to ensure a good experience in our server!",
   "thumbnail":"https://cdn.discordapp.com/avatars/958188607494049792/b5e6cd7d5380e58e6fe14c3668f02714.webp"
}

This returns an error:

DiscordAPIError[50035]: Invalid Form Body
embeds[0].thumbnail[MODEL_TYPE_CONVERT]: Only dictionaries may be used in a ModelType

This is because the url needs to be nested further in the thumbnail property:

{
   "title":"Rules",
   "description":"Follow these rules to ensure a good experience in our server!",
   "thumbnail":{
      "url":"https://cdn.discordapp.com/avatars/958188607494049792/b5e6cd7d5380e58e6fe14c3668f02714.webp"
   }
}

How do I fix this issue?

Also another bug I came to find is the color not being parsed as an integer:

{embed(title):test}
{embed(color):0x2F3036}

The JSON value returned:

{
   "title":"test",
   "color": "0x2F3036"
}

The Discord API for whatever reason only accepts number, which when parsing the embed into the EmbedBuilder I get an error that 0x2F3036 from the JSON value isn't an integer.

This however is an easy fix, all you need to do is convert the hex code to an integer:

if(res.actions?.embed.color) {
     res.actions.embed.color = parseInt(res.actions.embed.color.replace("#", ""), 16)
}

You might want to fix this in the package.

Is there any way to parse in values into the EmberParser?

{ embed: {
    "title": "Here's a random duck!",
    "image": { "url": {user.avatar} },
    "color": 15194415
} }

This returns an JSON error:

Unexpected token h in JSON at position 63

Is there any way to parse in the user's avatar into the embed?

You need to use " before and after the {user.avatar}

const { Interpreter } = require("tagscript")
const { EmbedParser } = require("tagscript-plugin-discord")

let ts = new Interpreter(new EmbedParser())
let result = await ts.run(`{embed:{
    "title": "Here's a random duck!",
    "image": { "url": "{user.avatar}" },
    "color": 15194415
} }`)

console.log(result)

and add StrictVarParser and user or member transformer and user.avatar will work

#119 (comment)

How do I fix this issue?

This definitely needs to be changed but it'll be a breaking change so create a new issue and I'll fix it in the next major release (not very soon tho) till then you can fix it like this

const embedData = result,actions.embed;
if (embedData) {
if(typeof embedData.thumbnail === 'string') embedData.thumbnail = {url: embedData.thumbnail};
// same for image
   embed = = new EmbedBuilder(embedData);
}

Also another bug I came to find is the color not being parsed as an integer:

{embed(title):test}
{embed(color):0x2F3036}

The JSON value returned:

{
   "title":"test",
   "color": "0x2F3036"
}

The Discord API for whatever reason only accepts number, which when parsing the embed into the EmbedBuilder I get an error that 0x2F3036 from the JSON value isn't an integer.

This however is an easy fix, all you need to do is convert the hex code to an integer:

if(res.actions?.embed.color) {
     res.actions.embed.color = parseInt(res.actions.embed.color.replace("#", ""), 16)
}

You might want to fix this in the package.

I'm adding a fix for this

Can you try installing #120 pr and see if it fixes your color issue

yarn add tagscript-plugin-discord@pr-120
# or
npm installl tagscript-plugin-discord@pr-120

No, I am still getting the same error:

DiscordAPIError[50035]: Invalid Form Body
embeds[0].color[NUMBER_TYPE_COERCE]: Value "0x2F3036" is not int.

The Embed:

{embed(title):TEST}
{embed(color):0x2F3036}

Package Version:

"tagscript-plugin-discord": "^2.0.6-pr-120.8c92f9f.0"

Hm, interesting. If you see this test, it's similar to your one. I'll test with your input

test.each(['Red', '#ed4245', 0xed4245])('GIVEN color %j in property THEN resolve it to hex color', async (color) => {

For some reason, the last release released main branch even tho I selected the pr 😞 I've released a new version tagscript-plugin-discord@2.0.6-pr-120.cc9dc97.0. Try this and lmk if it works

It's working as expected now

great, I'll release it in a few minute