Setup webhooks database

This commit is contained in:
amcmanu3 2023-06-03 13:35:29 -04:00
parent fb2bcd7e4e
commit cc157f86bb
2 changed files with 83 additions and 4 deletions

View File

@ -77,11 +77,14 @@ class HostStats(BaseModel):
# ********************************************************************************** # **********************************************************************************
class Webhooks(BaseModel): class Webhooks(BaseModel):
id = AutoField() id = AutoField()
name = CharField(max_length=64, unique=True, index=True) server_id = IntegerField(null=True)
method = CharField(default="POST") name = CharField(default="Custom Webhook", max_length=64)
url = CharField(unique=True) url = CharField(unique=True)
event = CharField(default="") webhook_type = CharField(default="Custom")
send_data = BooleanField(default=True) bot_name = CharField(default="Crafty Controller")
triggers = CharField(default="server_start,server_stop")
body = CharField(default="")
enabled = BooleanField(default=True)
class Meta: class Meta:
table_name = "webhooks" table_name = "webhooks"
@ -501,3 +504,53 @@ class HelpersManagement:
f"Not removing {dir_to_del} from excluded directories - " f"Not removing {dir_to_del} from excluded directories - "
f"not in the excluded directory list for server ID {server_id}" f"not in the excluded directory list for server ID {server_id}"
) )
# **********************************************************************************
# Webhooks Class
# **********************************************************************************
class HelpersWebhooks:
def __init__(self, database):
self.database = database
@staticmethod
def create_webhook(
server_id: int,
webhook_type: str,
name: str,
url: str,
bot_name: str,
body: str,
triggers: str,
enabled: bool,
) -> int:
"""Create a webhook in the database
Args:
server_id: ID of a server this webhook will be married to
name: The name of the webhook
url: URL to the webhook
webhook_type: The provider this webhook will be sent to
bot name: The name that will appear when the webhook is sent
triggers: Server actions that will trigger this webhook
body: The message body of the webhook
enabled: Should Crafty trigger the webhook
Returns:
int: The new webhooks's id
Raises:
PeeweeException: If the webhook already exists
"""
return Webhooks.insert(
{
Webhooks.server_id: server_id,
Webhooks.name: name,
Webhooks.webhook_type: webhook_type,
Webhooks.url: url,
Webhooks.bot_name: bot_name,
Webhooks.body: body,
Webhooks.triggers: triggers,
Webhooks.enabled: enabled,
}
).execute()

View File

@ -0,0 +1,26 @@
# Generated by database migrator
import peewee
def migrate(migrator, database, **kwargs):
migrator.drop_columns("webhooks", ["name", "method", "url", "event", "send_data"])
migrator.add_columns(
"webhooks",
server_id=peewee.IntegerField(null=True),
webhook_type=peewee.CharField(default="Custom"),
name=peewee.CharField(default="Custom Webhook", max_length=64),
url=peewee.CharField(default=""),
bot_name=peewee.CharField(default="Crafty Controller"),
trigger=peewee.CharField(default="server_start,server_stop"),
body=peewee.CharField(default=""),
enabled=peewee.BooleanField(default=True),
)
"""
Write your migrations here.
"""
def rollback(migrator, database, **kwargs):
"""
Write your rollback migrations here.
"""