-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·124 lines (100 loc) · 3.36 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
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
116
117
118
119
120
121
122
123
124
const Type = require('./typedef.js');
require("dotenv").config();
// ------ Logger init ------
//
DebugLog = new Type.Logger();
// ------ discord init ------
//
const Discord = require("discord.js");
const D_client = new Discord.Client();
// ------ twitch init ------
//
const Tmi = require('tmi.js');
const T_client = new Tmi.Client({
options: { debug: false, messagesLogLevel: "info" },
connection: {
reconnect: true,
secure: true
},
identity: {
username: 'IrongradBot',
password: ('oauth:' + process.env.TWITCH_OAUTH_TOKEN)
},
channels: [ '#irongradtv', '#krurakark', '#fedjoyd5', '#reddister', '#yatsugora' ]
});
// ------ Database init ------
//
const DTB_manager = new Type.DatabaseManager(
process.env.DATABASE_HOST,
process.env.DATABASE_USER,
process.env.DATABASE_PASSWORD,
process.env.DATABASE_DEFAULT_DTB
);
// ---- module init ----
//
const fs = require('fs');
const modulesFolder = './modules/';
var modules = [];
const cmd_string = process.env.CMD_STRING;
console.log('');
console.log('loading modules :');
indexModInit = 0;
fs.readdirSync(modulesFolder).forEach(file => {
if (file.endsWith(".js") && file != "template.js"/* || file.endsWith(".ts") && file != "template.ts"/**/) {
modules.push(require(modulesFolder + file));
modules[indexModInit].setup(D_client, T_client, DTB_manager);
console.log(' - ' + file + '(' + modules[indexModInit].info.name + ') ... LOADED');
indexModInit++;
}
});
console.log('');
//
// ------ discord ------
//
D_client.on("ready", () => {
console.log("discord Bot ... READY !");
});
D_client.on("message", message => {
if (message.content.startsWith(cmd_string) && !message.author.bot) {
CMD_executor(message.content.substr(cmd_string.length), new Type.DiscordDataCmd(true, message), new Type.TwitchDataCmd(false, '', null, ''));
}
});
D_client.login(process.env.DISCORD_BOT_TOKEN);
//
// ------ twitch ------
//
T_client.connect().then(() => {
console.log("twitch Bot ... READY !");
}).catch(console.error);
T_client.on('message', (channel, tags, message, self) => {
if (message.startsWith(cmd_string) && !self) {
CMD_executor(message.substr(cmd_string.length), new Type.DiscordDataCmd(false, null), new Type.TwitchDataCmd(true, channel, tags, message));
}
});
//
// --------- Command Executor ---------
//
/**
* @param {string} query The command
* @param {Type.DiscordDataCmd} discord The discord data
* @param {Type.TwitchDataCmd} twitch The twitch data
*/
var CMD_executor = function(query, discord, twitch)
{
firstSpaceIndex = query.indexOf(' ');
command = (firstSpaceIndex == -1 ? query : query.substring(0, firstSpaceIndex)).toUpperCase();
args = (firstSpaceIndex == -1 ? [] : query.substring(firstSpaceIndex + 1).split(' '));
var userPermission = new Type.Permissible();
if (discord.is) { userPermission.setDiscordUser(discord.message.guild, discord.message.author, discord.message); }
if (twitch.is) { userPermission.setTwitchUser(twitch.tags, twitch.channel); }
userPermission.async.then(function() {
if (userPermission.canExecuteCommand) {
modules.forEach(mod => {
if ((mod.info.discord && discord.is || mod.info.twitch && twitch.is) && mod.info.commands.indexOf(command) != -1) {
mod.run(command, args, discord, twitch, userPermission);
}
});
}
})
}
/**/