Python discord.py 分割输出绕过 2000 个字符限制的方法

Python discord.py ways to split output to bypass 2000 character limit

我正在尝试通过 Discord 将文本文件的内容输出到 Discord 频道。我遇到的问题是有 2000 个字符的限制。我已经能够使用下面的代码绕过这个

async def on_message(message):
    id = client.get_guild(73194604108722****)
    channels = ["football"]
    
    
    
    if str(message.channel) in channels:
        if message.content.find("!english") != -1:

            with open('/home/brendan/Desktop/englishfootball.txt', 'r') as file:
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())
                await message.channel.send(file.read(2000).strip())

运行 此代码导致 400 错误,说明无法发送空白消息,我认为这是由于存在额外的 file.read(2000) 语句而不是文本文件中的内容 (下面的错误消息)

  File "discordbot.py", line 62, in on_message
    await message.channel.send(file.read(2000).strip())
  File "/usr/lib/python3.8/site-packages/discord/abc.py", line 856, in send
    data = await state.http.send_message(channel.id, content, tts=tts, embed=embed, nonce=nonce)
  File "/usr/lib/python3.8/site-packages/discord/http.py", line 225, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message

一个积极因素是所有数据都被正确打印,但我只是想问问是否有人能想到更好的方法来执行此操作,从而导致 400 错误不显示?

下面是上面读取的文本文件的内容示例

Competition English League One - League One Play-off FINAL 
Competitors Oxford United v Wycombe Wanderers 
Match Date Monday, 13th  July 
Match Time ST: 19:30 
Channels: beIN Connect MENA 
   beIN Sports MENA 12 HD 
   beIN Sports MENA 6 HD 
   Nova Sport (bulgaria) / HD 
   Setanta Sports Ukraine+ HD 
   Sky Sports Football UK / HD 
   Sportdigital TV HD 
   SportKlub 6 (slovenia) 
   Stöd 2 Sport 2 / HD 
-------------------------------------------------------------------------------- 
Competition English Premier League - Week 35 
Competitors Manchester United v Southampton 
Match Date Monday, 13th  July 
Match Time ST: 20:00 
Channels: beIN Connect MENA 
   beIN Sports MENA 11 HD 
   beIN Sports MENA 2 HD 
   Belarus 5 Internet  
   Cosmote Sport 1 HD 
   Cytavision Sports 1 HD 
   DAZN Canada   
   DAZN España   
   Diema Sport 2 HD 
   EuroSport 1 Romania / HD 
   Football HD (tjk) 
   H2 Armenia 
   Idman TV 
   Pick UK / HD 
   Premier Sport HD (cze/svk) 
   RMC Sport 1 HD 
   S Sport 1 (turkey) HD 
   Sky One (uk) / HD 
   Sky Sport 1 / HD Germany 
   Sky Sport Austria 1 HD 
   Sky Sport Football Italia / HD 
   Sky Sport Uno Italia / HD 
   Sky Sports Main Event / HD 
   Sky Sports Premier League / HD 
   Sky Sports Ultra HD (4K) 
   Sport 1 Israel / HD 
   SportKlub 2 (slovenia) 
   SuperSport 3 RSA / HD 
   SuperSport MáXimo 1 RSA 
   TSN Malta 2 HD 
   TV2 Sport Premium HD 
   TV2sumo.no   
   TV3+ (denmark) / HD 
   V Sport Extra HD (sweden) 
   V Sport Jalkapallo HD 
   V Sport Premium HD 
   Varzesh TV 3 Farsi HD 
   ViaPlay (denmark) / HD 
   ViaPlay (finland) / HD 
   ViaPlay (sweden) / HD 
   VOOsport World 1 / HD 
--------------------------------------------------------------------------------```


Thanks in advance to anyone who proposes a solution or provides advice.

您是否有理由不能单独进行读取和发送,并在发送之前检查读取结果是否为空?

with open('/home/brendan/Desktop/englishfootball.txt', 'r') as file:
    msg = file.read(2000).strip()
    while len(msg) > 0:
        await message.channel.send(msg)
        msg = file.read(2000).strip()