Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restyle The API update #80

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions alembic/versions/8c4e4c4f3661_v0_1_1_0_integration_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,50 @@
Create Date: 2023-04-21 13:27:35.953542+00:00

"""
# pylint: skip-file
from alembic import op
import sqlalchemy as sa

from alembic import op
# pylint: skip-file

# revision identifiers, used by Alembic.
revision = '8c4e4c4f3661'
down_revision = '8c713ab3df0b'
revision = "8c4e4c4f3661"
down_revision = "8c713ab3df0b"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('general_configs', sa.Column('integrated', sa.Boolean(), nullable=True, comment='Whether the bot is integrated with the main bot'), schema='tickets_plus')
op.execute('UPDATE tickets_plus.general_configs SET integrated = FALSE')
op.alter_column('general_configs', 'integrated', nullable=False, schema='tickets_plus')
op.add_column('tickets', sa.Column('user_id', sa.BigInteger(), nullable=True, comment='Unique discord-provided user ID'), schema='tickets_plus')
op.add_column(
"general_configs",
sa.Column(
"integrated",
sa.Boolean(),
nullable=True,
comment="Whether the bot is integrated with the main bot",
),
schema="tickets_plus",
)
op.execute("UPDATE tickets_plus.general_configs SET integrated = FALSE")
op.alter_column("general_configs",
"integrated",
nullable=False,
schema="tickets_plus")
op.add_column(
"tickets",
sa.Column(
"user_id",
sa.BigInteger(),
nullable=True,
comment="Unique discord-provided user ID",
),
schema="tickets_plus",
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('tickets', 'user_id', schema='tickets_plus')
op.drop_column('general_configs', 'integrated', schema='tickets_plus')
op.drop_column("tickets", "user_id", schema="tickets_plus")
op.drop_column("general_configs", "integrated", schema="tickets_plus")
# ### end Alembic commands ###
42 changes: 21 additions & 21 deletions example_config.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"info": "This is the example config file for the bot. All the <name>_info are not used and just comments. The bot will use the info in the <name> field of the config.json. Feel free to remove all the _info values.",
"dbtype": "postgresql+asyncpg",
"info_dbtype": "The type of database to use. Active support for postgresql. Support for SQLite+aiosqlite will be provided on a least-effort basis and may be removed at any time.",
"dbhost": "localhost",
"info_dbhost": "The host of the database.",
"dbport": 5432,
"info_dbport": "The port of the database.",
"dbname": "bot",
"info_dbname": "The name of the database.",
"dbuser": "bot",
"info_dbuser": "The username for the database.",
"dbpass": "bot",
"info_dbpass": "The password for the database.",
"dev_guild_id": 478114566509821953,
"dev_guild_id_info": "The id of a guild where you can use the override commands. This is required for the bot to work.",
"https_port": 443,
"https_port_info": "The port to run the https server on. This is required for the API to work.",
"ssl_cert": "/etc/letsencrypt/live/ticketsplus.xyz/fullchain.pem",
"ssl_cert_info": "The path to the ssl certificate. This is required for the API to work.",
"auth_token": "thequickbrownfoxjumpedoverthelazydog",
"auth_token_info": "The token to use for the API. Please set this to something else. This is required for the API to work."
"info": "This is the example config file for the bot. All the <name>_info are not used and just comments. The bot will use the info in the <name> field of the config.json. Feel free to remove all the _info values.",
"dbtype": "postgresql+asyncpg",
"info_dbtype": "The type of database to use. Active support for postgresql. Support for SQLite+aiosqlite will be provided on a least-effort basis and may be removed at any time.",
"dbhost": "localhost",
"info_dbhost": "The host of the database.",
"dbport": 5432,
"info_dbport": "The port of the database.",
"dbname": "bot",
"info_dbname": "The name of the database.",
"dbuser": "bot",
"info_dbuser": "The username for the database.",
"dbpass": "bot",
"info_dbpass": "The password for the database.",
"dev_guild_id": 478114566509821950,
"dev_guild_id_info": "The id of a guild where you can use the override commands. This is required for the bot to work.",
"https_port": 443,
"https_port_info": "The port to run the https server on. This is required for the API to work.",
"ssl_cert": "/etc/letsencrypt/live/ticketsplus.xyz/fullchain.pem",
"ssl_cert_info": "The path to the ssl certificate. This is required for the API to work.",
"auth_token": "thequickbrownfoxjumpedoverthelazydog",
"auth_token_info": "The token to use for the API. Please set this to something else. This is required for the API to work."
}
10 changes: 5 additions & 5 deletions example_secret.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"info":"This is a file for the bot's private data. If you want to you can remove this line. Remember to remove the example_ from the file name!",
"token":"YOUR TOKEN HERE",
"token_info":"The token of the bot. You can get one @ discord.dev Don't share this with anyone or make it public. This is required for the bot to work.",
"ssl_key": "/etc/letsencrypt/live/ticketsplus.xyz/privkey.pem",
"ssl_key_info": "The path to the ssl key. This is required for the API to work."
"info": "This is a file for the bot's private data. If you want to you can remove this line. Remember to remove the example_ from the file name!",
"token": "YOUR TOKEN HERE",
"token_info": "The token of the bot. You can get one @ discord.dev Don't share this with anyone or make it public. This is required for the bot to work.",
"ssl_key": "/etc/letsencrypt/live/ticketsplus.xyz/privkey.pem",
"ssl_key_info": "The path to the ssl key. This is required for the API to work."
}
35 changes: 20 additions & 15 deletions tickets_plus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,32 @@
loop.run_until_complete(tickets_plus.start_bot(config))
```
"""
# License: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
# Copyright (c) 2021-present The Tickets+ Contributors
# This Source Code may also be made available under the following
# Secondary Licenses when the conditions for such availability set forth
# in the Eclipse Public License, v. 2.0 are satisfied: GPL-3.0-only OR
# If later approved by the Initial Contrubotor, GPL-3.0-or-later.
import signal
import sys
import ssl
import logging
import os
import signal
import ssl
import sys

import discord
import sqlalchemy
from discord.ext import commands
from sqlalchemy.ext import asyncio as sa_asyncio
from tornado import web
# Future Proofing for possible future use of asyncio

from tickets_plus import bot
from tickets_plus.api import routes
from tickets_plus.database import models, statvars
from tickets_plus.database import models
from tickets_plus.database import statvars

# License: EPL-2.0
# SPDX-License-Identifier: EPL-2.0
# Copyright (c) 2021-present The Tickets+ Contributors
# This Source Code may also be made available under the following
# Secondary Licenses when the conditions for such availability set forth
# in the Eclipse Public License, v. 2.0 are satisfied: GPL-3.0-only OR
# If later approved by the Initial Contrubotor, GPL-3.0-or-later.

# Future Proofing for possible future use of asyncio


# pylint: disable=unused-argument
Expand All @@ -55,8 +58,9 @@ def sigint_handler(sign, frame):
signal.signal(signal.SIGINT, sigint_handler)


async def start_bot(stat_data: statvars.MiniConfig = statvars.MiniConfig()
) -> None: # shush deepsource # skipcq: FLK-E124
async def start_bot(
stat_data: statvars.MiniConfig = statvars.MiniConfig(),
) -> None: # shush deepsource # skipcq: FLK-E124
"""Sets up the bot and starts it. Coroutine.

This function uses the exitsting .json files to set up the bot.
Expand Down Expand Up @@ -122,7 +126,8 @@ async def start_bot(stat_data: statvars.MiniConfig = statvars.MiniConfig()
pool_recycle=600,
connect_args={"server_settings": {
"jit": "off"
}})
}},
)
else:
engine = sa_asyncio.create_async_engine(
stat_data.get_url(),
Expand Down
2 changes: 1 addition & 1 deletion tickets_plus/api/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import json

import discord
from tornado import web
from sqlalchemy import orm
from tornado import web

from tickets_plus import bot
from tickets_plus.cogs import events
Expand Down
44 changes: 28 additions & 16 deletions tickets_plus/cogs/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

from tickets_plus import bot
from tickets_plus.api import handlers
from tickets_plus.database import layer, models
from tickets_plus.database import layer
from tickets_plus.database import models


class Events(commands.Cog, name="Events"):
Expand Down Expand Up @@ -160,7 +161,8 @@ async def message_discovery(self, message: discord.Message) -> None:
message: The message to check for links
"""
alpha = re.search(
r"https:\/\/(?:canary\.)?discord\.com\/channels\/(?P<srv>\d*)\/(?P<cha>\d*)\/(?P<msg>\d*)", # skipcq: FLK-E501 # pylint: disable=line-too-long
# skipcq: FLK-E501 # pylint: disable=line-too-long
r"https:\/\/(?:canary\.)?discord\.com\/channels\/(?P<srv>\d*)\/(?P<cha>\d*)\/(?P<msg>\d*)",
message.content,
)
if alpha:
Expand All @@ -181,7 +183,8 @@ async def message_discovery(self, message: discord.Message) -> None:
if not got_msg.content and got_msg.embeds:
discovered_result = got_msg.embeds[0]
discovered_result.set_footer(text="[EMBED CAPTURED] Sent in"
f" {chan.name}" # type: ignore
# type: ignore
f" {chan.name}"
f" at {time}")
else:
discovered_result = discord.Embed(
Expand All @@ -198,9 +201,13 @@ async def message_discovery(self, message: discord.Message) -> None:
if got_msg.attachments else None)
await message.reply(embed=discovered_result)

async def handle_anon(self, message: discord.Message, ticket: models.Ticket,
cnfg: layer.OnlineConfig,
guild: models.Guild) -> None:
async def handle_anon(
self,
message: discord.Message,
ticket: models.Ticket,
cnfg: layer.OnlineConfig,
guild: models.Guild,
) -> None:
"""Handles ticket anon messages.

Grabs a message and anonymises it, then sends it to the ticket.
Expand All @@ -215,8 +222,8 @@ async def handle_anon(self, message: discord.Message, ticket: models.Ticket,
staff = False
staff_roles = await cnfg.get_all_staff_roles(guild.guild_id)
for role in staff_roles:
parsed_role = message.guild.get_role( # type: ignore
role.role_id)
parsed_role = message.guild.get_role(
role.role_id) # type: ignore
if parsed_role in message.author.roles: # type: ignore
# Alredy checked for member
staff = True
Expand All @@ -230,9 +237,13 @@ async def handle_anon(self, message: discord.Message, ticket: models.Ticket,
)
await message.delete()

async def update_autoclose(self, message: discord.Message,
ticket: models.Ticket, guild: models.Guild,
cnfg: layer.OnlineConfig) -> None:
async def update_autoclose(
self,
message: discord.Message,
ticket: models.Ticket,
guild: models.Guild,
cnfg: layer.OnlineConfig,
) -> None:
"""Updates channel topic autoclose time.

Changes the channel topic to reflect the new autoclose time.
Expand All @@ -245,8 +256,8 @@ async def update_autoclose(self, message: discord.Message,
"""
chan = message.channel
if guild.any_autoclose:
time_since_update = (ticket.last_response -
datetime.datetime.utcnow())
time_since_update = ticket.last_response - datetime.datetime.utcnow(
)
if time_since_update >= datetime.timedelta(minutes=5):
crrnt = chan.topic # type: ignore
if crrnt is None:
Expand All @@ -262,7 +273,8 @@ async def update_autoclose(self, message: discord.Message,
r"<t:[0-9]*?:R>",
# skipcq: FLK-E501
f"<t:{int((message.created_at + datetime.timedelta(minutes=guild.any_autoclose)).timestamp())}:R>",
crrnt)
crrnt,
)
await chan.edit(topic=crrnt) # type: ignore
ticket.last_response = datetime.datetime.utcnow()
await cnfg.commit()
Expand Down Expand Up @@ -332,8 +344,8 @@ async def on_member_join(self, member: discord.Member) -> None:
if actv_member.status_till is not None:
# Split this up to avoid None comparison.
# pylint: disable=line-too-long
if actv_member.status_till <= datetime.datetime.utcnow(
): # type: ignore
if (actv_member.status_till <=
datetime.datetime.utcnow()): # type: ignore
# Check if the penalty has expired.
actv_member.status = 0
actv_member.status_till = None
Expand Down
Loading