how to delete embed yagpdb custom command - discord

(yagpdb custom command) i need to delete this embed 3 seconds after that it send, but {{deleteResponse 3}} doesn't work
{{ $id := reFind \d` .Cmd | toInt64 }}
{{ with (dbGet $id "afk") }}
{{ $user := userArg .UserID }}
{{ $eta := "" }}
{{ if gt .ExpiresAt.Unix 0 }} {{ $eta = humanizeDurationSeconds (.ExpiresAt.Sub currentTime) | printf "*%s will be back in around %s.*" $user.Username }} {{ end }}
{{ sendMessage nil (cembed
"author" (sdict "name" (printf "%s está AFK" $user.String) "icon_url" ($user.AvatarURL "256"))
"description" (joinStr "\n\n" $eta .Value)
"color" (randInt 0 16777216)
"footer" (sdict "text" "AFK desde")
"timestamp" .UpdatedAt
) }}

for me it worked to put the embed in a variable and then send the message storing the message id in another variable. After sleep for the seconds I wanted to show the embed, I deleted the message via ID and the embeded part was included.
Your code would look like this:
{{ $id := reFind \d` .Cmd | toInt64 }}
{{ with (dbGet $id "afk") }}
{{ $user := userArg .UserID }}
{{ $eta := "" }}
{{ if gt .ExpiresAt.Unix 0 }} {{ $eta = humanizeDurationSeconds (.ExpiresAt.Sub currentTime) | printf "*%s will be back in around %s.*" $user.Username }} {{ end }}
{{ $embed := cembed
"author" (sdict "name" (printf "%s está AFK" $user.String) "icon_url" ($user.AvatarURL "256"))
"description" (joinStr "\n\n" $eta .Value)
"color" (randInt 0 16777216)
"footer" (sdict "text" "AFK desde")
"timestamp" .UpdatedAt
}}
{{ $x := sendMessageRetID nil $embed }}
{{ sleep (3) }}
{{ deleteMessage nil $x }}

To delete yagpdb's response after a certain time, you have to store the response & the id of the response in a variable. Here's the code for doing it.
{{ $x := sendMessageRetID nil $embed }}
{{deleteMessage nil $x 3}}
In the above code, the bot sends the embed storing the embed's id in the variable "x" & then deletes the message stored in the variable"x" after 3 seconds. I hope it helped you.

Related

Create a dictionary from an xml in Ansible

So, I am working with Ansible and I am retrieving specific information from some devices. In particular, I have a task like this:
<rpc-reply message-id="urn:uuid:1914-b84d7ff">
<lldp-neighbors-information style="brief">
<lldp-neighbor-information>
<lldp-local-port-id>A1</lldp-local-port-id>
<lldp-local-parent-interface-name>-</lldp-local-parent-interface-name>
<lldp-remote-chassis-id-subtype>A2</lldp-remote-chassis-id-subtype>
<lldp-remote-chassis-id>A3</lldp-remote-chassis-id>
<lldp-remote-port-id-subtype>A4</lldp-remote-port-id-subtype>
<lldp-remote-port-id>A5</lldp-remote-port-id>
<lldp-remote-system-name>A6</lldp-remote-system-name>
</lldp-neighbor-information>
<lldp-neighbor-information>
<lldp-local-port-id>B1</lldp-local-port-id>
<lldp-local-parent-interface-name>-</lldp-local-parent-interface-name>
<lldp-remote-chassis-id-subtype>B2</lldp-remote-chassis-id-subtype>
<lldp-remote-chassis-id>B3</lldp-remote-chassis-id>
<lldp-remote-port-id-subtype>B4</lldp-remote-port-id-subtype>
<lldp-remote-port-id>B5</lldp-remote-port-id>
<lldp-remote-system-name>B6</lldp-remote-system-name>
</lldp-neighbor-information>
<lldp-neighbor-information>
<lldp-local-port-id>C1</lldp-local-port-id>
<lldp-local-parent-interface-name>-</lldp-local-parent-interface-name>
<lldp-remote-chassis-id-subtype>C2</lldp-remote-chassis-id-subtype>
<lldp-remote-chassis-id>C3</lldp-remote-chassis-id>
<lldp-remote-port-id-subtype>C4</lldp-remote-port-id-subtype>
<lldp-remote-port-id>C5</lldp-remote-port-id>
</lldp-neighbor-information>
</lldp-neighbors-information>
</rpc-reply>
My goal is to build a dictionary like this :
{'A6': ['A1', 'A2'], 'B6': ['B1', 'B2']}
What I have done is creating a list with all my keys by doing the following tasks:
- name: Retrieve lldp system names
xml:
xmlstring: "{{ item.string | regex_replace('\n', '') }}"
xpath: "{{ item.path }}"
content: text
loop:
- { path: "/rpc-reply/lldp-neighbors-information/lldp-neighbor-information/lldp-remote-system-name", string: "{{xml_reply.xml}}" }
register: sys_names
- name: Save all sys names in a list
set_fact:
sys_names_list: "{{ sys_names.results[0].matches | map('dict2items') | list | json_query('[].value') }}"
Then I could create a second list for the elements [A1, B1] and a 3rd list [A2, B2] respectively. So then I could just combine the 3 lists and create my dictionary.
So there are 3 questions here :
Is there a way to directly build an ansible-dictionary from xml elements or should I keep writing my own module ?
Since the last element C6 would be my 3rd key BUT does NOT exist, I want to skip it. How is that possible in my task above?
How do I combine combine the 3 lists and create my dictionary, having skipped the 3rd element ? Otherwise my lists will not match the correct info ..
You can perform this by combining a filter that converts your xml file to a structured json in order to facilitate data manipulations.
In this example, I use this converter from xml to json.
File filter_plugins/from_xml.py :
#!/usr/bin/python
# From https://github.com/nasgoncalves/ansible-xml-to-json-filter/blob/master/filter_plugins/xml_to_json.py
import json
import xml.etree.ElementTree as ET
from collections import defaultdict
class FilterModule(object):
def etree_to_dict(self, t):
d = {t.tag: {} if t.attrib else None}
children = list(t)
if children:
dd = defaultdict(list)
for dc in map(self.etree_to_dict, children):
for k, v in dc.items():
dd[k].append(v)
d = {t.tag: {k: v[0] if len(v) == 1 else v
for k, v in dd.items()}}
if t.attrib:
d[t.tag].update(('#' + k, v)
for k, v in t.attrib.items())
if t.text:
text = t.text.strip()
if children or t.attrib:
if text:
d[t.tag]['#text'] = text
else:
d[t.tag] = text
return d
def filters(self):
return {
'from_xml': self.from_xml,
'xml_to_json': self.xml_to_json
}
def from_xml(self, data):
root = ET.ElementTree(ET.fromstring(data)).getroot()
return self.etree_to_dict(root)
def xml_to_json(self, data):
return json.dumps(self.from_xml(data))
File playbook.yml, I use a jinja2 template to filter key and combine values in a list, then I convert this list to a dict with filter items2dict :
---
- hosts: localhost
gather_facts: no
connection: local
tasks:
- set_fact:
xml_message: |
<rpc-reply message-id="urn:uuid:1914-b84d7ff">
<lldp-neighbors-information style="brief">
<lldp-neighbor-information>
<lldp-local-port-id>A1</lldp-local-port-id>
<lldp-local-parent-interface-name>-</lldp-local-parent-interface-name>
<lldp-remote-chassis-id-subtype>A2</lldp-remote-chassis-id-subtype>
<lldp-remote-chassis-id>A3</lldp-remote-chassis-id>
<lldp-remote-port-id-subtype>A4</lldp-remote-port-id-subtype>
<lldp-remote-port-id>A5</lldp-remote-port-id>
<lldp-remote-system-name>A6</lldp-remote-system-name>
</lldp-neighbor-information>
<lldp-neighbor-information>
<lldp-local-port-id>B1</lldp-local-port-id>
<lldp-local-parent-interface-name>-</lldp-local-parent-interface-name>
<lldp-remote-chassis-id-subtype>B2</lldp-remote-chassis-id-subtype>
<lldp-remote-chassis-id>B3</lldp-remote-chassis-id>
<lldp-remote-port-id-subtype>B4</lldp-remote-port-id-subtype>
<lldp-remote-port-id>B5</lldp-remote-port-id>
<lldp-remote-system-name>B6</lldp-remote-system-name>
</lldp-neighbor-information>
<lldp-neighbor-information>
<lldp-local-port-id>C1</lldp-local-port-id>
<lldp-local-parent-interface-name>-</lldp-local-parent-interface-name>
<lldp-remote-chassis-id-subtype>C2</lldp-remote-chassis-id-subtype>
<lldp-remote-chassis-id>C3</lldp-remote-chassis-id>
<lldp-remote-port-id-subtype>C4</lldp-remote-port-id-subtype>
<lldp-remote-port-id>C5</lldp-remote-port-id>
</lldp-neighbor-information>
</lldp-neighbors-information>
</rpc-reply>
- set_fact:
my_dict: |
{% set json_message = xml_message | from_xml %}
{% for item in json_message['rpc-reply']['lldp-neighbors-information']['lldp-neighbor-information'] %}
{% if "lldp-remote-system-name" in item %}
- key: {{ item['lldp-remote-system-name'] }}
value: [{{ item['lldp-local-port-id'] }}, {{ item['lldp-remote-chassis-id-subtype'] }}]
{% endif %}
{% endfor %}
- set_fact:
my_dict: "{{ my_dict | from_yaml | items2dict }}"
- debug:
var: my_dict
Executing this playbook with ansible-playbook playbook.yml returns :
PLAY [localhost] **********************************
TASK [set_fact] ***********************************
ok: [localhost]
TASK [set_fact] ***********************************
ok: [localhost]
TASK [set_fact] ***********************************
ok: [localhost]
TASK [debug] **************************************
ok: [localhost] => {
"my_dict": {
"A6": [
"A1",
"A2"
],
"B6": [
"B1",
"B2"
]
}
}

How can I use a dynamic key/value on a second level in twig?

This is my group array:
array:4 [▼
0 => Fields {#7444 ▼
-id: 1
-name: "ID"
-unique_id: "6ab8c870ed"
-productgroup: PersistentCollection {#7448 ▶}
-type: Type {#7525 ▼
+__isInitialized__: true
-id: 2
-name: "hidden"
-unique_id: "5e1086c862"
-label: "hidden"
…2
}
}
1 => Fields {#7526 ▶}
2 => Fields {#7530 ▶}
3 => Fields {#7534 ▶}
]
This is my column array:
array:3 [▼
0 => "id"
1 => "name"
2 => "type"
]
I know that my id is 1:
$id = "1";
For each value in my column key I want to print out the according value from my group array.
So the result would be:
1
ID
hidden
I try to achieve this with twig
{% for key, value in column %}
{% for k, v in group %}
{{ v.[value] }};
{% endfor %}
{% endfor %}
The error is:
Expected name or number.
NOTE: This Symfony2 / Twig - getting array from dynamic array key is not helping me, because it only explains how to use the value like this v[value] or this v[key] but not in the second level like this v.[value].
You need to use two for-loops in your code to achieve what you want though,
{% for class in classes %}
if (optionValue == {{ class.id }}) {
{% for column in columns %}
var {{ column }} = '{{ attribute(class, column) }}';
{% endfor %}
}
{% endfor %}
demo

In Hugo templates, how do you check length of JSON file array?

In Hugo, you can assign the contents of a JSON file to a template variable:
{{ $json := getJSON "posts.json" }}
How do you check the length in a condition block?
You can use the eq function to compare the length:
{{ if (eq ($json | len) 0) }}
no data
{{ else }}
show posts
{{ end }}
You can use the len function to get the length:
{{$len := len $json}}

orderBy in ngRepeat using another variable property

Is it possible to sort items from one array based on another variable by only using orderBy and not adding a count property to the items in the array?
Lets say i have an array and a map in the controller:
$scope.x = [{no:1,name:"a"},
{no:2,name:"b"},
{no:3,name:"c"}];
$scope.y = { 1: [1],
2: [1,2,3],
3: [1,2] };
and the html will look like this:
<div ng-repeat="i in x | orderBy: y[i.no].length">
{{i.no}}
{{ y[i.no] }}
{{ y[i.no].length }}
</div>
output:
1 [1] 1
2 [1,2,3] 3
3 [1,2] 2
but it should be:
1 [1] 1
3 [1,2] 2
2 [1,2,3] 3
You could use a predicate function, to specify your condition.
Try this:
<div ng-repeat="i in x | orderBy: findOrder">
{{i.no}}
{{ y[i.no] }}
{{ y[i.no].length }}
</div>
And:
$scope.findOrder = function(elem){
console.log(elem.no);
return $scope.y[elem.no].length;
}
A working fiddle.

How to handle filter in Angular expression

Please have a look at below code
Json object
namelist = [{name: "Mayur" , checked : true }, { name: "Rayum" , checked : false }]
In HTML i want to show number of items which are checked true , for above Json object count should be 1.
{{namelist.length}} // gives me total count
//can we do something like below
{{ namelist.length | filter {checked:true} }}
which we give me only count of the filtered count.
Try this:
{{ (namelist | filter: { checked: true }).length }}
Try this: {{ (namelist | filter:{checked:true}).length }}
You can read about filters here

Resources