-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a DiscordMedium class for high-level APIs, have Nuwani respond to…
… mentions
- Loading branch information
1 parent
50efcc6
commit 2c58032
Showing
3 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2020 Las Venturas Playground. All rights reserved. | ||
// Use of this source code is governed by the MIT license, a copy of which can | ||
// be found in the LICENSE file. | ||
|
||
import { DiscordAPI } from 'features/nuwani_discord/discord_api.js'; | ||
|
||
import { encode } from 'components/networking/html_encoding.js'; | ||
|
||
// Endpoint to which the Discord REST API requests should be made. | ||
const kDiscordEndpoint = 'https://discordapp.com/api'; | ||
|
||
// The Discord medium is a communication channel through which interactions can be sent to Discord. | ||
// It abstracts the low-level API calls in more convenient, high-level JavaScript APIs. Most of | ||
// the APIs are documented in the following three articles on their Developer Portal: | ||
// | ||
// https://discord.com/developers/docs/resources/channel | ||
// https://discord.com/developers/docs/resources/guild | ||
// https://discord.com/developers/docs/resources/user | ||
export class DiscordMedium { | ||
#api_ = null; | ||
|
||
constructor(configuration) { | ||
this.#api_ = new DiscordAPI(configuration); | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------- | ||
// Actual Discord API methods | ||
// --------------------------------------------------------------------------------------------- | ||
|
||
// Creates the given |reaction| to the |message|, which must be one of the default emoji on the | ||
// server. This requires the |message| to have been sent to a channel. | ||
async createReaction(message, reaction) { | ||
const channelId = message.channel?.id; | ||
const messageId = message.id; | ||
const emoji = encode(reaction); | ||
|
||
if (!channelId || !messageId) | ||
return false; | ||
|
||
const path = `/channels/${channelId}/messages/${messageId}/reactions/${emoji}/@me`; | ||
console.log('issuing request to: ' + kDiscordEndpoint + path); | ||
return this.#api_.call('PUT', kDiscordEndpoint + path); | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------- | ||
|
||
dispose() { | ||
this.#api_ = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters