-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstill_testing.js
More file actions
222 lines (195 loc) · 7.36 KB
/
still_testing.js
File metadata and controls
222 lines (195 loc) · 7.36 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Load discord.js Module V14
const { Client, Collection, Events, GatewayIntentBits, Partials } = require("discord.js");
// Load Node's native file system module.
const fs = require("fs");
// Load Sequelize, an object-relational-mapper(ORM) for database usage
const Sequelize = require("sequelize");
// Load configuration +
const {
prefix,
botToken,
bot_id,
helloserverid,
hellochannelid,
version,
activityMessage,
} = require("./config/config.json");
const { error } = require("console");
// create Still Testing Discord client
const Still_testing = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.GuildVoiceStates,
],
'partials': [Partials.Channel]
});
// create a extend JS's native Map class for better mapping
Still_testing.commands = new Collection();
// create command cooldown collaction
const cooldown = new Collection();
// dynamically retrieve all commands in the /command directory, filter only javascript files
const commandFiles = fs
.readdirSync("./src/commands")
.filter((file) => file.endsWith(".js"));
// log the console log into a .txt file. 'a' means appending (old data will be preserved)
const consoleLog = new console.Console(
fs.createWriteStream("./log/logoutput.txt", { flags: "a" })
);
consoleLog.log(
`/*---------New Console Log startet at ${new Date()}---------*/`
);
/*########_Boot_#########*/
/*-----------------------------Database-----------------------------*/
//define sqlite database connection info
const sqliteDB = new Sequelize(
"still testing database",
"zhang",
"halloworld",
{
//database storage host position
host: "localhost",
//database engine
dialect: "sqlite",
//disable/enable verbos output from
logging: false,
//sqlite only option, because sqlite stores all its data to a single file
storage: "./database/still testing database.sqlite",
}
);
//create a temporary database for temporary data storage. The database ceases to exist as soon as the connection is closed.
const tempMemory = new Sequelize({
dialect: "sqlite",
storage: ":memory:",
});
/*-----------------------------Startup-----------------------------*/
//Programm startup message
Still_testing.once("ready", () => {
//display connect status in
console.log("Connected as " + Still_testing.user.tag);
console.log("Bot Version:" + version);
const helloserver = Still_testing.guilds.cache.get(helloserverid);
const hellochannel = helloserver.channels.cache.get(hellochannelid);
//send message in the specific server on login
//hellochannel.send('Hello, Im back again');
//Send hello message to all servers
/*const allhelloserver = Still_testing.guilds.cache.map(guild => guild);
for(helloguild of allhelloserver){
if(helloguild.systemChannel != undefined){
helloguild.systemChannel.send(helloMessage);
}
}*/
//set activity
Still_testing.user.setActivity(`${activityMessage} | ${prefix}help`);
});
/*-----------------------------ReadFile-----------------------------*/
//Read fils from /commands folder
for (const file of commandFiles) {
const commandName = require(`./src/commands/${file}`);
// set a new item in the Collection
// with the key as the command name and the value as the exported module
Still_testing.commands.set(commandName.name, commandName);
}
const shutupcheck = Still_testing.commands.get("shutup");
/*########_Main_Programm_#########*/
//Listening Message
Still_testing.on("messageCreate", async (message) => {
/*-----------------------------DirectReaction-----------------------------*/
//check if the command comes from the bot
if (message.author.id == bot_id) {
return;
}
//check shutup
shutupcheck.check(message);
console.log('cp1');
message.mentions.users.forEach(user => console.log(user.username));
//react to all commands that starts with $kick your ass
if (message.content.startsWith(`$kick`)) {
message.channel.send("kick your ass too");
}
//Helloworld answer
else if (message.content === "still_testing") {
// Send Helloworld
message.channel.send("Helloworld");
}
//auto delete ass bot online message
/*else if (message.content.startsWith('Hi')) {
message.delete();
}*/
/*-----------------------------dynamic_Prefix Commands-----------------------------*/
//Exit early if the message starts without prefix
//add || message.author.bot after prefix for excluding bot messages
if (!message.content.startsWith(prefix)) return;
//Trim prefix out of message content, removes the leading and trailing white space and line terminator characters from a string and put it into an array for optimisation
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
//find the requested command in the command p
const command =
Still_testing.commands.get(commandName) ||
Still_testing.commands.find(
(cmd) => cmd.aliases && cmd.aliases.includes(commandName)
);
if (!command) return;
//check if the given command is server only
if (command.guildOnly && message.guild == null) {
return message.reply(
"oops! this command is a server only command ( 0 _ 0 )"
);
}
//check for all commands with args option set to true if the command is empty.
if (command.args && !args.length) {
let reply = `You didn't provide any arguments, ${message.author}!`;
//If it is empty, return a reply and the proper usage of the command
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
//check if command cooldown is expired
if (!cooldown.has(command.name)) {
cooldown.set(command.name, new Collection());
}
//get current time
const now = Date.now();
//get triggerd command
const timestamps = cooldown.get(command.name);
//get the cooldown time. Default is set to 1
const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
//check if the cooldown is over
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(
`please wait ${timeLeft.toFixed(
1
)} more second(s) before reusing the \`${command.name}\` command.`
);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
//compair incomming commands with dynamic commands from /command folder, catch any error occuring
try {
await command.execute(message, args, sqliteDB, tempMemory);
console.log(`${message.author} used ${message} with ${args} as arguments`);
consoleLog.log(
`#LOG <${new Date()}>: ${
message.author
} used ${message} with ${args} as arguments`
);
} catch (error) {
message.reply(
`Error executing command ${message},\n\n Command:\n ${message}\n Error source:\n ${error}.\n\nI definitly still need some testing`
);
consoleLog.log(`#ERROR <${new Date()}>: ${error}`);
console.error(error);
}
});
/*########_Connect_#########*/
//Bot Login
Still_testing.login(botToken);