1+ // Copyright 2020 Las Venturas Playground. All rights reserved.
2+ // Use of this source code is governed by the MIT license, a copy of which can
3+ // be found in the LICENSE file.
4+
5+ import { DiscordAPI } from 'features/nuwani_discord/discord_api.js' ;
6+
7+ import { encode } from 'components/networking/html_encoding.js' ;
8+
9+ // Endpoint to which the Discord REST API requests should be made.
10+ const kDiscordEndpoint = 'https://discordapp.com/api' ;
11+
12+ // The Discord medium is a communication channel through which interactions can be sent to Discord.
13+ // It abstracts the low-level API calls in more convenient, high-level JavaScript APIs. Most of
14+ // the APIs are documented in the following three articles on their Developer Portal:
15+ //
16+ // https://discord.com/developers/docs/resources/channel
17+ // https://discord.com/developers/docs/resources/guild
18+ // https://discord.com/developers/docs/resources/user
19+ export class DiscordMedium {
20+ #api_ = null ;
21+
22+ constructor ( configuration ) {
23+ this . #api_ = new DiscordAPI ( configuration ) ;
24+ }
25+
26+ // ---------------------------------------------------------------------------------------------
27+ // Actual Discord API methods
28+ // ---------------------------------------------------------------------------------------------
29+
30+ // Creates the given |reaction| to the |message|, which must be one of the default emoji on the
31+ // server. This requires the |message| to have been sent to a channel.
32+ async createReaction ( message , reaction ) {
33+ const channelId = message . channel ?. id ;
34+ const messageId = message . id ;
35+ const emoji = encode ( reaction ) ;
36+
37+ if ( ! channelId || ! messageId )
38+ return false ;
39+
40+ const path = `/channels/${ channelId } /messages/${ messageId } /reactions/${ emoji } /@me` ;
41+ console . log ( 'issuing request to: ' + kDiscordEndpoint + path ) ;
42+ return this . #api_. call ( 'PUT' , kDiscordEndpoint + path ) ;
43+ }
44+
45+ // ---------------------------------------------------------------------------------------------
46+
47+ dispose ( ) {
48+ this . #api_ = null ;
49+ }
50+ }
0 commit comments