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

2024-06-28 11:11:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为我的bot发出命令,切换automod(在调节之前,on_message事件将读取文件,如果是真的),但bot没有启动。请帮助:

命令:

@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事件(我如何使读取文件成为btw):

@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()

Tags: 文件命令messageasyncifondefbot
1条回答
网友
1楼 · 发布于 2024-06-28 11:11:50
if status='enable'

首先,比较使用两个=运算符,因此这两个运算符应该是

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

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

至于你的第二个问题(how to read a file in python),你可以通过谷歌搜索“python读取文件”很容易找到这个问题。试着在提问之前尽可能少地查找一些东西

相关问题 更多 >