Skip to content

Commit

Permalink
Send discord need message (#148)
Browse files Browse the repository at this point in the history
* Update need command

* add need command timer

* add need call webhook function

* add need call webhook function variables

* Update config.yaml

* change timer with delay

* change timer with delay

* check if need_webhook exists in server config

* add url to need_webhook config

* add title to config

* check if need_webhook is enabled

* add color to send_webhook

* Update webhooks.py
  • Loading branch information
UnDeviato authored Jun 24, 2024
1 parent 6224e23 commit 88fb2cc
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config_sample/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,15 @@ unban_webhook:
enabled: true
username: Unban
avatar_url:

need_webhook:
enabled: true
delay: 60
username: Need
avatar_url:
color: "12745742"
pingoption: false
role_id:
title: "AO NEED CALL"
message: "A user has used the /need command for players in the Attorney Online server!"
url:
17 changes: 17 additions & 0 deletions server/client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def __init__(self, server, transport, user_id, ipid):
self.casing_steno = False
self.case_call_time = 0

# Need command
self.need_call_time = 0

# flood-guard stuff
self.mus_counter = 0
self.mus_mute_time = 0
Expand Down Expand Up @@ -1998,10 +2001,24 @@ def set_case_call_delay(self):
"""Begin the case announcement cooldown."""
self.case_call_time = round(time.time() * 1000.0 + 60000)

def set_need_call_delay(self):
"""Begin the need cooldown."""
try:
self.need_call_time = round(
time.time() * 1000.0
+ int(self.server.config["need_webhook"]["delay"]) * 1000.0
)
except:
self.need_call_time = round(time.time() * 1000 + 60000)

def can_call_case(self):
"""Whether or not the client can currently announce a case."""
return (time.time() * 1000.0 - self.case_call_time) > 0

def can_call_need(self):
"""Whether or not the client can currently call a need."""
return (time.time() * 1000.0 - self.need_call_time) > 0

def disemvowel_message(self, message):
"""Disemvowel a chat message."""
message = re.sub("[aeiou]", "", message, flags=re.IGNORECASE)
Expand Down
6 changes: 6 additions & 0 deletions server/commands/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ def ooc_cmd_need(client, arg):
raise ClientError("You have advertisements muted.")
if len(arg) == 0:
raise ArgumentError("You must specify what you need.")
if not client.can_call_need():
client.send_ooc("Please wait for the next need announcements!")
return
client.server.broadcast_need(client, arg)
if client.server.need_webhook:
client.server.webhooks.needcall(client, arg)
client.set_need_call_delay()
database.log_area("chat.announce.need", client, client.area, message=arg)


Expand Down
36 changes: 36 additions & 0 deletions server/network/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def send_webhook(
message=None,
embed=False,
title=None,
color=None,
description=None,
url=None,
):
Expand All @@ -40,6 +41,7 @@ def send_webhook(
embed = {}
embed["description"] = description
embed["title"] = title
embed["color"] = color
data["embeds"].append(embed)
result = requests.post(
url, data=json.dumps(data), headers={"Content-Type": "application/json"}
Expand Down Expand Up @@ -89,6 +91,40 @@ def modcall(self, char, ipid, area, reason=None):
description=description,
)

def needcall(self, client=None, reason=None):
if "need_webhook" not in self.server.config:
return

is_enabled = self.server.config["need_webhook"]["enabled"]
username = self.server.config["need_webhook"]["username"]
avatar_url = self.server.config["need_webhook"]["avatar_url"]
color = self.server.config["need_webhook"]["color"]
title = self.server.config["need_webhook"]["title"]
url = self.server.config["need_webhook"]["url"]
pingoption = self.server.config["need_webhook"]["pingoption"]
if pingoption not in [True, False]:
pingoption = False

if not is_enabled:
return

message = ""
if pingoption:
message += f"<@&{self.server.config['need_webhook']['role_id']}> \n"
message += self.server.config["need_webhook"]["message"]
description = f"{client.name} ({client.ipid}) in hub [{client.area.area_manager.id}] {client.area.area_manager.name} [{client.area.id}] {client.area.name} {'without reason (using <2.6?)' if reason is None else f'needs: {reason}'}"

self.send_webhook(
username=username,
avatar_url=avatar_url,
message=message,
embed=True,
title=title,
color=color,
description=description,
url=url,
)

def kick(self, ipid, reason="", client=None, char=None):
is_enabled = self.server.config["kick_webhook"]["enabled"]
username = self.server.config["kick_webhook"]["username"]
Expand Down
5 changes: 5 additions & 0 deletions server/tsuserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self):
self.ipRange_bans = []
self.geoIpReader = None
self.useGeoIp = False
self.need_webhook = False
self.supported_features = [
"yellowtext",
"customobjections",
Expand Down Expand Up @@ -153,6 +154,10 @@ def start(self):
except Exception as ex:
# Don't end the whole server if bridgebot destroys itself
print(ex)

if "need_webhook" in self.config and self.config["need_webhook"]["enabled"]:
self.need_webhook = True

asyncio.ensure_future(self.schedule_unbans())

database.log_misc("start")
Expand Down

0 comments on commit 88fb2cc

Please sign in to comment.