Skip to content
Merged
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
1 change: 1 addition & 0 deletions scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@
"dhuysamen@gmail.com": "GodsBoy", # PR #19318
"mrcoferland@gmail.com": "mrcoferland", # PR #19023
"chenlinfeng@ruije.com.cn": "noOne-list", # PR #19050
"briansu@Mac-mini.attlocal.net": "likejudy", # PR #19052
}


Expand Down
21 changes: 19 additions & 2 deletions tests/tools/test_discord_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ def test_missing_required_channel_id(self, monkeypatch):
assert "error" in result
assert "channel_id" in result["error"]

def test_missing_required_message_id_for_delete(self, monkeypatch):
monkeypatch.setenv("DISCORD_BOT_TOKEN", "test-token")
result = json.loads(discord_admin_handler(action="delete_message", channel_id="11"))
assert "error" in result
assert "message_id" in result["error"]

def test_missing_multiple_params(self, monkeypatch):
monkeypatch.setenv("DISCORD_BOT_TOKEN", "test-token")
result = json.loads(discord_admin_handler(action="add_role"))
Expand Down Expand Up @@ -407,10 +413,10 @@ def test_list_pins(self, mock_req, monkeypatch):


# ---------------------------------------------------------------------------
# Actions: pin_message / unpin_message
# Actions: pin_message / unpin_message / delete_message
# ---------------------------------------------------------------------------

class TestPinUnpin:
class TestPinUnpinDelete:
@patch("tools.discord_tool._discord_request")
def test_pin_message(self, mock_req, monkeypatch):
monkeypatch.setenv("DISCORD_BOT_TOKEN", "test-token")
Expand All @@ -425,6 +431,16 @@ def test_unpin_message(self, mock_req, monkeypatch):
mock_req.return_value = None
result = json.loads(discord_admin_handler(action="unpin_message", channel_id="11", message_id="500"))
assert result["success"] is True
mock_req.assert_called_once_with("DELETE", "/channels/11/pins/500", "test-token")

@patch("tools.discord_tool._discord_request")
def test_delete_message(self, mock_req, monkeypatch):
monkeypatch.setenv("DISCORD_BOT_TOKEN", "test-token")
mock_req.return_value = None
result = json.loads(discord_admin_handler(action="delete_message", channel_id="11", message_id="500"))
assert result["success"] is True
assert "deleted" in result["message"]
mock_req.assert_called_once_with("DELETE", "/channels/11/messages/500", "test-token")


# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -586,6 +602,7 @@ def test_admin_schema_description(self):
desc = entry.schema["description"]
assert "list_guilds()" in desc
assert "add_role(guild_id, user_id, role_id)" in desc
assert "delete_message(channel_id, message_id)" in desc
# Core actions should NOT be in admin description
assert "fetch_messages(" not in desc
assert "create_thread(" not in desc
Expand Down
12 changes: 12 additions & 0 deletions tools/discord_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@ def _unpin_message(token: str, channel_id: str, message_id: str, **_kwargs: Any)
return json.dumps({"success": True, "message": f"Message {message_id} unpinned."})


def _delete_message(token: str, channel_id: str, message_id: str, **_kwargs: Any) -> str:
"""Delete a message from a channel or thread."""
_discord_request("DELETE", f"/channels/{channel_id}/messages/{message_id}", token)
return json.dumps({"success": True, "message": f"Message {message_id} deleted."})


def _create_thread(
token: str, channel_id: str, name: str,
message_id: Optional[str] = None,
Expand Down Expand Up @@ -476,6 +482,7 @@ def _remove_role(token: str, guild_id: str, user_id: str, role_id: str, **_kwarg
"list_pins": _list_pins,
"pin_message": _pin_message,
"unpin_message": _unpin_message,
"delete_message": _delete_message,
"create_thread": _create_thread,
"add_role": _add_role,
"remove_role": _remove_role,
Expand All @@ -502,6 +509,7 @@ def _remove_role(token: str, guild_id: str, user_id: str, role_id: str, **_kwarg
("list_pins", "(channel_id)", "pinned messages in a channel"),
("pin_message", "(channel_id, message_id)", "pin a message"),
("unpin_message", "(channel_id, message_id)", "unpin a message"),
("delete_message", "(channel_id, message_id)", "delete a message"),
("create_thread", "(channel_id, name)", "create a public thread; optional message_id anchor"),
("add_role", "(guild_id, user_id, role_id)", "assign a role"),
("remove_role", "(guild_id, user_id, role_id)", "remove a role"),
Expand All @@ -522,6 +530,7 @@ def _remove_role(token: str, guild_id: str, user_id: str, role_id: str, **_kwarg
"list_pins": ["channel_id"],
"pin_message": ["channel_id", "message_id"],
"unpin_message": ["channel_id", "message_id"],
"delete_message": ["channel_id", "message_id"],
"create_thread": ["channel_id", "name"],
"add_role": ["guild_id", "user_id", "role_id"],
"remove_role": ["guild_id", "user_id", "role_id"],
Expand Down Expand Up @@ -758,6 +767,9 @@ def get_dynamic_schema() -> Optional[Dict[str, Any]]:
"unpin_message": (
"Bot lacks MANAGE_MESSAGES permission in this channel."
),
"delete_message": (
"Bot lacks MANAGE_MESSAGES permission in this channel, or cannot view the channel/message."
),
"create_thread": (
"Bot lacks CREATE_PUBLIC_THREADS in this channel, or cannot view it."
),
Expand Down
Loading