问题
I am attempting to output the contents of a text file to a discord channel via Discord. The issue I have is that there is a 2000 character limit. I have been able to bypass this by using the code below
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())
Running this code leads to a 400 error stating cannot send blank message, which I think is due to there being extra file.read(2000) statements than there is content in the text file (error message below)
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
One positive is that all the data is being printed correctly but I just wanted to ask if anyone can think of a better method to do this that would result in the 400 error not showing?
Below is an example of the content of the text file that is being read above
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.
回答1:
Is there a reason you can't just do your read and your send separately and check if the read result is empty before you do your send?
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()
来源:https://stackoverflow.com/questions/62878525/python-discord-py-ways-to-split-output-to-bypass-2000-character-limit