使用discord.py在反垃圾邮件机器人上工作,这段代码运行,但没有做它应该做的事情



在一个反垃圾邮件机器人上工作,该机器人将作者ID记录到.txt文件中(每10秒清除一次(,该代码运行,但我没有看到任何作者名称填充在我的txt文件中。

  • 注意,我删除了带有用户密钥的代码部分,所以我列出的只是与实际反垃圾邮件功能相关的代码

如有任何帮助,我们将不胜感激。

import asyncio
import os
import json
import time
import random
import discord
import datetime
from discord.ext import commands
from discord.ext.commands import bot
## On Logon ##
@client.event
async def on_ready():
print("Ready")
while True:
print("cleared")
await asyncio.sleep(10)
with open("spam-bank.txt", "r+") as file:
file.truncate(0)
async def on_message(message):
counter = 0
with open("spam-bank.txt", "r+") as file:
for lines in file:
if lines.strip("n") == str(message.author.id):
counter+=1

file.writelines(f"{str(message.author.id)}n")
if counter > 5:
await message.guild.ban(message.author, reason="Caught by bot Spamming")
await asyncio.sleep(1)
await message.guild.unban(message.author)
print("Flagged by bot for spamming chat..")

原来我在异步defon_message 之前缺少@client.event

相关内容

最新更新