diff --git a/src/main.py b/src/main.py index 33e4c27..6a3dc5d 100644 --- a/src/main.py +++ b/src/main.py @@ -19,7 +19,7 @@ def compose(self): if __name__ == '__main__': while not Listener.ready: - time.sleep(0.5) + time.sleep(0.1) while not auth.logged_in(): print('u need to login.') diff --git a/src/models/messaging.py b/src/models/messaging.py index 171fb67..0d5f5a4 100644 --- a/src/models/messaging.py +++ b/src/models/messaging.py @@ -90,6 +90,37 @@ def send_message(channel_id: str, content: str): return True return False + @staticmethod + def delete_message(channel_id: str, message_id: str): + '''Deletes a message from the channel.''' + + # send request + res = requests.delete( + url = f'https://discord.com/api/v9/channels/{channel_id}/messages/{message_id}', + headers = headers | { 'authorization': auth.get_token() } + ) + + # return status TODO: better error handling and documentation + if res.status_code == 204: + return True + return False + + @staticmethod + def edit_message(channel_id: str, message_id: str, content: str): + '''Edits a message in the channel.''' + + # send request + res = requests.patch( + url = f'https://discord.com/api/v9/channels/{channel_id}/messages/{message_id}', + headers = headers | { 'authorization': auth.get_token() }, + data = json.dumps({ 'content': content }) + ) + + # return status TODO: better error handling and documentation + if res.status_code == 200: + return True + return False + @staticmethod def __log_user(channel_id: str, username: str): '''Logs a user's nickname and color in the channel.''' @@ -160,8 +191,8 @@ def __get_message_history(channel_id: str, limit: int = 100, before: str = None) return [] @staticmethod - def __append_message(message_data: dict): - '''Recieves a message from the websocket.''' + def __append_message_list(message_data: dict): + '''Adds a message to the MessageList and tells view to update.''' if message_data.get('channel_id') in Messaging.__subscribed_channels: channel = Messaging.__subscribed_channels[message_data.get('channel_id')] @@ -174,8 +205,8 @@ def __append_message(message_data: dict): }) @staticmethod - def __delete_message(message_data: dict): - '''Recieves a message from the websocket.''' + def __delete_message_list(message_data: dict): + '''Deletes a message from the MessageList and tells view to update.''' if message_data.get('channel_id') in Messaging.__subscribed_channels: # get record from cache @@ -189,8 +220,8 @@ def __delete_message(message_data: dict): }) @staticmethod - def __edit_message(message_data: dict): - '''Recieves a message from the websocket.''' + def __edit_message_list(message_data: dict): + '''Edits a message from the MessageList and tells view to update.''' if message_data.get('channel_id') in Messaging.__subscribed_channels: # get record from cache @@ -203,6 +234,6 @@ def __edit_message(message_data: dict): 'data': message_data }) - Listener.subscribe_event('MESSAGE_CREATE', __append_message) - Listener.subscribe_event('MESSAGE_DELETE', __delete_message) - Listener.subscribe_event('MESSAGE_UPDATE', __edit_message) \ No newline at end of file + Listener.subscribe_event('MESSAGE_CREATE', __append_message_list) + Listener.subscribe_event('MESSAGE_DELETE', __delete_message_list) + Listener.subscribe_event('MESSAGE_UPDATE', __edit_message_list) \ No newline at end of file diff --git a/src/style.tcss b/src/style.tcss index 28f8bbb..a7bb5e8 100644 --- a/src/style.tcss +++ b/src/style.tcss @@ -36,4 +36,31 @@ CommandInput { margin-left: 2; margin-right: 2; margin-bottom: 2; +} + +.delete_button { + background: #ff4a4a; + height: 1; + width: 3; + text-align: center; +} + +.edit_button { + background: #ffc640; + height: 1; + width: 3; + text-align: center; +} + +.reply_button { + background: #619eff; + height: 1; + width: 3; + text-align: center; +} + +.options { + width: 0.25fr; + align: right middle; + height: 1; } \ No newline at end of file diff --git a/src/views/chat/delete_button.py b/src/views/chat/delete_button.py new file mode 100644 index 0000000..c9f0c45 --- /dev/null +++ b/src/views/chat/delete_button.py @@ -0,0 +1,11 @@ +from textual.widgets import Label +from models.messaging import Messaging + +class DeleteButton(Label): + + def __init__(self, message, *args, **kwargs): + super().__init__(renderable='x', classes='delete_button', *args, **kwargs) + self.message = message + + def on_click(self): + Messaging.delete_message(self.message['channel_id'], self.message['id']) \ No newline at end of file diff --git a/src/views/chat/edit_button.py b/src/views/chat/edit_button.py new file mode 100644 index 0000000..bd66f7e --- /dev/null +++ b/src/views/chat/edit_button.py @@ -0,0 +1,10 @@ +from textual.widgets import Label +from models.messaging import Messaging + +class EditButton(Label): + + def __init__(self, message, *args, **kwargs): + super().__init__(renderable='e' *args, **kwargs) + + def on_click(self): + Messaging.edit_message(self.message['channel_id'], self.message['id']) \ No newline at end of file diff --git a/src/views/chat/message.py b/src/views/chat/message.py index b2a2ef1..c4aab3c 100644 --- a/src/views/chat/message.py +++ b/src/views/chat/message.py @@ -3,9 +3,12 @@ from textual.containers import Horizontal from textual.color import Color from datetime import datetime +from views.chat.delete_button import DeleteButton +from models.user import User class Message(Static): + show_options = reactive(False, recompose=True) message = reactive('', recompose=True) def __init__(self, message, nick, color, *args, **kwargs): @@ -27,11 +30,15 @@ def decimal_to_rgb(self, decimal_color): def parse_timestamp(self, timestamp): return datetime.fromisoformat(timestamp).astimezone().strftime('%m-%d-%Y %H:%M:%S') - + def compose(self): name = Label(f"{self.nick if self.nick else self.message['author']['username']}:", classes='username') name.styles.color = self.decimal_to_rgb(self.color) + delete = DeleteButton(self.message) + edit = Label('e', classes='edit_button') + reply = Label('r', classes='reply_button') + with Horizontal(classes='message'): with Horizontal(classes='message-content'): yield name @@ -39,5 +46,4 @@ def compose(self): (' (edited)' if self.message.get('edited_timestamp') else ''), classes='content', shrink=True ) - - yield Label(self.parse_timestamp(self.message['timestamp']), classes='timestamp') \ No newline at end of file + yield (Horizontal(reply, edit, delete, classes='options') if self.message['author']['username'] == User.get_username() else Horizontal(reply, classes='options')) if self.show_options else Label(self.parse_timestamp(self.message['timestamp']), classes='timestamp') \ No newline at end of file