discord.py bot脚本不响应list.pop()函数。如何解决这个问题?

2024-09-30 14:31:17 发布

您现在位置:Python中文网/ 问答频道 /正文

相当愚蠢的情况下,我已经做了一个不和谐的机器人,从一个特定的网站,并应打印出一个笑话后,命令“!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通过删除打印出来的笑话来让机器人更新笑话列表


Tags: oftextimportsend列表botpagescript
2条回答

注意:我不熟悉Discord机器人。这只是解决问题的常用方法

您可以尝试将所有打印的笑话存储在一个列表中,并检查每次打印的笑话是否更早

jokes = []    #Create a empty list
    
def get_and_print():    #Get your jokes
    while True:
        #get joke as myJoke
        if myJoke in jokes:
            #get new joke
        else:
            break
    print(myJoke)
    jokes.append(myJoke)    #Insert it in jokes list

如果列表大小超过100,您也可以删除任何笑话

if len(jokes) > 100:
    jokes.pop(-1)

两个(甚至三个)同名的不同变量anekdots存在问题

存在全局变量

anekdots = []

局部变量

anekdots = anekdot_script(...)

它实际上不是局部变量,而是在

async def anekdot(..., anekdots = ...)

所有这些都会造成混乱,因为您认为您分配给了全局变量,但代码将其分配给了局部变量,下次它再次使用anekdots = []时,它运行anekdot_script(),并再次读取所有笑话

这对我有用

anekdots = []
n_of_page = 1

@bot.command()
async def anekdot(ctx):
    global anekdots   # inform function that `anekdots = ...` has to assign to global variable instead of creating local one
    global n_of_page  # inform function that `n_of_page = ...` has to assign to global variable instead of creating local one

    anekdots, n_of_page = anekdot_script(anekdots, n_of_page)

def anekdot_script(anekdots, n_of_page):
    # ... code ...
    return anekdots, n_of_page # need to return also `n_of_page`

完整的工作代码,但在命令!anekdot中没有额外的参数

import discord
from discord.ext import commands
import requests
from bs4 import BeautifulSoup
 
def anekdot_script(anekdots, n_of_page): #return list of anekdots of a specific page
    url = 'https://humornet.ru/anekdot/evrei/page/{}/'

    if not 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 anekdots, n_of_page # need to return also `n_of_page`


bot = commands.Bot(command_prefix= '!')

anekdots = []
n_of_page = 1

@bot.command()
async def anekdot(ctx):
    global anekdots   # inform function that `anekdots = ...` has to assign to global variable instead of creating local one
    global n_of_page  # inform function that `n_of_page = ...` has to assign to global variable instead of creating local one

    anekdots, n_of_page = 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!')

import os
TOKEN = os.getenv('DISCORD_TOKEN')

bot.run(TOKEN)

相关问题 更多 >