python - Discord.py 文本写入和读取文本文件
问题描述
我想为我的机器人发出命令,切换自动模式(该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"
这很可能会使编译失败,因此不会启动。不过,您应该从中得到一个错误。如果这不是问题,那么您需要发布更多代码。
至于您的第二个问题(how to read a file in python
),您可以通过谷歌搜索“python 读取文件”轻松找到此问题。在提出问题之前,尽量少做一些事情来查找。
推荐阅读
- java - 检索并分配 jcr:mixinTypes magnolia 中的多值属性
- pyqt5 - 在 PyQt5 中获取 GUI 的标准调色板
- c# - Moq 验证特定异常是否被抛出并被捕获
- php - 为什么 get 返回 10 个项目,但 paginate 不返回任何项目?
- android - 在 FrameLayout 上带有覆盖屏幕的 Android YouTubePlayer
- c++ - 用地址初始化指针还是用新指针有区别吗?
- javascript - 如何在图例悬停时触发工具提示?
- python - 从 Pytest 夹具中调用自定义函数
- python - 如何使用pyqt5在UI的graphWidget中显示K表示聚类图?
- simple.odata.client - Simple.Odata.Client - 批处理