pycord dropdown menu - multiple selection if statement - discord

The title Probably makes it confusing but let me clarify what my question is
So pretty much I have a dropdown menu where the user will select two of the colors, I don't understand how I can make it so if someone for example was the select Yellow and Red Circle it would respond with a GIF of that selection - It would do the same thing with each combination...
class mix(discord.ui.View):
#discord.ui.select( # the decorator that lets you specify the properties of the select menu
placeholder = "Choose a potion", # the placeholder text that will be displayed if nothing is selected
min_values = 2, # the minimum number of values that must be selected by the users
max_values = 2, # the maximum number of values that can be selected by the users
options = [ # the list of options from which users can choose, a required field
discord.SelectOption(
label="🟡",
value='1'
),
discord.SelectOption(
label="🟢",
value='2'
),
discord.SelectOption(
label="🧊",
value='3'
),
discord.SelectOption(
label="🔴",
value='4'
),
discord.SelectOption(
label="🔵",
value='5'
),
]
)
async def select_callback(self, select, interaction): # the function called when the user is done selecting options
if select.value == "🟡" and "🟢":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957164412807036928/yg.gif")
elif select.value[0] == "🟡" and "🧊":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957166502258286592/yc.gif")
async def button(ctx):
embed=discord.Embed(title=f"Choose Wisely!", description="",color=0x660066)
embed.set_image(url="https://media.discordapp.net/attachments/999932873077424128/1047271785298657310/unknown.png?width=459&height=586")
await ctx.respond(embed=embed, view=mix()) # Send a message with our View class that contains the button```
Any help would be apricated <3

Figures it out! - Here for anyone that might need this
#discord.ui.select( # the decorator that lets you specify the properties of the select menu
placeholder = "Choose a potion", # the placeholder text that will be displayed if nothing is selected
min_values = 1, # the minimum number of values that must be selected by the users
max_values = 2, # the maximum number of values that can be selected by the users
options = [ # the list of options from which users can choose, a required field
discord.SelectOption(
label="🟡",
value='1'
),
discord.SelectOption(
label="🟢",
value='2'
),
discord.SelectOption(
label="🧊",
value='3'
),
discord.SelectOption(
label="🔴",
value='4'
),
discord.SelectOption(
label="🔵",
value='5'
),
]
)
async def select_callback(self, select, interaction): # the function called when the user is done selecting options
if select.values[0] == "1" and "2":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957164412807036928/yg.gif")
if select.values[0] == "2" and "1":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957164412807036928/yg.gif")
if select.values[0] == "1" and "3":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957166502258286592/yc.gif")
if select.values[0] == "3" and "1":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957166502258286592/yc.gif")
if select.values[0] == "1" and "4":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957165125381533746/yp.gif")
if select.values[0] == "4" and "1":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957165125381533746/yp.gif")
if select.values[0] == "1" and "5":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957165284152717322/yb.gif")
if select.values[0] == "5" and "1":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957165284152717322/yb.gif")
if select.values[0] == "2" and "3":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957165926103519282/gc.gif")
if select.values[0] == "3" and "2":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957165926103519282/gc.gif")
if select.values[0] == "2" and "4":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957166060077981716/gp.gif")
if select.values[0] == "4" and "2":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957166060077981716/gp.gif")
if select.values[0] == "2" and "5":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957166244820312064/gb.gif")
if select.values[0] == "5" and "2":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957166244820312064/gb.gif")
if select.values[0] == "3" and "4":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957164839044792320/cp.gif")
if select.values[0] == "4" and "3":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957164839044792320/cp.gif")
if select.values[0] == "3" and "5":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957167056443305984/cb.gif")
if select.values[0] == "5" and "3":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957167056443305984/cb.gif")
if select.values[0] == "4" and "5":
await interaction.response.send_message("https://media.discordapp.net/attachments/957163963588681788/957168215568580618/pb.gif")

Related

PostgreSQL jsonb_set multiple elements in array

I have following jsonb structure in column recipients in a table called mailing:
[
{
"text": "Text1",
"smsId": 1,
"value": "123456",
"status": "Sent"
},
{
"text": "Text1",
"smsId": 2,
"value": "23456",
"status": "Sent"
},
{
"text": "Text1",
"smsId": 3,
"value": "345678",
"status": "Sent"
}]
I need to update one field in multiple elements, so the outcome should look like this:
[
{
"text": "Text1",
"smsId": 1,
"value": "123456",
"status": "Delivered"
},
{
"text": "Text1",
"smsId": 2,
"value": "23456",
"status": "Delivered"
},
{
"text": "Text1",
"smsId": 3,
"value": "345678",
"status": "Delivered"
}]
The most close I got to solution is this:
WITH item AS (SELECT mailing_id, ('{' || INDEX-1 || ',status}')::text[] AS PATH
FROM mailing, jsonb_array_elements(recipients) WITH ORDINALITY arr(recipient, INDEX)
WHERE recipient->>'smsId' = any(array['1', '2', '3']))
UPDATE mailing m
SET recipients = jsonb_set(recipients, item.path, '"Delivered"',FALSE)
FROM item
WHERE m.mailing_id = item.mailing_id;
But this solution updates only first row, and I am not sure if I should somehow loop this or try different approach?
You need to aggregate modified array elements with jsonb_agg():
with new_data as (
select
mailing_id,
jsonb_agg(
case when value->>'smsId' = any('{1,2,3}') then value || '{"status": "Delivered"}'
else value
end) as recipients
from mailing
cross join jsonb_array_elements(recipients)
group by mailing_id
)
update mailing m
set recipients = n.recipients
from new_data n
where m.mailing_id = n.mailing_id;
Test it in db<>fidlle.

Discord.js - JSON Parsing?

Okay, I'm a bit new to JSON in Discord.js Bot Development, And I'm currently making a command where it grabs all of the data in a json file and splits it so it looks nicely formatted.
Currently have:
let Games = {
"1": "Jailbreak",
"2": "Adopt Me",
"3": "Bubble Gum Simulator",
"4": "Thick Legends",
"5": "Arsenal",
"6": "Legends Of Speed",
"7": "Speed Champions",
"8": "Build A Boat For Treasure",
"9": "Boxing Simulator",
"10": "Flight Simulator",
"11": "Mad City",
"12": "Redwood Prison",
"13": "Horrific Housing",
"14": "Welcome To Bloxburg",
"15": "Tower Of Hell"
}
function getGames(lol) {
lol = JSON.parse(Games[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]);
for (let j in lol) {
return "• " + lol[j];
}
}
I want to group all of the Games and make it output:
"• example\n"
Any answers?
You can get all object values, use map to add • and join it with '\n'.
Like this:
let Games = {
"1": "Jailbreak",
"2": "Adopt Me",
"3": "Bubble Gum Simulator",
"4": "Thick Legends",
"5": "Arsenal",
"6": "Legends Of Speed",
"7": "Speed Champions",
"8": "Build A Boat For Treasure",
"9": "Boxing Simulator",
"10": "Flight Simulator",
"11": "Mad City",
"12": "Redwood Prison",
"13": "Horrific Housing",
"14": "Welcome To Bloxburg",
"15": "Tower Of Hell"
}
console.log(getGames(Games))
function getGames(gameList) {
return Object.values(gameList).map(game => `• ${game}`).join('\n')
}
Live example
let Games = {
"1": "Jailbreak",
"2": "Adopt Me",
"3": "Bubble Gum Simulator",
"4": "Thick Legends",
"5": "Arsenal",
"6": "Legends Of Speed",
"7": "Speed Champions",
"8": "Build A Boat For Treasure",
"9": "Boxing Simulator",
"10": "Flight Simulator",
"11": "Mad City",
"12": "Redwood Prison",
"13": "Horrific Housing",
"14": "Welcome To Bloxburg",
"15": "Tower Of Hell"
}
console.log(getGames(Games))
function getGames(gameList) {
return Object.values(gameList).map(game => `• ${game}`).join('\n')
}
You can get an array of the object values with Object.values()
let gamenames = Object.values(games)
Then,you can loop through the array elements to add them to a string.
let myString = "List: \n"
gamenames.forEach((val) => {
myString += `• ${val} \n`
})
myString is now equal to an organized list of the games! It should look like:
List:
• Jailbreak
• Adopt me
...etc..
(The list does put every single value on a seperate line,but StackOverflow doesn't want to show them properly for some reason)
Hope this helped! If it did,please mark my question as a valid answer.

How to Join Multiple Arrays inside filter function of Arrays in Typescript

I am using Typescript in an Angular/Ionic project. I have an array of users that contain an array of skills. I have to filter users based on their online status as well as skills.
[
{
"id": 1,
"name": "Vikram Shah",
"online_status": "Online",
"skills": [{
"id": 2,
"title": "CSS"
},
{
"id": 3,
"title": "JavaScript"
},
{
"id": 4,
"title": "Python"
}
]
},
{
"id": 1,
"name": "Abhay Singh",
"online_status": "Online",
"skills": [{
"id": 1,
"title": "HTML"
},
{
"id": 2,
"title": "CSS"
},
{
"id": 3,
"title": "JavaScript"
},
{
"id": 4,
"title": "Python"
}
]
},
{
"id": 1,
"name": "Test Oberoi",
"online_status": "Online",
"skills": [{
"id": 1,
"title": "HTML"
},
{
"id": 2,
"title": "CSS"
},
{
"id": 3,
"title": "JavaScript"
},
{
"id": 4,
"title": "Python"
}
]
}
]
This is how all skills look like
this.skill_types = [
{"id":8,"title":"Cleaner", checked:false},
{"id":7,"title":"Painter", checked:false},
{"id":6,"title":"Plumber", checked:false},
{"id":5,"title":"Carpenter", checked:false},
{"id":4,"title":"Advisor", checked:false},
{"id":3,"title":"Team Leader", checked:false},
{"id":2,"title":"Management", checked:false},
{"id":1,"title":"Administrator", checked:false}
];
This array contains the IDs of skills that I want to filter
filterArr = [1, 3, 6];
This solution is almost working as expected. It is filtering well based on two criteria together.But not sure how to add condition for second filtering. The second filter should apply only if filterArr is not empty.
return this.items = this.items.filter((thisUser) => {
return thisUser.online_status.toLowerCase().indexOf(onlineStatus.toLowerCase()) > -1 &&
thisUser.skills.some(c => this.filterArr.includes(c.id))
});
The issue I am facing with code above is When there is no skill selected in the filter criteria, I would like to display all users. But it is not working that way. The logic here is to not apply any filter when the size of selected skills (filter condition) is greater than zero. So I tried this way....which looks similar to the way above...but this makes everything worse.
let filteredByStatus = [];
filteredByStatus = this.items.filter((thisUser) => {
return thisUser.online_status.toLowerCase().indexOf(onlineStatus.toLowerCase()) > -1
});
//Condition can be applied if filtering is separated
let filteredBySkills = [];
filteredBySkills = this.items.filter((thisUser) => {
return thisUser.skills.some(c => this.filterArr.includes(c.id))
});
//Expecting to join results from multiple filters
return this.items = filteredByStatus.concat(filteredBySkills);
But this is not working at all. Not sure what wrong is there. I am looking for a solution that enables to join arrays of similar objects without duplicating them.
Don't think you need to join arrays for your filtering. You can use something like rxjs filter.
return from(this.items)
.pipe(
filter(user => {
return user.online_status.toLowerCase().indexOf(onlineStatus.toLowerCase()) > -1
&& user.skills.some(c => filterArr.includes(c.id));
})
);
Or if you like to split it up you can just change it to like:
return from(this.items)
.pipe(
filter(user => user.online_status.toLowerCase().indexOf(onlineStatus.toLowerCase()) > -1),
filter(user => user.skills.some(c => filterArr.includes(c.id)))
);
Stackblitz: https://stackblitz.com/edit/angular-pk3w8b
You can tweak your condition a bit and place !this.filterArr.length in your condition (in terms of OR condition AND with user status) to make your whole condition gets true so that user gets filter.

How to modify a complex JSON Object in using Immutable

I have below JSON and wanted to update the value depending on Aid, Bid and Cid using Immutable.js
e.g.
Below input provided.
Aid= A, Bid = 1, Cid= 4, NewValue = 'FOUR'
If above input is provided the value "One" needs to be changed to "FOUR"
let sampleJson = {
Aid: 'A', detail:"sample", list: [
{
"Bid": "1",
"group": [
{
"name": "Group A",
"Cid": "4",
"value": "One"
},
{
"name": "Group A",
"Cid": "41",
"value": "1"
},
]
},
{
"Bid": "2",
"group": [
{
"name": "Group A",
"Cid": "4",
"value": "1"
},
{
"name": "Group A",
"Cid": "4",
"value": "1"
},
]
};
I was able to access the value using below code. How can i return the entire JSON with updated value?
let variale = Immutable.fromJS(sampleJson).
getIn(['list']).
find(allocation => allocation.get("Bid") === "1").
getIn(['group']).
find(fun => fun.get("Cid") === "4").set('value',"FOUR");
Anyone has any suggestions on how to resolve this problem?
I think you can try to do this like so:
let immutable = Immutable.fromJS(sampleJson);
immutable = immutable.setIn(['list', 0, 'group', 0, 'value'], 'FOUR');
This monstrosity is how I would do it:
const newData = originalData.update('list', list => {
const itemIndex = list.findIndex(item => item.get('Bid') === '2');
return list.update(itemIndex, listItem => {
return listItem.update('group', groupList => {
const groupIndex = list.findIndex(group => group.get('Cid') === '4');
return groupList.update(groupIndex, group => {
return group.set('value', 'FOUR');
});
});
});
});
https://jsbin.com/latupo/7/edit?html,js,console
Personally I stopped using Immutable, I always found it a bit painful (not to mention those docs!). I now use redux and good old cloning to not mutate state. Less performant in theory but if you've got nothing that runs over a few milliseconds anyway, save yourself the trouble...

web-proxy for angularjs application

I have to create a web-proxy script for my angularjs files because I got the error of CORS(Cross Origin Request Method) and I dont have any options to use Access Control Allow Origin because I cant make any changes to my server end.
My backend data is in java. So please someone tell me how to make a web-proxy for my angularjs application.
Or is there anyway to bypass the cors request from my browser.
A quick work around using foreach and json_decode.
If your print_r($json) comes in this format:
Array
(
[0] => Array
(
[studentid] => 5
[firstame] => jagdjasgd
[lastname] => kjdgakjd
[gender] => 1
[email] => dgahsdg#em.com
[fathername] => hashsdh
[mothername] => djhavshd
[birthday] => 2016-03-21
[address] => gafdhfadhs
[tenth] => 45.235
[twelfth] => 56.25
)
)
This will do the trick:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$json = json_decode(file_get_contents("php://input"), true);
//print_r($json);
$data =array();//Open blank array for student data
$num = array();//Open Blank array for number of student
foreach($json as $k => $v):
$num [] = $v; //number of student
if(is_array($v)){
foreach($v as $key=>$val):
$data[$key] = $val;//Student data
endforeach;
}
endforeach;
$row= count($num);//Put number of student in $row
for($i=1; $i<=$row; $i++){
$q = 'INSERT INTO table (`col1`)
VALUES($data['studentid'])';//Looping through sql statement
}
Hope this will help.
User json_decode with true params
$data = "{"studentid":"5","firstame":"jagdjasgd","lastname":"kjdgakjd","email":"dgahsdg#em.com"}";
$d = json_decode($data,true); // true means it will result in aaray
print_r($d);
$stdId = $d['studentid'];
$fname = $d['firstname'];
$lname = $d['lastname'];
$mail = $d['email'];
EDIT:
For multiple json data:
$data = '[
{
"0": "1",
"studentid": "1",
"1": "David",
"firstname": "David",
"2": "Beckham",
"lastname": "Beckham",
"3": "1",
"gender": "1",
"4": "david123#gmail.com",
"email": "david123#gmail.com",
"5": "Beckham",
"fathername": "Beckham",
"6": "Beckhamii",
"mothername": "Beckhamii",
"7": "2016-03-13",
"birthday": "2016-03-13",
"8": "dgasdhghasd\nkajsdgjaksdh\nkahdgjaksgdas",
"address": "dgasdhghasd\nkajsdgjaksdh\nkahdgjaksgdas",
"9": "58.25",
"tenth": "58.25",
"10": "62.25",
"twelfth": "62.25"
},
{
"0": "3",
"studentid": "3",
"1": "Chris",
"firstname": "Chris",
"2": "Gayle",
"lastname": "Gayle",
"3": "1",
"gender": "1",
"4": "chrisgayle#email.com",
"email": "chrisgayle#email.com",
"5": "Chris Potters",
"fathername": "Chris Potters",
"6": "Christine",
"mothername": "Christine",
"7": "2016-04-20",
"birthday": "2016-04-20",
"8": "adhafsdh\njgadahksgdkjas\njagdjahsdlkajsld\nkajsgdjlahsdlkas",
"address": "adhafsdh\njgadahksgdkjas\njagdjahsdlkajsld\nkajsgdjlahsdlkas",
"9": "87.587",
"tenth": "87.587",
"10": "98.256",
"twelfth": "98.256"
},
{
"0": "5",
"studentid": "5",
"1": "jagdjasgd",
"firstname": "jagdjasgd",
"2": "kjdgakjd",
"lastname": "kjdgakjd",
"3": "1",
"gender": "1",
"4": "dgahsdg#em.com",
"email": "dgahsdg#em.com",
"5": "hashsdh",
"fathername": "hashsdh",
"6": "djhavshd",
"mothername": "djhavshd",
"7": "2016-03-21",
"birthday": "2016-03-21",
"8": "gafdhfadhs\nagdkjashdas\ndjkahsdklsaj",
"address": "gafdhfadhs\nagdkjashdas\ndjkahsdklsaj",
"9": "45.235",
"tenth": "45.235",
"10": "56.25",
"twelfth": "56.25"
}
]';
$json = json_decode($data, true);
echo '<pre>';
foreach ($json as $key => $value) {
echo "StudentID: ".$value['studentid']."<br>";
}
Output:
StudentID: 1
StudentID: 3
StudentID: 5
Decode ur array like below..
$newarr= json_decode('urjsonstring');
extract($newarr);
$query="insert into stud values($studentid, $firstname,$lastname...)";

Resources