在 discord.py 重写中读取文本文件
Reading a text file in discord.py rewrite
我正在编写一个机器人,我已经完成了大部分,但遇到了瓶颈。
我有一个文本文件,我希望机器人搜索该文件以查看其中是否有某个词,如果有则回复是,如果没有回复则不是
我希望它像这样工作
!Platoon XXX (XXX= word from file)
机器人回复
Yes they are in platoon
或
No they are not in platoon
到目前为止我有
@client.command()
async def Platoon(ctx):
await ctx.send('Checking File please stand by.')
f = open("Platoon.txt", "r")
您可以尝试使用 re
库在每一行中查找单词。
import re
@client.command()
async def Platoon(ctx, *, word: str):
await ctx.send('Checking File please stand by.')
with open("Platoon.txt", "r") as f:
searching = re.compile(r'\b({0})\b'.format(word), flags=re.IGNORECASE).search
line = True
while line:
line = f.readline()
if not line:
break
if searching(line):
await ctx.send("Yes they are in platoon")
return
await ctx.send("No they are not in platoon")
我正在编写一个机器人,我已经完成了大部分,但遇到了瓶颈。
我有一个文本文件,我希望机器人搜索该文件以查看其中是否有某个词,如果有则回复是,如果没有回复则不是
我希望它像这样工作
!Platoon XXX (XXX= word from file)
机器人回复
Yes they are in platoon
或
No they are not in platoon
到目前为止我有
@client.command()
async def Platoon(ctx):
await ctx.send('Checking File please stand by.')
f = open("Platoon.txt", "r")
您可以尝试使用 re
库在每一行中查找单词。
import re
@client.command()
async def Platoon(ctx, *, word: str):
await ctx.send('Checking File please stand by.')
with open("Platoon.txt", "r") as f:
searching = re.compile(r'\b({0})\b'.format(word), flags=re.IGNORECASE).search
line = True
while line:
line = f.readline()
if not line:
break
if searching(line):
await ctx.send("Yes they are in platoon")
return
await ctx.send("No they are not in platoon")