Fixed temporary bans, and added an option to exclude discord from banning.

changed the color of "Time is UP!" in the roll results, as requested.
This commit is contained in:
2022-08-09 17:09:52 +02:00
parent d4d08b3054
commit 28f0e66394
7 changed files with 84 additions and 26 deletions
+54 -11
View File
@@ -1,14 +1,21 @@
import time
import typing
import hjson
from core.aochat.BaseModule import BaseModule
from core.chat_blob import ChatBlob
from core.command_param_types import Any, Const, Options, Time, Character
from core.db import DB
from core.decorators import instance, command
from core.lookup.character_service import CharacterService
from core.translation_service import TranslationService
from core.decorators import instance, command, timerevent
from core.igncore import IgnCore
from modules.core.ban.ban_service import BanService
if typing.TYPE_CHECKING:
from modules.core.accounting.services.account_service import AccountService
from modules.core.ban.ban_service import BanService
from core.lookup.character_service import CharacterService
from core.translation_service import TranslationService
@instance()
@@ -19,6 +26,7 @@ class BanController(BaseModule):
self.db: DB = registry.get_instance("db")
self.text = registry.get_instance("text")
self.util = registry.get_instance("util")
self.account_service: AccountService = registry.get_instance("account_service")
self.ban_service: BanService = registry.get_instance("ban_service")
self.command_alias_service = registry.get_instance("command_alias_service")
self.ts: TranslationService = registry.get_instance("translation_service")
@@ -36,12 +44,13 @@ class BanController(BaseModule):
@command(command="ban", params=[Const("list", is_optional=True)], access_level="moderator",
description="Show the ban list")
def ban_list_cmd(self, _, _1):
query = self.db.query("SELECT a.*, p.*, b.reason, b.finished_at, b.created_at, b.sender_char_id from account a "
"left join player p on a.char_id = p.char_id "
"left join ban_list b on a.char_id = b.char_id "
"where main=a.char_id "
"and disabled=1 "
"and p.org_id NOT IN (SELECT org_id from ban_org_list)")
# query = self.db.query("SELECT a.*, p.*, b.reason, b.finished_at, b.created_at, b.sender_char_id from account a "
# "left join player p on a.char_id = p.char_id "
# "left join ban_list b on a.char_id = b.char_id "
# "where main=a.char_id "
# "and (disabled=1 or b.ended_early < 1)"
# "and p.org_id NOT IN (SELECT org_id from ban_org_list)")
query = self.ban_service.get_ban_list()
blob = ""
for row in query:
ends = "never" if (row.finished_at or -1) == -1 else self.util.format_datetime(row.finished_at)
@@ -56,13 +65,19 @@ class BanController(BaseModule):
@command(command="ban", params=[Options(["rem", "remove"]), Character("character")], access_level="moderator",
description="Remove a character from the ban list")
def ban_remove_cmd(self, _, _1, char):
def ban_remove_cmd(self, request, _1, char):
if not char.char_id:
return self.getresp("global", "char_not_found", {"char": char.name})
elif not self.ban_service.get_ban(char.char_id):
return self.getresp("module/ban", "not_banned", {"char": char.name})
else:
self.ban_service.remove_ban(char.char_id)
self.account_service.add_log(char.char_id, 'admin',
'Ban has been lifted.',
request.sender.char_id)
self.account_service.add_log(request.sender.char_id, 'admin',
f'lifted active ban for {char.name}',
self.bot.get_char_id())
return self.getresp("module/ban", "unbanned_self", {"char": char.name})
@command(command="ban",
@@ -75,5 +90,33 @@ class BanController(BaseModule):
elif self.ban_service.get_ban(char.char_id):
return f"<highlight>{char.name}</highlight> is already banned."
else:
if not duration:
duration = -1
if not reason:
reason = 'No reason given.'
else:
reason = f"Reason: {reason}"
self.account_service.add_log(char.char_id, 'admin', f'{"Banned permanently. " if duration < 0 else f"Banned for {self.util.time_to_readable(duration)}."} {reason}', request.sender.char_id)
self.account_service.add_log(request.sender.char_id, 'admin',
f' {char.name} has been {"banned permanently. " if duration < 0 else f"banned for {self.util.time_to_readable(duration)}."} {reason}',
request.sender.char_id)
self.ban_service.add_ban(char.char_id, request.sender.char_id, duration, reason)
request.reply(f"<highlight>{char.name}</highlight> has been added to the ban list.")
@timerevent(budatime="1h", description="lift expired temporary bans")
def fix_bans(self, _, _1):
d = self.db.query(
f"SELECT b.*, COALESCE(p1.name, b.char_id) AS name, p2.name AS sender_name FROM ban_list b "
f"LEFT JOIN player p1 ON b.char_id = p1.char_id "
f"LEFT JOIN player p2 ON b.sender_char_id = p2.char_id "
f"WHERE ended_early < 1 AND (finished_at < ?) "
f"ORDER BY b.created_at DESC", [time.time()])
for active in d:
dur = time.time() - active.created_at
self.ban_service.remove_ban(active.char_id, True)
self.account_service.add_log(active.char_id, "admin", f"Temporary ban lifted after a duration of {self.util.time_to_readable(dur)}. {active.reason}", self.bot.get_char_id())
for still_active in self.ban_service.get_ban_list():
self.account_service.account_disable(still_active.char_id)
+14 -10
View File
@@ -67,10 +67,14 @@ class BanService:
return num_rows
def remove_ban(self, char_id):
def remove_ban(self, char_id, tmp=False):
t = int(time.time())
num_rows = self.db.exec("UPDATE ban_list SET ended_early = 1 "
"WHERE char_id = ? AND (finished_at > ? OR finished_at = -1)", [char_id, t])
if not tmp:
num_rows = self.db.exec("UPDATE ban_list SET ended_early = 1 "
"WHERE char_id = ? and ended_early = 0", [char_id])
else:
num_rows = self.db.exec("UPDATE ban_list SET ended_early = 2 "
"WHERE char_id = ? AND (finished_at < ?)", [char_id, t])
if num_rows:
self.db.exec("UPDATE account SET disabled = 0 "
"WHERE main = (SELECT main from account where char_id=?)", [char_id])
@@ -83,19 +87,19 @@ class BanService:
"WHERE char_id = (SELECT main from account where char_id=?) and disabled=1",
[char_id])
def get_ban_list(self):
def get_ban_list(self, full=True) -> list[DictObject]:
t = int(time.time())
return self.db.query("SELECT b.*, COALESCE(p1.name, b.char_id) AS name, p2.name AS sender_name FROM ban_list b "
"LEFT JOIN player p1 ON b.char_id = p1.char_id "
"LEFT JOIN player p2 ON b.sender_char_id = p2.char_id "
"WHERE ended_early != 1 AND (finished_at > ? OR finished_at = -1) "
"ORDER BY b.created_at DESC", [t])
return self.db.query(f"SELECT b.*, COALESCE(p1.name, b.char_id) AS name, p2.name AS sender_name FROM ban_list b "
f"LEFT JOIN player p1 ON b.char_id = p1.char_id "
f"LEFT JOIN player p2 ON b.sender_char_id = p2.char_id "
f"WHERE ended_early < 1 AND (finished_at > ? {'OR finished_at = -1' if full else ''}) "
f"ORDER BY b.created_at DESC", [t])
def check_for_banned(self, context):
char_id = context.char_id
if self.get_ban(char_id):
# do nothing if character is banned
self.logger.info("ignoring banned character %d for command '%s'" % (char_id, context.message))
self.logger.info(f"ignoring banned character {self.account_service.character_service.resolve_char_to_name(char_id, default=str(char_id))} for command '{context.message}'")
return False
else:
return True