mirror of
https://github.com/evennia/evennia.git
synced 2026-03-31 04:57:16 +02:00
Add the ability to PATCH discord nicknames and roles.
This commit is contained in:
parent
1add10bcb0
commit
37ecac009d
3 changed files with 64 additions and 2 deletions
|
|
@ -637,6 +637,9 @@ 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):
|
||||
super().msg(nickname=(new_nickname, guild_id, user_id))
|
||||
|
||||
def direct_msg(self, message, sender, **kwargs):
|
||||
"""
|
||||
Called when the Discord bot receives a direct message on Discord.
|
||||
|
|
|
|||
|
|
@ -1942,6 +1942,7 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
|
|||
"list",
|
||||
"remove",
|
||||
"start",
|
||||
"nickname",
|
||||
)
|
||||
locks = "cmd:serversetting(DISCORD_ENABLED) and pperm(Developer)"
|
||||
help_category = "Comms"
|
||||
|
|
@ -1981,6 +1982,11 @@ class CmdDiscord2Chan(COMMAND_DEFAULT_CLASS):
|
|||
self.msg("Starting the Discord bot session.")
|
||||
return
|
||||
|
||||
if "nickname" in self.switches:
|
||||
discord_bot.change_nickname("Test", 541788744471281700, 124523120668311552)
|
||||
self.msg(f"Changed username")
|
||||
return
|
||||
|
||||
if "guild" in self.switches:
|
||||
discord_bot.db.tag_guild = not discord_bot.db.tag_guild
|
||||
self.msg(
|
||||
|
|
|
|||
|
|
@ -375,6 +375,39 @@ class DiscordClient(WebSocketClientProtocol, _BASE_SESSION_CLASS):
|
|||
|
||||
d.addCallback(cbResponse)
|
||||
|
||||
def _patch_json(self, url, data, **kwargs):
|
||||
"""
|
||||
Post JSON data to a REST API endpoint
|
||||
|
||||
Args:
|
||||
url (str) - The API path which is being posted to
|
||||
data (dict) - Content to be sent
|
||||
"""
|
||||
url = f"{DISCORD_API_BASE_URL}/{url}"
|
||||
body = FileBodyProducer(BytesIO(json.dumps(data).encode("utf-8")))
|
||||
d = _AGENT.request(
|
||||
b"PATCH",
|
||||
url.encode("utf-8"),
|
||||
Headers(
|
||||
{
|
||||
"User-Agent": [DISCORD_USER_AGENT],
|
||||
"Authorization": [f"Bot {DISCORD_BOT_TOKEN}"],
|
||||
"Content-Type": ["application/json"],
|
||||
}
|
||||
),
|
||||
body,
|
||||
)
|
||||
|
||||
def cbResponse(response):
|
||||
if response.code == 200:
|
||||
d = readBody(response)
|
||||
d.addCallback(self.post_response)
|
||||
return d
|
||||
elif should_retry(response.code):
|
||||
delay(300, self._post_json, url, data, **kwargs)
|
||||
|
||||
d.addCallback(cbResponse)
|
||||
|
||||
def post_response(self, body, **kwargs):
|
||||
"""
|
||||
Process the response from sending a POST request
|
||||
|
|
@ -483,10 +516,30 @@ class DiscordClient(WebSocketClientProtocol, _BASE_SESSION_CLASS):
|
|||
|
||||
"""
|
||||
|
||||
data = {"content": text}
|
||||
data = {"nick": text}
|
||||
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}
|
||||
self._patch_json(f"guilds/{guild_id}/members/{user_id}", data)
|
||||
|
||||
def send_roles(self, current_roles, role, guild_id, user_id, **kwargs):
|
||||
"""
|
||||
Assign the user a role on a Discord server.
|
||||
Use with session.msg(roles=(current_roles, role, guild_id, user_id))
|
||||
"""
|
||||
|
||||
data = {"roles": current_roles.append(role)}
|
||||
data.update(kwargs)
|
||||
self._post_json(f"guilds/{guild_id}/members/{user_id}", data)
|
||||
|
||||
def send_default(self, *args, **kwargs):
|
||||
"""
|
||||
Ignore other outputfuncs
|
||||
|
|
@ -552,4 +605,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))
|
||||
Loading…
Add table
Add a link
Reference in a new issue