Skip to content

Commit

Permalink
spostata l'apertura del file in una funzione
Browse files Browse the repository at this point in the history
  • Loading branch information
Skylake-dev committed Mar 7, 2021
1 parent 2754590 commit cae0e66
Showing 1 changed file with 88 additions and 96 deletions.
184 changes: 88 additions & 96 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ async def on_message_delete(message):
"""
if message.author == bot.user or message.author.bot:
return
try:
with open('aflers.json','r') as file:
prev_dict = json.load(file)
except FileNotFoundError:
prev_dict = open_file()
if not prev_dict:
return
try:
d = prev_dict.get(str(message.author.id))
Expand Down Expand Up @@ -233,11 +231,10 @@ async def unwarn(ctx, member: discord.Member):
@bot.command(aliases=['warnc', 'wc'])
async def warncount(ctx, member:discord.Member = None):
"""stampa nel canale in cui viene chiamato l'elenco di tutti i warn degli utenti. può accettare come parametro un
username (tramite menzione) e in tal caso stampa i warn riguardanti quel membro. Può essere chiamata da chiunque"""
try:
with open('aflers.json','r') as file:
prev_dict = json.load(file)
except FileNotFoundError:
username (tramite menzione) e in tal caso stampa i warn riguardanti quel membro. Può essere chiamata da chiunque
"""
prev_dict = open_file()
if not prev_dict:
await ctx.send('nessuna attività registrata', delete_after=5)
await ctx.message.delete(delay=5)
return
Expand Down Expand Up @@ -301,10 +298,8 @@ async def periodic_checks():
L'avvio è programmato ogni giorno allo scatto della mezzanotte.
"""
print('controllo conteggio messaggi')
try:
with open('aflers.json','r') as file:
prev_dict = json.load(file)
except FileNotFoundError:
prev_dict = open_file()
if not prev_dict:
return
for key in prev_dict:
item = prev_dict.get(key)
Expand Down Expand Up @@ -352,38 +347,30 @@ def update_counter(message):
"""
if not does_it_count(message):
return
prev_dict = {}
try:
with open('aflers.json','r') as file:
prev_dict = json.load(file)
except FileNotFoundError:
print('file non trovato, lo creo ora')
with open('aflers.json','w+') as file:
prev_dict = {} #dizionario per permettere di cercare dell'ID facilmente
finally:
key = str(message.author.id)
if key in prev_dict:
d = prev_dict.get(key)
#incrementa solo il campo corrispondente al giorno corrente
d[weekdays.get(datetime.today().weekday())] += 1
else:
#contatore per ogni giorno per ovviare i problemi discussi nella issue #2
afler = {
"mon": 0,
"tue": 0,
"wed": 0,
"thu": 0,
"fri": 0,
"sat": 0,
"sun": 0,
"violations_count": 0,
"last_violation_count": None,
"active": False,
"expiration": None
}
afler[weekdays.get(datetime.today().weekday())] = 1
prev_dict[message.author.id] = afler
update_json_file(prev_dict, 'aflers.json')
prev_dict = open_file()
key = str(message.author.id)
if key in prev_dict:
d = prev_dict.get(key)
#incrementa solo il campo corrispondente al giorno corrente
d[weekdays.get(datetime.today().weekday())] += 1
else:
#contatore per ogni giorno per ovviare i problemi discussi nella issue #2
afler = {
"mon": 0,
"tue": 0,
"wed": 0,
"thu": 0,
"fri": 0,
"sat": 0,
"sun": 0,
"violations_count": 0,
"last_violation_count": None,
"active": False,
"expiration": None
}
afler[weekdays.get(datetime.today().weekday())] = 1
prev_dict[message.author.id] = afler
update_json_file(prev_dict, 'aflers.json')

def does_it_count(message):
"""Controlla se il messaggio ricevuto rispetta le condizioni per essere conteggiato ai fini del ruolo attivo"""
Expand All @@ -399,6 +386,19 @@ def update_json_file(data, json_file):
with open(json_file, 'w') as file:
json.dump(data, file, indent=4)

def open_file():
"""apre il file aflers.json, se non esiste ne crea uno vuoto
ritorna il contenuto del file.
"""
try:
with open('aflers.json','r') as file:
prev_dict = json.load(file)
except FileNotFoundError:
with open('aflers.json','w+') as file:
prev_dict = {}
finally:
return prev_dict

def count_messages(item):
"""Ritorna il conteggio totale dei messaggi dei 7 giorni precedenti"""
count = 0
Expand Down Expand Up @@ -426,58 +426,50 @@ def contains_banned_words(text):

async def add_warn(member, reason, number):
"""incrementa o decremente il numero di violazioni di numero e tiene traccia dell'ultima violazione commesa"""
prev_dict = {}
prev_dict = open_file()
penalty = 'warnato.'
try:
with open('aflers.json','r') as file:
prev_dict = json.load(file)
except FileNotFoundError:
print('file non trovato, lo creo ora')
with open('aflers.json','w+') as file:
prev_dict = {}
finally:
key = str(member.id)
if key in prev_dict:
d = prev_dict.get(key)
d["violations_count"] += number
d["last_violation_count"] = datetime.date(datetime.now()).__str__()
if d["violations_count"] < 0:
d["violations_count"] = 0
d["last_violation_count"] = None
return
if number < 0: #non deve controllare se è un unwarn
return
if d["violations_count"] == 3:
await member.add_roles(bot.get_guild(GUILD_ID).get_role(UNDER_SURVEILLANCE_ID))
penalty = 'sottoposto a sorveglianza, il prossimo sara\' un ban.'
channel = await member.create_dm()
await channel.send('Sei stato ' + penalty + ' Motivo: ' + reason + '.')
elif d["violations_count"] == 4:
penalty = 'bannato dal server.'
channel = await member.create_dm()
await channel.send('Sei stato ' + penalty + ' Motivo: ' + reason + '.')
await member.ban(delete_message_days = 0, reason = reason)
else:
channel = await member.create_dm()
await channel.send('Sei stato ' + penalty + ' Motivo: ' + reason + '.')
key = str(member.id)
if key in prev_dict:
d = prev_dict.get(key)
d["violations_count"] += number
d["last_violation_count"] = datetime.date(datetime.now()).__str__()
if d["violations_count"] < 0:
d["violations_count"] = 0
d["last_violation_count"] = None
return
if number < 0: #non deve controllare se è un unwarn
return
if d["violations_count"] == 3:
await member.add_roles(bot.get_guild(GUILD_ID).get_role(UNDER_SURVEILLANCE_ID))
penalty = 'sottoposto a sorveglianza, il prossimo sara\' un ban.'
channel = await member.create_dm()
await channel.send('Sei stato ' + penalty + ' Motivo: ' + reason + '.')
elif d["violations_count"] == 4:
penalty = 'bannato dal server.'
channel = await member.create_dm()
await channel.send('Sei stato ' + penalty + ' Motivo: ' + reason + '.')
await member.ban(delete_message_days = 0, reason = reason)
else:
#contatore per ogni giorno per ovviare i problemi discussi nella issue #2
if number < 0:
return
afler = {
"mon": 0,
"tue": 0,
"wed": 0,
"thu": 0,
"fri": 0,
"sat": 0,
"sun": 0,
"violations_count": number,
"last_violation_count": datetime.date(datetime.now()).__str__(),
"active": False,
"expiration": None
}
prev_dict[key] = afler
update_json_file(prev_dict, 'aflers.json')
channel = await member.create_dm()
await channel.send('Sei stato ' + penalty + ' Motivo: ' + reason + '.')
else:
#contatore per ogni giorno per ovviare i problemi discussi nella issue #2
if number < 0:
return
afler = {
"mon": 0,
"tue": 0,
"wed": 0,
"thu": 0,
"fri": 0,
"sat": 0,
"sun": 0,
"violations_count": number,
"last_violation_count": datetime.date(datetime.now()).__str__(),
"active": False,
"expiration": None
}
prev_dict[key] = afler
update_json_file(prev_dict, 'aflers.json')

bot.run(TOKEN)

0 comments on commit cae0e66

Please sign in to comment.