Skip to content

Commit

Permalink
Add Threads/Tickets. (#53)
Browse files Browse the repository at this point in the history
* update discord py

* feat: monkey-patch embeds to give more init param options

* feat: add dmrelay cog for relaying dms to channel

some values are hardcoded until the configuration system is reimplemented

* feat: Add close command and enhance dmrelay support via webhooks

* fix: add missing config value to default config

temporary fix until the configuration system is rewritten

* chore: use correct types, pass thread, archive after message.

* chore: remove support for webhooks

Using webhooks to send messages do not add much if any benefit, as
a webhook with the member username and pfp is a bit harder to tell apart
from staff at first glance. This is also the only benefit that using
webhooks give, for the multiple disadventages they give, as now the bot
has to manage a webhook too.

We may support webhooks in the future as an official plugin, but for now
we are focusing on the core functionality of the bot.

* chore: rename DmRelay to Tickets

* chore: move thread decorator to threads.py

* threads: add ticket model

* chore: update discordpy

* chore: [temp fix] add members intent

* chore: don't have any hardcoded config

* threads: rewrite thread decorator

Thread decorator now also checks if it is a modmail thread or not, to
ensure ticket only commands aren't enabled on some other thread.

* chore[DMs]: add helper method to check if a user can be dmed

* chore[dependencies]: run poetry update

* minor: rename utils.user to utils.users

* feat: add ticket system to relay messages from dms to server, and back

IT LIVES!!
As of this commit, the bot can now function as a modmail bot, albiet
it is not fully implemented, and is somewhat buggy, but the basic
implementation is there.

* minor: add empty test file for threads/tickets

* chore: add required variable to .env.template

* minor: get rid of unused parameter

* nit: fix docstring in not-yet-written tests

* changes: document threads existence, and breaking env var

* nit: fix dms blocked alert message

* minor: make open a top level command named contact

* minor: add logging statements to threads.py

* minor: use TYPE_CHECKING

* threads: add on_typing relay

* tools: run poetry update

* threads: add typing event relay

* threads: add embed format stub methods

* breaking: refactor threads

* threads: move Ticket objects to be stored on the bot obj

* chore: make the embed creator an object

This is for when configuration will need to be passed to the embed creator.
Most embed options will be customisable, and it is easier to pass a
configuration object to the embed creator once than it is to pass
configuration parameters to every single method.

* threads: implement more embeds, add buggy edit command

* fix: send error message when thread can't be created

* chore: update docstring of contact command

* fix: add delete command, make edit command work

* fix: channel name now matches recipient

* threads: add user-side close embed

* chore: update discord.py and use Member.display_avatar

* add closing on thread archive events

closes tickets on thread archive events
also implements a lock for adding and removing threads

* fix: use audit logs so its possible to detect the user who archived the thread

* minor: make audit log optional at expense of not knowing who closed a thread

* nit: remove duped author info from embeds

* scrap thread embed creator

was too complicated, didn't serve a good use at the moment, could possibly make a return in the future but very unlikely

* minor: type the message dict return type properly

* chore: show ids on server relay but not on dm relay

* minor changes to support relaying embeds between servers

* chore: get rid of useless message ID

* fix: patch getitem method of messagedict

* minor: split relay into two methods

* chore: patch message dict to only allow discord.Message objects

* feat: listen to edit and deletion dm events and relay them to guild

* fix: edit works even if the last message was deleted and delete can delete multiple messages

* nit: save users to dm_channel to user mapping if they don't exist

* fix: handle stickers and developer debug command.

* minor: use built in discord timestamps with arrow

* add better attachment support and thread to dm deletion relay

* relay attachments from guild to dm

* fix handling of stickers and attachments

* fix: multiple tickets can't be opened with a user

* fix: send thread open message on opens

* minor: don't relay a thread close msg if no messages were sent

* fix: allow mods to provide contents when closing a thread

* fix: add missing type annotations

* fix: message sent in server is now better looking

* fix: rename thread debug command to prevent future conflicts

* nit: change user_dms_closed message

* Partially Revert 18d4f93

* chore: update comments and docs as requested

* chore: relock the lockfile

* nit: add missing period and use f-string

* fix: different colours for received and sent message embeds

* minor: move opened-at timestamp to a field

* nit: don't specify None when using get() on a dict

* make first wave of suggested changes

I need to commit and push, will squash these after

* make second wave of suggested changes

I need to commit and push, will squash these after

* fix: ignore all checkfailures from dm commands

* fix: remove unused converters

these converters may be useful, but they are not needed at this time

* nit: better detail 100% error rate on check_can_dm_user

* chore: unprivatize most thread methods

* minor: privatize ticket dict

* fix: remove additional files as part of 03ec70f

* nit: rename thread test file

* chore: make some requsted changes

- rename kwargs 'raise_for_preexisting_ticket' to 'raise_for_preexisting'
- don't use discord.ext.tasks for a simple bot.loop.create_task event
- fix a few typos in commands
- define and label the sets used for handling deleted messages

* minor: use modmail.utils.time for timestamps

* minor: fix edited messages not being relayed to guild

* fix: method now  returns instance of class instead of class

other minor changes including annotations and comments

* tests: add some tests for threads

* tests: add tests for utils.thread.decorators

* tests: add tests for the contact command

* nit: remove some extra trailing commas and move typing indicator

* tests: add general test for reply_to_user

* fix: use async rather than sync method for fetching tickets

* chore: rename variables, use named format strings,  and touch up error ui

* nit: use valid kwargs footer_text instead of footer

* chore: standardize and dedupe code for edit and delete commands

* chore: refactor fetch_ticket to return None if no ticket found

* chore: rename variables, fix logic bug, make constant

* fix: use correct logic

a few if statements were checking a variable is not None,
they should be checking the variable is None

Co-authored-by: Shivansh-007 <shivansh-007@outlook.com>
  • Loading branch information
onerandomusername and Shivansh-007 authored Apr 21, 2022
1 parent 10a6468 commit 0c4e1c2
Show file tree
Hide file tree
Showing 15 changed files with 1,818 additions and 2 deletions.
9 changes: 8 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
### Breaking
- Bot now requires a `RELAY_CHANNEL_ID` configuration variable. (#53)
- This is where tickets with users will be relayed.
- At a later point in time, this will be additionally included in a configuration command.

### Added
- Threads system (#53)
- Messages can now be relayed between a user and a server.
- NOTE: There is not a database yet, so none of these messages are stored.
- Added Dispatcher system, although it is not hooked into important features like thread creation yet. (#71)
- Officially support python 3.10 (#119)
- Officially support windows and macos (#121)
Expand Down
5 changes: 4 additions & 1 deletion modmail/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from modmail.log import ModmailLogger
from modmail.utils.extensions import EXTENSIONS, NO_UNLOAD, walk_extensions
from modmail.utils.plugins import PLUGINS, walk_plugins
from modmail.utils.threads import Ticket


REQUIRED_INTENTS = Intents(
Expand All @@ -38,9 +39,11 @@ class ModmailBot(commands.Bot):
logger: ModmailLogger = logging.getLogger(__name__)
dispatcher: Dispatcher

_tickets: t.Dict[int, Ticket] = dict()

def __init__(self, **kwargs):
self.config = config()
self.start_time: t.Optional[arrow.Arrow] = None # arrow.utcnow()
self.start_time: arrow.Arrow = arrow.utcnow()
self.http_session: t.Optional[aiohttp.ClientSession] = None
self.dispatcher = Dispatcher()

Expand Down
22 changes: 22 additions & 0 deletions modmail/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import types
import typing
from collections import defaultdict
from typing import Optional

import attr
import desert
Expand Down Expand Up @@ -383,6 +384,26 @@ class EmojiConfig:
)


@attr.mutable(slots=True)
class ThreadConfig:
"""Thread configuration."""

thread_mention_role_id: Optional[int] = attr.ib(
default=0,
metadata={
METADATA_TABLE: ConfigMetadata(
description="Role to mention on ticket creation.",
)
},
converter=int,
)
relay_channel_id: Optional[int] = attr.ib(
default=0,
metadata={METADATA_TABLE: ConfigMetadata(description="Channel to use for creating tickets.")},
converter=int,
)


@attr.s(auto_attribs=True, slots=True)
class BaseConfig:
"""
Expand All @@ -406,6 +427,7 @@ class BaseConfig:
},
)
emojis: EmojiConfig = EmojiConfig()
threads: ThreadConfig = ThreadConfig()


# build configuration
Expand Down
4 changes: 4 additions & 0 deletions modmail/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ production = true
[emojis]
failure = ":x:"
success = ":thumbsup:"

[threads]
relay_channel_id = 0
thread_mention_role_id = 0
3 changes: 3 additions & 0 deletions modmail/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ dev:
emojis:
failure: ':x:'
success: ':thumbsup:'
threads:
relay_channel_id: 0
thread_mention_role_id: 0
Loading

0 comments on commit 0c4e1c2

Please sign in to comment.