Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EmbedParser doesn't work #119

Closed
marcussaw123 opened this issue Oct 10, 2022 · 20 comments · Fixed by #120
Closed

EmbedParser doesn't work #119

marcussaw123 opened this issue Oct 10, 2022 · 20 comments · Fixed by #120

Comments

@marcussaw123
Copy link

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"
@imranbarbhuiya
Copy link
Owner

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)

@imranbarbhuiya
Copy link
Owner

examples are updated now

@marcussaw123
Copy link
Author

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?

@imranbarbhuiya
Copy link
Owner

imranbarbhuiya commented Oct 10, 2022

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.

@marcussaw123
Copy link
Author

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

@imranbarbhuiya
Copy link
Owner

imranbarbhuiya commented Oct 10, 2022

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

@marcussaw123
Copy link
Author

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

@imranbarbhuiya
Copy link
Owner

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

@marcussaw123
Copy link
Author

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?

@marcussaw123
Copy link
Author

marcussaw123 commented Oct 11, 2022

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?

@marcussaw123
Copy link
Author

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.

@marcussaw123 marcussaw123 changed the title StringTransformer doesn't work EmbedParser doesn't work Oct 11, 2022
@imranbarbhuiya
Copy link
Owner

imranbarbhuiya commented Oct 11, 2022

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

@imranbarbhuiya
Copy link
Owner

imranbarbhuiya commented Oct 11, 2022

#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);
}

@imranbarbhuiya
Copy link
Owner

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

@imranbarbhuiya
Copy link
Owner

imranbarbhuiya commented Oct 11, 2022

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

@marcussaw123
Copy link
Author

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"

@imranbarbhuiya
Copy link
Owner

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) => {

@imranbarbhuiya
Copy link
Owner

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

@marcussaw123
Copy link
Author

It's working as expected now

@imranbarbhuiya
Copy link
Owner

great, I'll release it in a few minute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants