Я пытаюсь создать бот-диссонанс, который запускает фильтр чата; моя конечная цель заключается в том, что бот должен иметь список слов, и если будет сказано одно из этих слов из списка, бот добавит к члену счет и если они будут похожи на 3, тогда будет запущена команда для запуска с сервера.
Создание списка очень просто, и я знаю, как написать команду, чтобы набить кого-то, но я абсолютно потерял то, как я заставлю бота отслеживать значения для каждого члена.... Мне нужно установить идентификатор члена как переменную?
Любая помощь приветствуется, я просто застрял и имею ограниченный опыт работы в модуле discord.py.
import asyncio
import json
import discord
from discord.ext import commands
from discord.ext.commands import Bot
bot = commands.Bot(command_prefix='#')
infractions = {}
blacklist = ["bad", "word"]
limit = 5
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.event
async def on_ready():
global infractions
try:
with open('infractions.json') as f:
infractions = json.load(f)
except FileNotFoundError:
print("Could not load infractions.json")
infractions = {}
@bot.event
async def on_message(message):
content = message.content.lower()
if any(word in content for word in blacklist):
id = message.author.id
infractions[id] = infractions.get(id, 0) + 1
if infractions[id] >= limit:
await bot.kick(message.author)
else:
warning = f"{message.author.mention} this is your {infractions[id]} warning"
await bot.send_message(message.channel, warning)
await bot.process_commands(message)
@bot.command()
async def save():
with open('infractions.json', 'w+') as f:
json.dump(infractions, f)
Вы можете сохранить словарь идентификаторов членов до количества нарушений. Вы можете получить идентификатор из атрибута message.author.id
from discord.ext import commands
infractions = {}
blacklist = ["bad", "word"]
limit = 5
bot = commands.Bot('!')
@bot.event
async def on_message(message):
content = message.content.lower()
if any(word in content for word in blacklist):
id = message.author.id
infractions[id] = infractions.get(id, 0) + 1
if infractions[id] >= limit:
await bot.kick(message.author)
else:
warning = f"{message.author.mention} this is your {infractions[id]} warning"
await bot.send_message(message.channel, warning)
await bot.process_commands(message)
Я предлагаю использовать ids как ключи, а не объекты- Member
чтобы вы могли легче сохранить файл как JSON, что позволит вам сохранять значения при отключении вашего бота. См. Другой ответ, который я написал для примера того, как это сделать