Skip to content

Commit 2c58032

Browse files
committed
Add a DiscordMedium class for high-level APIs, have Nuwani respond to mentions
1 parent 50efcc6 commit 2c58032

3 files changed

Lines changed: 73 additions & 4 deletions

File tree

javascript/features/nuwani_discord/discord_manager.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@
55
// High-level manager that's responsible for our interaction with Discord. Builds upon many layers
66
// of Discord intelligence and behaviour, to end up with a convenient, easy to use API.
77
export class DiscordManager {
8-
constructor() {
8+
#medium_ = null;
99

10+
constructor(medium) {
11+
this.#medium_ = medium;
1012
}
1113

1214
// ---------------------------------------------------------------------------------------------
1315
// Events received from Discord
1416
// ---------------------------------------------------------------------------------------------
1517

1618
// Called when the given |message| has been received in one of the Discord channels, as an
17-
// instance of the Message class populated with all the relevant (and available) data.
19+
// instance of the Message class populated with all the relevant (and available) data. Private
20+
// messages will be ignored, as we want all interaction with Nuwani to be attributable.
1821
onMessage(message) {
22+
if (!message.guild)
23+
return; // ignore private messages
24+
25+
const nuwani = message.guild.bot;
26+
if (message.membersMentioned.has(nuwani) || message.content.includes('likeme'))
27+
this.#medium_.createReaction(message, '🤖');
28+
1929
console.log(`Message from ${message.author?.nickname} (Id: ${message.id})`);
2030
console.log(`-- channel: ${message.channel?.name}`);
2131
console.log(`-- content: ${message.content}`);
@@ -26,6 +36,6 @@ export class DiscordManager {
2636
// ---------------------------------------------------------------------------------------------
2737

2838
dispose() {
29-
39+
this.#medium_ = null;
3040
}
3141
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

javascript/features/nuwani_discord/nuwani_discord.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// be found in the LICENSE file.
44

55
import { DiscordManager } from 'features/nuwani_discord/discord_manager.js';
6+
import { DiscordMedium } from 'features/nuwani_discord/discord_medium.js';
67
import { DiscordRuntime } from 'features/nuwani_discord/discord_runtime.js';
78
import { Feature } from 'components/feature_manager/feature.js';
89

@@ -11,6 +12,7 @@ import { Feature } from 'components/feature_manager/feature.js';
1112
// integration is specified in the README.md file, which also describes the architecture.
1213
export default class NuwaniDiscord extends Feature {
1314
manager_ = null;
15+
medium_ = null;
1416
nuwani_ = null;
1517
runtime_ = null;
1618

@@ -20,9 +22,13 @@ export default class NuwaniDiscord extends Feature {
2022
// Depend on Nuwani because, well, we're part of the Nuwani system.
2123
this.nuwani_ = this.defineDependency('nuwani');
2224

25+
// The medium allows us to interact with Discord and send messages, on top of our own high-
26+
// level API. It abstracts this from the DiscordAPI class that issues REST calls.
27+
this.medium_ = new DiscordMedium(this.nuwani_().configuration.discord);
28+
2329
// The manager is the high-level interface through which we'll react to Discord messages
2430
// and state. It builds on top of the Runtime, which is the intelligence layer.
25-
this.manager_ = new DiscordManager();
31+
this.manager_ = new DiscordManager(this.medium_);
2632

2733
// The main Discord runtime, which owns the connection and decides what has to happen based
2834
// on which messages are being received by the server. Responsible for keeping state.
@@ -37,6 +43,9 @@ export default class NuwaniDiscord extends Feature {
3743
this.manager_.dispose();
3844
this.manager_ = null;
3945

46+
this.medium_.dispose();
47+
this.medium_ = null;
48+
4049
this.nuwani_ = null;
4150
}
4251
}

0 commit comments

Comments
 (0)