-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (179 loc) · 5.99 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
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
const Discord = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes, GatewayIntentBits } = require('discord.js');
const client = new Discord.Client({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,] });
require('dotenv').config();
const { MongoClient } = require('mongodb');
var read = require('fs-readdir-recursive');
const { leveling } = require('./function');
let cmds = new Discord.Collection();
client.on('ready', async () => {
client.guilds.cache.forEach(guild => {
guild.musicData = {
queue: [],
previous: [],
filters: [],
volume: 1,
isPlaying: false,
nowPlaying: null,
loopSong: false,
loopQueue: false,
songDispatcher: null,
connection: null,
voiceChannel: null,
}
guild.games = []
});
try {
client.pool = await MongoClient.connect(process.env.MONGO, { useNewUrlParser: true, useUnifiedTopology: true, keepAlive: true }).catch((err) => {
console.log(err)
});
console.log("Connected to the database.")
client.guilds.cache.forEach(async (guild) => {
if (!client.pool) return;
client.pool.connect(err => {
let result = client.pool.db("Tubb").collection("servers").find({ id: guild.id }).toArray();
if (err) console.log(err);
if (result.length == 0) {
client.pool.db("Tubb").collection("servers").insertOne({ id: guild.id, prefix: "-", leveling: 1, queue: null })
}
});
});
} catch (err) {
client.pool = null
console.log('Unable to connect to the database: ' + err);
}
setInterval(() => {
client.user.setActivity(`-help in ${client.guilds.cache.size} Servers`, { type: 'WATCHING' })
}, 60000);
console.log('Done!')
let cmddirs = read('./cmds');
let commands = []
cmddirs.forEach(e => {
let cmd = e.replace(`\\`, '/')
let cmdpath = require(`./cmds/${cmd}`)
cmds.set(cmdpath.name, cmdpath);
if (cmdpath.data!=undefined) {
commands.push(cmdpath.data.toJSON());
}
});
const rest = new REST({ version: '10' }).setToken(token);
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
});
client.on('message', async (message) => {
if (message.channel.type == "dm") return;
if (message.author.bot) return;
let prefix = process.env.PREFIX;
if (client.pool) {
const result = await client.pool.db("Tubb").collection("servers").find({ id: message.guild.id }).toArray()
prefix = result[0].prefix
if (result[0].leveling == 1) {
try {
await leveling(message, client)
} catch (err) { console.log(err) }
}
}
if (message.mentions.has(client.user) && !message.content.includes(`@everyone`) && message.type != 'REPLY' && !message.content.includes("@here")) message.channel.send(`Well... this is awkward... your server's prefix is \`${prefix}\`...`)
if (message.content.startsWith(prefix)) {
let content = message.content.slice(prefix.length).split(" ");
const command = cmds.get(content[0]) || cmds.find(cmd => cmd.aliases && cmd.aliases.includes(content[0]));
if (!command) return;
if (command.permissions) {
let perms = []
command.permissions.forEach(permission => {
if (!message.member.hasPermission(permission)) {
perms.push(permission)
}
});
if (perms.length >= 1) return message.reply(`You need the permission(s) \`${perms.join(', ')}\``)
}
if (command.permission) {
let perms = []
command.permission.forEach(permission => {
if (!message.guild.me.permissions.has(permission)) {
perms.push(permission)
}
});
if (perms.length >= 1) return message.reply(`I need the permission(s) \`${perms.join(', ')}\``)
}
if (command.ownerOnly == true && message.author.id !== process.env.OWNER) return message.reply("you cant do that!")
if (command.NSFW == true && message.channel.nsfw !== true) return message.reply(`move it to an NSFW channel.`)
const args = content.splice(1).join(" ");
command.execute(message, args, client)
}
})
client.on('guildCreate', async (guild) => {
if (client.pool) {
client.pool.db("Tubb").collection("servers").insertOne({ id: guild.id, prefix: "-", leveling: 1, queue: null })
guild.musicData = {
queue: [],
previous: [],
filters: [],
volume: 1,
isPlaying: false,
nowPlaying: null,
loopSong: false,
loopQueue: false,
songDispatcher: null,
connection: null,
voiceChannel: null
}
}
const channel = guild.channels.cache.find(channel => channel.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES'))
const invite = {
title: `Thank you for inviting me to \`\`${guild.name}\`\`!`,
fields: [
{
name: `My default prefix is`,
value: `\`\`-\`\` but you can always change it by running \n-prefix`,
inline: true,
},
{
name: `My commands`,
value: `Run -help to show all of my commands!`,
inline: true,
},
{
name: `Link to invite me to your server`,
value: `[Invite me!](https://discord.com/api/oauth2/authorize?client_id=750123677739122819&permissions=414569197296&scope=bot)`,
inline: true,
},
{
name: `My Code`,
value: `[Link](https://github.com/NotARoomba/TubbBot/)`,
inline: true,
},
],
timestamp: new Date(),
footer: {
text: `Thank you from the creator of ${client.user.username}`
}
}
channel.send({ embed: invite })
})
client.once('voiceStateUpdate', async (___, newState) => {
if (newState.member.user.bot && !newState.channelID && newState.guild.musicData.songDispatcher && newState.member.user.id == client.user.id) {
newState.guild.musicData.queue.length = 0;
newState.guild.musicData.songDispatcher.end();
return;
}
if (newState.member.user.bot && newState.channelID && newState.member.user.id == client.user.id && !newState.selfDeaf) {
newState.setSelfDeaf(true);
}
});
client.login(process.env.TOKEN);