相当愚蠢的情况下,我已经做了一个不和谐的机器人,从一个特定的网站,并应打印出一个笑话后,命令“!anekdot”
机器人本身工作,它打印出第一个笑话。但在打印第一个笑话后,机器人必须将其弹出。但它并没有做到这一点(anekdots的类型及其长度已被用于了解是否有任何变化,以及我是否可以将list的函数应用于anekdots list):
import discord
from discord.ext import commands
from script import anekdot_script
bot = commands.Bot(command_prefix= '!')
anekdots = []
n_of_page = 1
@bot.command()
async def anekdot(ctx, anekdots = anekdots, n_of_page = n_of_page):
anekdots = anekdot_script(anekdots, n_of_page)
if anekdots:
await ctx.send(type(anekdots))
await ctx.send(anekdots[0].text)
anekdots.pop(0)
await ctx.send(len(anekdots))
else:
await ctx.send('Nothing left! Find new web-site!')
bot.run('ID')
我认为将anekdots和n_页面变量设置为全局变量,甚至不必出现问题。 不一致的输出(完全不变):
<class 'list'>
The joke in russian...
24
脚本只检查列表是否为空,如果为空-脚本将刷新页面,创建笑话列表并返回:
def anekdot_script(anekdots, n_of_page): #return list of anekdots of a specific page
url = 'https://humornet.ru/anekdot/evrei/page/{}/'
if anekdots == []:
response = requests.get(url.format(n_of_page)) #getting page info
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'lxml') #parses the page to html format
anekdots = soup.select('.text') #anekdots have class 'text' on this page
n_of_page += 1
return list(anekdots)
我也试过切片,一点帮助都没有。 我不理解hot通过删除打印出来的笑话来让机器人更新笑话列表
注意:我不熟悉Discord机器人。这只是解决问题的常用方法
您可以尝试将所有打印的笑话存储在一个列表中,并检查每次打印的笑话是否更早
如果列表大小超过100,您也可以删除任何笑话
两个(甚至三个)同名的不同变量
anekdots
存在问题存在全局变量
局部变量
它实际上不是局部变量,而是在
所有这些都会造成混乱,因为您认为您分配给了全局变量,但代码将其分配给了局部变量,下次它再次使用
anekdots = []
时,它运行anekdot_script()
,并再次读取所有笑话这对我有用
与
完整的工作代码,但在命令
!anekdot
中没有额外的参数相关问题 更多 >
编程相关推荐