Python discord.py分割输出以绕过2000个字符限制的方法
我试图通过discord将文本文件的内容输出到discord频道。我的问题是有2000个字符的限制。我已经能够通过使用下面的代码绕过这个问题Python discord.py分割输出以绕过2000个字符限制的方法,python,text-files,discord.py,Python,Text Files,Discord.py,我试图通过discord将文本文件的内容输出到discord频道。我的问题是有2000个字符的限制。我已经能够通过使用下面的代码绕过这个问题 async def on_message(message): id = client.get_guild(73194604108722****) channels = ["football"] if str(message.channel) in channels:
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.read2000语句,而不是下面文本文件错误消息中的内容
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 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()
比赛英国甲级联赛-甲级决赛
竞争对手牛津联队对维康比流浪者队
比赛日期:7月13日星期一
比赛时间:19:30
频道:beIN Connect MENA有什么原因不能单独执行读取和发送,并在发送前检查读取结果是否为空
打开“/home/brendan/Desktop/englishfootball.txt”,“r”作为文件:
msg=file.read2000.strip
当lenmsg>0时:
等待message.channel.sendmsg
msg=file.read2000.strip
有什么原因不能在发送之前,将读取和发送分开并检查读取结果是否为空
打开“/home/brendan/Desktop/englishfootball.txt”,“r”作为文件:
msg=file.read2000.strip
当lenmsg>0时:
等待message.channel.sendmsg
msg=file.read2000.strip