【发布时间】:2020-08-30 11:36:20
【问题描述】:
我试图让这个机器人有一个线程,以便它每 24 小时执行一次 almanax 协程而不使用任何命令,但是当我尝试在另一个线程中这样调用它时,我收到一个错误 (TypeError: almanax() missing 1 个必需的位置参数:'ctx')它需要 ctx,我已经尝试过,但我只是卡住了。我对此完全陌生。
import discord
from discord.ext import commands
import urllib.request
import re
import datetime
from bs4 import BeautifulSoup
import requests
import asyncio
bot = commands.Bot(command_prefix='-', description= "Este es un DuckBot")
sourceLinkAlmanax = 'http://www.krosmoz.com/es/almanax'
fechaexacta = '{0:%d-%m-%Y}'.format(datetime.datetime.now())
async def dailyAlmanax():
while 1:
await asyncio.sleep(5) #86400
await almanax()
@bot.command()
async def almanax(ctx):
print("Procesando almanax")
source = requests.get(sourceLinkAlmanax).text
soup = BeautifulSoup(source, 'lxml')
mision = soup.find('div', class_='mid').p.text
bonus = soup.find('div', class_='more').getText()
ofrenda = soup.find('div', class_='more-infos-content').p.text
bonus = bonus.replace(mision, "")
bonus = bonus.replace(ofrenda, "")
linkImagen = soup.find('div', {"class": "more-infos"}).img['src']
fechaexacta = '{0:%d-%m-%Y}'.format(datetime.datetime.now())
mensaje = discord.Embed(title = "`Duckmanax del " + fechaexacta + "`", url=sourceLinkAlmanax, color=0xe5be01)
mensaje.add_field(name="Mision: ", value=f"{mision}", inline=False)
mensaje.add_field(name="Bonus: ", value=f"{bonus.strip()}", inline=False)
mensaje.add_field(name="Ofrenda: ", value=f"{ofrenda.strip()}", inline=False)
mensaje.set_image(url=linkImagen)
await ctx.send(embed = mensaje)
print("Almanax enviado")
@bot.command()
async def salmanax(ctx, busqueda: str):
print("Procesando busqueda de almanax")
fecha = datetime.datetime.now()
año = fecha.year
smes = fecha.month
sdia = fecha.day
for mes in range (smes,13):
if mes > smes:
sdia = 1
for dia in range (sdia,32):
print("Procesando Año:", año, "Mes:", mes, "Dia:", dia, "Buscando:", busqueda)
if mes < 10:
mes2 = "0" + str(mes)
else:
mes2 = mes
if dia < 10:
dia2 = "0" + str (dia)
else:
dia2 = dia
link = "http://www.krosmoz.com/es/almanax/" + str(año) + "-" + str(mes2) + "-" + str(dia2)
try:
data = urllib.request.urlopen(link).read().decode('utf-8')
except Exception as error:
pass
for linea in data.split("/n"):
try:
if re.findall(busqueda, linea, re.IGNORECASE):
await ctx.send("Encontre esta coincidencia de " + busqueda + " " + link)
except Exception as error2:
pass
print("Busqueda de almanax finalizada")
@bot.event
async def on_message(ctx):
if ctx.channel.name == 'almanax':
await bot.process_commands(ctx)
@bot.event
async def on_ready():
print("Bot listo")
await bot.change_presence(activity=discord.Streaming(name="-almanax",url="https://www.twitch.tv/kerdrai"))
bot.loop.create_task(dailyAlmanax())
bot.run(token)
【问题讨论】:
-
您的 dailyAlmanax 函数在 almanax() 上等待。你调用这个函数时不带参数,但它需要一个,就像错误消息告诉你的那样。顺便说一句,您不是从另一个线程而是从另一个协程调用它。
标签: python discord python-asyncio discord.py python-multithreading