-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
69 lines (56 loc) · 1.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {
createMachine
} from './machine.js'
import { Client as DiscordClient } from 'discord.js'
// workaround for importing json
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const { appKey, appToken } = require('./config.json')
// This int isn't sensitive, it just describes the permissions we're requesting:
const PERMISSIONS_INT = 2147503168
const link = `https://discord.com/oauth2/authorize?client_id=${appKey}&scope=bot`
main()
async function main () {
console.log(`Starting SES-bot!`)
/**
* DISCORD CONFIG SECTION
*/
console.log(`Add to your discord server with this link: \n${link}`)
const discord = new DiscordClient({
client_id: appKey,
scope: 'bot',
permissions: PERMISSIONS_INT
})
discord.login(appToken)
discord.on('message', (discordMsg) => {
handleMessage({
authorId: discordMsg.author.id,
content: discordMsg.content,
reply: (text) => discordMsg.reply(text),
})
})
/**
* MESSAGE HANDLING
*/
const machine = createMachine()
await machine.replayPastFromDisk()
async function handleMessage (msg) {
const authorId = msg.authorId
const message = msg.content
const simulatePrefix = '?'
const messagePrefix = '$'
if (![simulatePrefix, messagePrefix].includes(message[0])) {
return
}
// This is a command for us!
const command = message.substr(messagePrefix.length) // Cut off the prefix
const loggable = { id: authorId, command }
// For simulated calls, invoke a new machine to try it.
if (simulatePrefix === message[0]) {
const counterfactual = await machine.fork({ logging: false })
counterfactual.queue({ loggable, msg })
return
}
machine.queue({ loggable, msg })
}
}