-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (177 loc) · 6.32 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
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
const { Client, Events, GatewayIntentBits, REST, Routes } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus, VoiceConnectionStatus } = require('@discordjs/voice');
const { TOKEN,CLIENT_ID, CATAPI} = require('./secrets.json');
const { users } = require('./users.json');
const chance = {
"gif": 3,
"react": 15
}
function getTime(){
const now = new Date();
return `${now.getHours()}:${now.getMinutes()}`;
}
function random(percent) {
return Math.floor(Math.random()*100) < percent;
}
function RandomReact(msg,emotes) {
emotes = emotes.filter(n => n)
msg.react(emotes[Math.floor(Math.random() * emotes.length)]);
console.log(`${getTime()} Bot has reacted to msg: ${msg.guild.name} -> ${msg.channel.name}`);
}
function RandomReply(msg,gifs) {
gifs = gifs.filter(n => n)
msg.reply(gifs[Math.floor(Math.random() * gifs.length)]);
console.log(`${getTime()} Bot has replied to msg: ${msg.guild.name} -> ${msg.channel.name}`);
}
function playSound(user,sound){
try {
const channel = user.voice.channel;
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
connection.on(VoiceConnectionStatus.Ready, () => {
console.log(`${getTime()} Bot has joined the voice channel: ${channel.guild.name} -> ${channel.name} and played: ${sound}`);
});
const player = createAudioPlayer();
var resource = createAudioResource(`sounds/${sound}.mp3`);
player.play(resource);
connection.subscribe(player);
player.on(AudioPlayerStatus.Idle, () => {
connection.destroy();
});
player.on('error', error => {
console.error(`${getTime()} Error with audio player: ${error.message}`);
});
}
catch (error) {
console.error(`${getTime()} Error processing voice state update: ${error.message}`);
}
}
const commands = [
{
name: 'bonk',
description: 'Plays BONK! sound',
},
{
name: 'roll',
description: 'DEATH ROLL!',
options: [
{
name: 'max',
description: 'The maximum number to roll (default is 100)',
type: 4,
required: false,
},
],
},
{
name: 'kitty',
description: 'Sends a random cat picture!',
},
];
const rest = new REST({ version: '10' }).setToken(TOKEN);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationCommands(CLIENT_ID),
{ body: commands }
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error('Error while reloading application (/) commands:', error);
}
})();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates
]
});
client.once(Events.ClientReady, readyClient => {
console.log(`${getTime()} Ready! Logged in as ${readyClient.user.tag}`);
});
client.on(Events.VoiceStateUpdate, async (oldState, newState) => {
if(newState.member.user.bot){
return;
}
else if (!oldState.channelId && newState.channelId) {
playSound(newState.member,"wonderful-day")
}
});
client.on(Events.MessageCreate, msg => {
try{
if(!msg.author.bot){
const author = msg.author.id
//console.log(`${getTime()} ${msg.author.username}$ : ${msg.content} => ${msg.channel.name}`);
switch(msg.content.toLocaleLowerCase()){
case '@everyone':
msg.reply("We don't do that here");
msg.react('😬');
return;
case 'hello there':
msg.reply('General Kenobi!');
return;
}
if(users[author] == undefined){
if(users.default.gifs[0] != "" && random(chance.gif)){
RandomReply(msg,users.default.gifs)
return
}
else if(random(chance.react)){
RandomReact(msg,users.default.emotes)
return
}
}
else{
if(users[author].gifs[0] != "" && random(chance.gif)){
RandomReply(msg,users.default.gifs.concat(users[author].gifs))
return
}
else if(random(chance.react)){
RandomReact(msg,users.default.emotes.concat(users[author].emotes))
return
}
}2
}
}
catch (error) {
console.error(error)
}
});
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const { commandName } = interaction;
if (commandName === 'bonk') {
const voiceChannel = interaction.member.voice.channel;
if (!voiceChannel) {
return await interaction.reply('You need to be in a voice channel to use this command!');
}
await interaction.reply('Bonk incoming!');
playSound(interaction.member,"bonk")
}
else if (commandName === 'roll') {
const max = interaction.options.getInteger('max') || 100;
const roll = Math.floor(Math.random() * max) + 1;
await interaction.reply(`${interaction.member} rolls ${roll} (1-${max})`);
}else if (commandName === 'kitty') {
try {
const response = await fetch('https://api.thecatapi.com/v1/images/search', {
headers: {
'x-api-key': CATAPI
}
});
const data = await response.json();
const imageUrl = data[0].url;
await interaction.reply({ content: 'Here’s a cute cat!', files: [imageUrl] });
} catch (error) {
console.error('Error fetching cat image:', error);
await interaction.reply('Sorry, I couldn’t fetch a cat picture at the moment.');
}
}
});
client.login(TOKEN);