Discord.py 文本写入和读取文本文件

Discord.py text writing and reading text file

我想为我的机器人发出命令,以切换 automod(on_message 事件将读取文件,如果它在审核之前为真),但机器人没有启动。请帮助:

命令:

@bot.command()
async def automod(ctx, status):
    if status='enable':
        with open('automod.txt', 'w') as wf:
            wf.write("true")
    if status='disable':
        with  open('automod.txt', 'w') as wf:
            wf.write("false")

on_message 事件(顺便说一句,我怎样才能读取文件):

@bot.event
async def on_message(message):
    for word in filtered_words:
        if word in message.content:
            await message.delete()
            botmsg1 = await message.channel.send(f'Deleted {message.author.mention} for using bad words.')
            await asyncio.sleep(5)
            await botmsg1.delete()
if status='enable'

首先比较用到两个=运算符,所以这两个应该是

if status == "enable"
..
if status == "disable"

这很可能会导致编译失败,因此无法启动。你应该从中得到一个错误。如果那不是问题,那么您需要 post 更多代码。

关于你的第二个问题(how to read a file in python),你可以通过谷歌搜索“python读取文件”轻松找到这个问题。在提问之前尽量少查资料。