Separating index values from an array inside discord embed - discord.py

I'm looking for a way to spit out an embed that lists all the "suggestions" in my database with their corresponding index values like this. So far I can send an embed with all of the information like this but that just adds the information as 2 strings after each other (suggestions then the index values).
I need a way to display the "suggestions" in the embed with the index values separately like this:
[#1] suggestion 1
[#2] suggestion 2
[#3] suggestion 3
[#4] suggestion 4
Ideally this would all be done within one embed field.
I'm sorry if this was poorly worded but if I knew how to describe what I'm looking for, I might have found the answer sooner. Thanks for any advice in advance.
(I'm using the replit database - if any more of the code is needed please let me know)
#client.command()
async def suggestions(ctx):
suggestions = []
if "suggestions" in db.keys():
suggestions = db["suggestions"]
joined = '\n'.join([str(elem) for elem in suggestions])
index_number = '\n'.join([str(i) for i in range(len(suggestions))])
channel = client.get_channel(0000000000000)
suggestionsEmbed=discord.Embed(title="Server Suggestions", description="Vote for suggestions with **$vote #** Remove suggestions with **$del #**", color=0x15e538)
suggestionsEmbed.set_thumbnail(url="https://www.startup.pk/wp-content/uploads/2020/07/ideaideabulblightbulbicon-1320144733751939202.png")
suggestionsEmbed.add_field(name='Suggestions Created', value=f'{joined} **#{index_number}**')
await channel.send(embed=suggestionsEmbed)

Related

IFTTT JavaScript filter - How to make case insensitive searches + How to search Include and Exclude sets of terms

First off I'm a total novice for Javascript, so please go gently. I'm aware of how people feel about having to now pay for IFTTT, but it's perfect for what I need.
I am using a more expansive version of this code below to capture certain keywords from Tweets to then generate emails if the search returns a positive result. This search works very nicely, except it is case sensitive which is a problem.
Yes, I know you can manipulate the twitter search to pick up specific words or phrases. I am very proficient in achieving searches this way. I am casting a wide net to pick up approx 120 search words or phrases which is too long to achieve through "OR" Twitter search parameters alone which is why I'm using this.
Q1 - I have tried adding item.toLowerCase() and just .toLowerCase() in various parts of the code so it wouldn't matter if the sentence case of the search term is different to that of the original tweet text case. I just can't get it to work though. I've seen various posts on here but I can't get any of them to work in IFTTT. I believe IFTTT doesn't accept REGEX either, which is annoying.
Any advice of how to get this code running so it's case-insensitive for text within IFTTT?
Q2 - I have approx 120 search terms for the tweet text to return positive results. There is a lot of junk that comes through with that. Does anyone know how to add a second layer of 'and exclude' search terms?
I have something like 300-400 words and specific phrases which would be used to stop the email from being triggered - so it'd be something like "IF tweet text contains a, b, c BUT text ALSO contains x, y, z... do not send the email"
let str=Twitter.newTweetFromSearch.Text;
let searchTerms=[
"Northbound",
"Westbound",
"Southbound",
"Eastbound"
]
let foundOne=0;
if(searchTerms.some(function(v){return str.indexOf(v)>=0;})){
foundOne=1;
}
if(foundOne==0){
Email.sendMeEmail.skip();
}
I have looked at the Twitter API, but that is a step too far for my coding ability which is why I'm using IFTTT.
Any help is very much appreciated
Thank you.
I'm playing with IFTTT Filter myself at the moment, so here are some thoughts about solving your solution.
If you want to do a case insensitive seatch on the original text, convert the original text to lowercase, then have all your search terms in lowercase.
Plus I think you want to iterate over the searchTerms array, and use the includes() method. Ok, just realised that .some() does the iteration for you, but I prefer includes() over indexof().
let str=Twitter.newTweetFromSearch.Text.toLowerCase();
let searchTerms=[
"northbound",
"westbound",
"southbound",
"eastbound"
]
let foundOne=0;
if(searchTerms.some(function(term){return str.includes(term);})){
foundOne=1;
}
if(foundOne==0){
Email.sendMeEmail.skip();
}
Or you could just skip having the foundOne variable, and do the search in the if() statement.
let str=Twitter.newTweetFromSearch.Text.toLowerCase();
let searchTerms=[
"northbound",
"westbound",
"southbound",
"eastbound"
]
if(!searchTerms.some(function(term){return str.includes(term);})){
Email.sendMeEmail.skip();
}

Scan result of a aggregation

I wanted to see how many unique link that a user has posted for every user. Here is what I have come up so far
s.aggs.bucket('user_term', A('terms', field='user__id')).metric('url_count', A('value_count', field='link'))
However, I have yet found a way to iterate through that result. Is there a way for this?
This will not give you a unique count, just a number of docs with a value for that field, you want to use a cardinality instead:
s.aggs.bucket('users', 'terms', field='user.id').metric('url_count', 'cardinality', field='link')
r = s.execute()
for user in r.aggregations.users.buckets:
print(f'User {user.key} posted {user.url_count.value} links')
Hope this helps

Scraping all data from Reddit searches

I am using PRAW to scrape data off of reddit. I am using the .search method to search very specific people. I can easily print the title of the submission if the keyword is in the title, but if the keyword is in the text of the submission nothing pops up. Here is the code I have so far.
import praw
reddit = praw.Reddit(----------)
alls = reddit.subreddit("all")
for submission in alls.search("Yoa ming",sort = comment, limit = 5):
print(submission.title)
When I run this code i get
Yoa Ming next to Elephant!
Obama's Yoa Ming impression
i used to yoa ming... until i took an arrow to the knee
Could someone make a rage face out of our dearest Yoa Ming? I think it would compliment his first one so well!!!
If you search Yoa Ming on reddit, there are posts that dont contain "Yoa Ming" in the title but "Yoa Ming" in the text and those are the posts I want.
Thanks.
You might need to update the version of PRAW you are using. Using v6.3.1 yields the expected outcome and includes submissions that have the keyword in the body and not the title.
Also, the sort=comment parameter should be sort='comments'. Using an invalid value for sort will not throw an error but it will fall back to the default value, which may be why you are seeing different search results between your script and the website.

Sorting by counting the intersection of two lists in MongoDB

We have a posting analyzing requirement, that is, for a specific post, we need to return a list of posts which are mostly related to it, the logic is comparing the count of common tags in the posts. For example:
postA = {"author":"abc",
"title":"blah blah",
"tags":["japan","japanese style","england"],
}
there are may be other posts with tags like:
postB:["japan", "england"]
postC:["japan"]
postD:["joke"]
so basically, postB gets 2 counts, postC gets 1 counts when comparing to the tags in the postA. postD gets 0 and will not be included in the result.
My understanding for now is to use map/reduce to produce the result, I understand the basic usage of map/reduce, but I can't figure out a solution for this specific purpose.
Any help? Or is there a better way like custom sorting function to work it out? I'm currently using the pymongodb as I'm python developer.
You should create an index on tags:
db.posts.ensure_index([('tags', 1)])
and search for posts that share at least one tag with postA:
posts = list(db.posts.find({_id: {$ne: postA['_id']}, 'tags': {'$in': postA['tags']}}))
and finally, sort by intersection in Python:
key = lambda post: len(tag for tag in post['tags'] if tag in postA['tags'])
posts.sort(key=key, reverse=True)
Note that if postA shares at least one tag with a large number of other posts this won't perform well, because you'll send so much data from Mongo to your application; unfortunately there's no way to sort and limit by the size of the intersection using Mongo itself.

sorting array of object in backbone js or underscore js

[
{"group_id":1,"name":"All Area","parent_id":0,"date_created":1250172000,"type":"area","description":null},
{"group_id":2,"name":"IT","parent_id":5,"date_created":1250172000,"type":"area","description":null},
{"group_id":3,"name":"BPO","parent_id":5,"date_created":1250172000,"type":"area","description":null},
{"group_id":4,"name":"Engineering","parent_id":5,"date_created":1250172000,"type":"area","description":null},
{"group_id":5,"name":"Baroda","parent_id":6,"date_created":1250172000,"type":"site","description":null},
{"group_id":6,"name":"Gujarat","parent_id":0,"date_created":1250172000,"type":"site","description":null},
{"group_id":7,"name":"Surat","parent_id":10,"date_created":1250172000,"type":"site","description":null},
{"group_id":8,"name":"IT","parent_id":7,"date_created":1250172000,"type":"site","description":null},
{"group_id":9,"name":"Ahemadabad","parent_id":10,"date_created":1250172000,"type":"site","description":null},
]
I want to generate one list of all the data using backbone js or underscore js in such a way that each item comes under its parent.
i.e. Here data with name 'Gujarat' should come first as it has no parent. Underneath that item name with Baroda should come. Under Baroda, item names whose parent_id is group_id of Baroda should come i.e (item with group_id 2,3,4). Under Surat will come all its children. As seen, parent_id of Surat and Ahemdadabad is 10 but there is no item with group_id
of 10 listed here. So Surat and Ahemdadabad also become main items.
All data should be placed in one unorder-list.
Please see below for the output format. I need to generate a view in backbone using this model data.
All areas
Gujarat
Baroda
IT
BPO
Engineering
Surat
IT
Ahemadabad
Does anyone has solution for this?
Thanks in advance.
I suggest you go read up on Backbone collections here:
http://backbonetutorials.com/what-is-a-collection/
and if you need info on how to sort or filter you can find helper functions here: http://documentcloud.github.com/backbone/#Collection-Underscore-Methods (especially the collection.sortBy should be of help)
remark
Sorry i was so blunt in the comment, but this is a platform specifically for people with a problem / error on which other people can comment what the solution can be, we are not here to write a whole page for you.

Resources