forked from wastydev/welcome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasty.js
131 lines (106 loc) · 4.15 KB
/
wasty.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
const { Client, VoiceChannel, GuildMember } = require("discord.js");
const fs = require("fs");
const CONFIG = JSON.parse(fs.readFileSync("./config.json", { encoding: "utf-8" }));
const moment = require("moment");
require("moment-duration-format");
const Voice = new Client({ fetchAllMembers: true, disableMentions: "none" });
Voice.staffJoined = false;
Voice.playingVoice = false;
Voice.voiceConnection = null;
Voice.channelID = null;
Voice.on("ready", async() => {
Voice.user.setPresence({
status: "dnd",
activity: {
name: CONFIG.DEFAULTS.ACTIVITY_TEXT
}
});
Voice.log(`Voice client \'${Voice.user.username}\' has been activated!`);
const Guild = Voice.guilds.cache.get(CONFIG.DEFAULTS.GUILD_ID) || Voice.guilds.cache.first();
if(!Guild) {
Voice.error("Guild not found!");
return Voice.destroy();
}
const Channel = Guild.channels.cache.get(CONFIG.DEFAULTS.VOICE_CHANNEL);
if(!Channel) {
Voice.error("Channel not found!");
return Voice.destroy();
}
Channel.join().then(connection =>{
Voice.voiceConnection = connection;
Voice.channelID = Channel.id;
Voice.log("The audio file is playing now...")
if(!Channel.hasStaff()) playVoice(Voice);
else Voice.staffJoined = true;
}).catch(err => {
Voice.error(`Cannot connect to voice channel (${Channel.name}) (${Channel.id}): ` + err.message)
return Voice.destroy();
});
});
Voice.on("voiceStateUpdate", async(oldState, newState) => {
if(
newState.channelID && (oldState.channelID !== newState.channelID) &&
newState.member.isStaff() &&
newState.channelID === Voice.channelID &&
!newState.channel.hasStaff(newState.member)
) {
Voice.staffJoined = true;
return playVoice(Voice);
}
if(
oldState.channelID &&
(oldState.channelID !== newState.channelID) &&
newState.member.isStaff() &&
oldState.channelID === Voice.channelID &&
!oldState.channel.hasStaff()
) {
Voice.staffJoined = false;
return playVoice(Voice);
}
});
Voice.login(CONFIG.TOKEN).catch(err => {
Voice.error("An occured error while connecting to Voice client: " + err.message);
return Voice.destroy();
});
/**
*
* @param {Client} Voice
*/
function playVoice(Voice) {
try {
const Path = Voice.staffJoined === true ? "./" + CONFIG.FILES.STAFF : "./" + CONFIG.FILES.WELCOME;
Voice.playingVoice = true;
Voice.voiceConnection.play(Path, {
volume: 1
}).on("finish", async() => {
Voice.playingVoice = false;
if(Voice.staffJoined === true) return;
playVoice(Voice);
});
} catch(err) {
return Voice.log("An occured error while playing voice file: " + err.message);
}
};
Client.prototype.log = function(content) {
return console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}] [VOICE BOT] ${content}`);
};
Client.prototype.error = function(content) {
return console.error(`[${moment().format('YYYY-MM-DD HH:mm:ss')}] [VOICE BOT] ERR! ${content}`);
};
VoiceChannel.prototype.hasStaff = function(checkMember = false) {
if(this.members.some(m => (checkMember !== false ? m.user.id !== checkMember.id : true) && !m.user.bot && m.roles.highest.position >= m.guild.roles.cache.get(CONFIG.DEFAULTS.MIN_STAFF_ROLE).position)) return true; // m.roles.highest.position >= this.guild.roles.cache.get(CONFIG.DEFAULTS.MIN_STAFF_ROLE).position
return false;
}
VoiceChannel.prototype.getStaffs = function(checkMember = false) {
return this.members.filter(m => (checkMember !== false ? m.user.id !== checkMember.id : true) && !m.user.bot && m.roles.highest.position >= m.guild.roles.cache.get(CONFIG.DEFAULTS.MIN_STAFF_ROLE).position).size
}
GuildMember.prototype.isStaff = function() {
if(
!this.user.bot &&
([...CONFIG.DEFAULTS.AUTHORS].includes(this.id) ||
this.hasPermission("ADMINISTRATOR") ||
this.roles.highest.position >= this.guild.roles.cache.get(CONFIG.DEFAULTS.MIN_STAFF_ROLE).position
)
) return true;
return false;
}