So, i'm trying to make a YouTube channel search, I've made a simple code, but it didn't work, It always sends https://www.youtube.com/channel/v=XIDzSr3oX1w%22,%22webPageT, (Not a valid link),
The code is
#client.command()
async def channel(ctx, *, search):
query_string = urllib.parse.urlencode({
'search_query': search
})
htm_content = urllib.request.urlopen(
'https://www.youtube.com/results?' + query_string
)
search_results = re.findall(r'/watch\?v=(.{24})', htm_content.read().decode())
await ctx.send('https://www.youtube.com/channel/' + search_results[0])
You cannot parse the search results in youtube. Youtube is a dynamically generated website. If you want to actually parse youtube you will probably need to use youtube API or using python selenium.
Related
For a get request, I am trying to user order by like below but always get a 400 bad request. Here, multiple users can have multiple blog posts each with a unique id like b1 in screenshot below. The long sequence of characters is the uid of a user under blogs. Each user has their own uid.
https://assignment-c3557-default-rtdb.asia-southeast1.firebasedatabase.app/blogs.json?orderBy="createdAt"
I followed the documentation here
https://firebase.google.com/docs/database/rest/retrieve-data
All I am doing is issuing a simple GET request in react js as follows:
const resp = await fetch(`https://assignment-c3557-default-rtdb.asia-southeast1.firebasedatabase.app/blogs.json?orderBy="createdAt"``)
const data = await resp.json()
if(!resp.ok) { ... }
Below is the database single entry for schema reference
As I said in my previous comment, this URL is invalid:
https://assignment-c3557-default-rtdb.asia-southeast1.firebasedatabase.app/blogs.json/orderBy="createdAt"
^
The query portion of a URL starts with a ? character.
https://assignment-c3557-default-rtdb.asia-southeast1.firebasedatabase.app/blogs.json?orderBy="createdAt"
^
Firebase Realtime Database - Adding query to Axios GET request?
I followed the above similar issue resolution to solve my problem
async def _ping(ctx): # Defines a new "context" (ctx) command called "ping."
async def command_hi(ctx):
await ctx.defer()
await ctx.send("Pong!")
pass
I was making slash commands and first time it worked but secondtime, it does not work.
I tried await ctx.defer() but it's not working either
Hey #Justnoob I recommend you to use Pycord, It has inbuilt Buttons, Slash, Dropdowns and much more but for now the discord api with application commands is a bit complicated we usually use ctx.send but in app commands it is ctx.respond so here is the code:-
async def ping(ctx): #Don't use _ping too.
await ctx.respond("Pong!")
And I still don't understand why you created two async def in one command. There should be only one and you already specified it.
So I want to make my bot give a youtube link with multiple names of commands
#client.command(aliases=['youtube', 'yt'])
async def youtube (ctx):
embed = nextcord.Embed(
title="Youtube", url="https://www.youtube.com/",
description="Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.",
colour = nextcord.Colour.brand_red(),
)
await ctx.send(embed=embed)```
[the is an error][1]
[1]: https://i.stack.imgur.com/xrzG5.png
Based on what I assume nextcord is (an extension of discord.py?), you cannot have a command alias of a command that already exists. The command is already called youtube, so you cannot you cannot have youtube as an alias. You'll need to remove it.
#client.command(aliases=['yt'])
async def youtube (ctx):
embed = nextcord.Embed(
title="Youtube",
url="https://www.youtube.com/",
description="Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.",
colour = nextcord.Colour.brand_red(),
)
await ctx.send(embed=embed)
Okay so I know that there is a way to open let's say a wiki. By typing a command(archive) it will then search the wiki and return with a message containing certain information about what you searched for.
I know that you can use webhooks for this, but I don't know how. Can anyone of you help?
There are multiple ways to get a summary of a page from a wiki. The easiest of them is probably the TextExtracts extension that allows you to get the plain text of some article. However, this only works if the wiki in question has that extension installed -- the Star Wars Fandom wiki that you wanted to work with does not. Nevertheless, this should give you an idea of how to use the MediaWiki API to get information about articles from there.
import discord
from discord.ext import commands
import aiohttp
bot = commands.Bot(command_prefix='-')
WIKI_ENDPOINT = 'https://en.wikipedia.org/w/api.php'
NUMBER_OF_SENTENCES = 3 # (must be between 1 and 10)
#bot.command('lookup')
async def lookup_wiki(ctx, *query_elements):
query = ' '.join(query_elements)
params = {
"action": "query",
"prop": "extracts",
"titles": query,
"exsentences": NUMBER_OF_SENTENCES,
"explaintext": "true",
"format": "json",
"redirects": "true"
}
headers = {"User-Agent": "BOT-NAME_CHANGE-ME/1.0"}
async with aiohttp.ClientSession() as session:
async with session.get(WIKI_ENDPOINT, params=params, headers=headers) as r:
data = await r.json()
pages = data['query']['pages']
page = pages[ list(pages)[0] ]
try:
extract = page['extract']
if extract is None: raise ValueError
except:
extract = f"We could not fetch an extract for this page. Perhaps it does not exist, or the wiki queried does not support the **TextExtracts** MediaWiki extension: https://www.mediawiki.org/wiki/Extension:TextExtracts\nThe page data received is: `{page}`"
await ctx.send(extract)
bot.run('your bot token here')
So I want to create a bot that will go to a website and fetch a number from the website. So let us say we have a website like this
A basic website
I want my bot to find that the value of that number is 5, then post a message saying something like "the number is 5!" Thanks so much, everyone!
You should use aiohttp which is already included with discord.py. requests would result in blocking
Here is a simple command to get the data, either as JSON or text.
#bot.command()
async def get_info(ctx, *, link):
async with aiohttp.ClientSession() as session:
async with session.get(link) as r:
if r.status == 200:
info = await r.json()
#or if it is text
info = await r.text()
await ctx.send(info)
Simple use case
Json:
get_info https://uselessfacts.jsph.pl/random.txt
Text:
get_info https://uselessfacts.jsph.pl/random.json