forked from chrislennon/lnbits-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBot.js
115 lines (101 loc) · 3.17 KB
/
Bot.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const Discord = require(`discord.js`);
const InteractionHandler = require(`./InteractionHandler`);
const MessageHandler = require(`./MessageHandler`);
const ReactionHandler = require(`./ReactionHandler`);
const dotenv = require(`dotenv`);
dotenv.config();
class Bot {
/**
* Initializes all modules, a
* +Discord client, binds events.
* @constructor
*/
constructor() {
this.client = new Discord.Client({
partials: [`CHANNEL`, `MESSAGE`, `REACTION`, `USER`],
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_MEMBERS
]
});
this.InteractionHandler = new InteractionHandler(this.client);
this.MessageHandler = new MessageHandler();
this.ReactionHandler = new ReactionHandler();
this.bindEvents();
}
/**
* Bind event functions.
*/
bindEvents() {
this.client.on(`ready`, this.onReady.bind(this));
this.client.on(`guildCreate`, this.onGuildJoin.bind(this));
this.client.on(`interactionCreate`, this.onInteraction.bind(this));
this.client.on(`messageCreate`, this.onMessage.bind(this));
this.client.on(`messageReactionAdd`, this.onMessageReactionAdd.bind(this));
this.client.on(`messageReactionRemove`, this.onMessageReactionRemove.bind(this));
}
/**
* Login client to Discord.
*/
connect() {
this.client.login(process.env.AUTH_TOKEN);
}
/**
* Destroy Discord client.
*/
destroy() {
console.log(`Shutting down.`);
this.client.destroy();
}
/**
* Passes message events to the MessageHandler.
* @param {Message} Message The Discord message object.
*/
onMessage(Message) {
if (
Message.content.toLowerCase() === `!deploy` &&
Message.author.id === `177898294939222016`
) {
this.InteractionHandler.updateCommands();
return;
}
this.MessageHandler.handleMessage(Message);
}
/**
* Passes interaction events to the InteractionHandler.
* @param {Interaction} Interaction The Discord interaction object.
*/
onInteraction(Interaction) {
this.InteractionHandler.handleInteraction(Interaction);
}
/**
* Passes reaction add events to the ReactionHandler.
* @param {Reaction} Reaction The Discord reaction object.
* @param {User} User The Discord user that added the reaction.
*/
onMessageReactionAdd(Reaction, User) {
this.ReactionHandler.handleReaction(Reaction, User, `ADD`);
}
/**
* Passes reaction remove events to the ReactionHandler.
* @param {Reaction} Reaction The Discord reaction object.
* @param {User} User The Discord user that removed the reaction.
*/
onMessageReactionRemove(Reaction, User) {
this.ReactionHandler.handleReaction(Reaction, User, `REMOVE`);
}
onGuildJoin() {
console.log(`joined new guild`);
this.InteractionHandler.updateCommands();
}
/**
* Bot is connected to Discord.
*/
onReady() {
this.InteractionHandler.updateCommands();
console.log(`Connected to Discord as ${this.client.user.username}#${this.client.user.discriminator} <@${this.client.user.id}>`);
console.log(`Using lnbits host: ${process.env.LNBITS_HOST}`);
}
}
module.exports = Bot;