Skip to content

Commit

Permalink
add delete, edit, reply to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
al3xbro committed Jun 9, 2024
1 parent 6742261 commit 428828b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
49 changes: 40 additions & 9 deletions src/models/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'''
Expand Down Expand Up @@ -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')]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Listener.subscribe_event('MESSAGE_CREATE', __append_message_list)
Listener.subscribe_event('MESSAGE_DELETE', __delete_message_list)
Listener.subscribe_event('MESSAGE_UPDATE', __edit_message_list)
27 changes: 27 additions & 0 deletions src/style.tcss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 11 additions & 0 deletions src/views/chat/delete_button.py
Original file line number Diff line number Diff line change
@@ -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'])
10 changes: 10 additions & 0 deletions src/views/chat/edit_button.py
Original file line number Diff line number Diff line change
@@ -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'])
12 changes: 9 additions & 3 deletions src/views/chat/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -27,17 +30,20 @@ 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
yield Label((self.message['content'] if 'content' in self.message else '') +
(' (edited)' if self.message.get('edited_timestamp') else ''),
classes='content', shrink=True
)

yield Label(self.parse_timestamp(self.message['timestamp']), classes='timestamp')
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')

0 comments on commit 428828b

Please sign in to comment.