Merge pull request #3286 from holl0wstar/main

Expansion of the Discord integration
This commit is contained in:
Griatch 2023-10-07 13:24:30 +02:00 committed by GitHub
commit 630a06b5b6
2 changed files with 45 additions and 3 deletions

View file

@ -637,6 +637,30 @@ class DiscordBot(Bot):
# send outputfunc channel(msg, discord channel)
super().msg(channel=(strip_ansi(message.strip()), dc_chan))
def change_nickname(self, new_nickname, guild_id, user_id, **kwargs):
"""
Changes a given user's nickname on the given guild the bot is in.
Args:
new_nickname (str) - The user's new nickname.
guild_id (int) - The guild the nickname will be changed in.
user_id (int) - The Discord ID of the user who's nickname will be changed.
"""
super().msg(nickname=(new_nickname, guild_id, user_id))
def assign_role(self, role_id, guild_id, user_id, **kwargs):
"""
Assigns a user the role on the given guild the bot is in.
Args:
role_id (int) - The Discord role's ID.
guild_id (int) - The guild the role will be assigned in.
user_id (int) - The user the given role will be assigned to.
"""
super().msg(role=(role_id, guild_id, user_id))
def direct_msg(self, message, sender, **kwargs):
"""
Called when the Discord bot receives a direct message on Discord.

View file

@ -352,8 +352,10 @@ class DiscordClient(WebSocketClientProtocol, _BASE_SESSION_CLASS):
"""
url = f"{DISCORD_API_BASE_URL}/{url}"
body = FileBodyProducer(BytesIO(json.dumps(data).encode("utf-8")))
request_type = kwargs.pop("type", "POST")
d = _AGENT.request(
b"POST",
request_type.encode("utf-8"),
url.encode("utf-8"),
Headers(
{
@ -366,7 +368,7 @@ class DiscordClient(WebSocketClientProtocol, _BASE_SESSION_CLASS):
)
def cbResponse(response):
if response.code == 200:
if response.code == 200 or response.code == 204:
d = readBody(response)
d.addCallback(self.post_response)
return d
@ -487,6 +489,22 @@ class DiscordClient(WebSocketClientProtocol, _BASE_SESSION_CLASS):
data.update(kwargs)
self._post_json(f"channels/{channel_id}/messages", data)
def send_nickname(self, text, guild_id, user_id, **kwargs):
"""
Changes a user's nickname on a Discord server.
Use with session.msg(nickname=(new_nickname, guild_id, user_id))
"""
data = {"nick": text}
data.update(kwargs)
self._post_json(f"guilds/{guild_id}/members/{user_id}", data, type="PATCH")
def send_role(self, role_id, guild_id, user_id, **kwargs):
data = kwargs
self._post_json(f"guilds/{guild_id}/members/{user_id}/roles/{role_id}", data, type="PUT")
def send_default(self, *args, **kwargs):
"""
Ignore other outputfuncs
@ -552,4 +570,4 @@ class DiscordClient(WebSocketClientProtocol, _BASE_SESSION_CLASS):
# send the data for any other action types on to the bot as-is for optional server-side handling
keywords = {"type": action_type}
keywords.update(data["d"])
self.sessionhandler.data_in(self, bot_data_in=("", keywords))
self.sessionhandler.data_in(self, bot_data_in=("", keywords))