我如何在 discord.py 中创建存档命令?
How do i make a archive command in discord.py?
好的,我知道有一种方法可以打开 wiki。通过键入命令(存档),它会搜索 wiki 和 return 并显示一条消息,其中包含有关您搜索内容的某些信息。
我知道您可以为此使用 webhook,但我不知道如何使用。你们谁能帮忙吗?
有多种方法可以从 wiki 获取页面摘要。其中最简单的可能是您不想使用的 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。不过,这应该能让您了解如何使用 MediaWiki API 从那里获取有关文章的信息。
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')
好的,我知道有一种方法可以打开 wiki。通过键入命令(存档),它会搜索 wiki 和 return 并显示一条消息,其中包含有关您搜索内容的某些信息。
我知道您可以为此使用 webhook,但我不知道如何使用。你们谁能帮忙吗?
有多种方法可以从 wiki 获取页面摘要。其中最简单的可能是您不想使用的 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。不过,这应该能让您了解如何使用 MediaWiki API 从那里获取有关文章的信息。
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')