有什么办法可以使我自动预警和自动踢

时间:2019-04-16 12:37:49

标签: python-3.x discord discord.py-rewrite

错误:不,因为我还没有编写代码/我不知道如何编写代码,所以代码在下面

 @bot.event
 async def on_message(message):
     test = 'test'
     if test in message.content.lower():
          await message.author.send('you have been warned for using "bad words" in the server')

我希望我的代码向用户发出3次警告,然后在第4次该机器人踢用户在服务器中编写测试时发出警告,

1 个答案:

答案 0 :(得分:1)

您需要创建一个用户ID映射到被警告的次数。在僵尸程序处于脱机状态时,保留此地图的最简单方法是将其存储在JSON文件中,尽管随着僵尸程序的扩展,您最终应该采用数据库。

from discord.ext.commands import Bot
from json import load

bot = Bot("!")

try:
    with open("warns.json") as f:
        warns = load(f)
except:
    warns = {}

def should_warn(message):
    return 'test' in message.content.lower()

async def warn_or_kick(member):
    id = str(member.id)
    if id not in warns:
        warns[id] = 0
    warns[id] += 1
    if warns[id] >= 4:
        await member.send("You have been kicked")
        await member.kick()
    else:
        await member.send(f"This is warning number {warn[id]}")
    with open("warns.json") as f:
        dump(warns, f)

@bot.event
async def on_message(message):
    if bot.user == message.author:
        return 
    if should_warn(message):
        await warn_or_kick(message.author)
    else:
        await bot.process_commands(message)

bot.run("TOKEN")