-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
148 lines (131 loc) · 4.71 KB
/
bot.ts
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
import { Events, DamonJs, Plugins, SearchResultTypes } from '../src/Index';
import { Shoukaku, Connectors } from 'shoukaku';
import { ChannelType, Client, GatewayIntentBits } from 'discord.js';
const { Guilds, GuildVoiceStates, GuildMessages, MessageContent } = GatewayIntentBits;
const Nodes = [
{
name: 'owo',
url: 'localhost:2333',
auth: 'youshallnotpass',
secure: false,
},
];
const client = new Client({ intents: [Guilds, GuildVoiceStates, GuildMessages, MessageContent] });
const damonjs = new DamonJs(
{
resolveError: {
max: 5,
time: 30 * 1000,
},
exceptions: {
max: 5,
time: 30 * 1000,
},
stuck: {
max: 5,
time: 30 * 1000,
},
skipResolveError: true,
skipOnException: true,
skipOnStuck: true,
defaultSearchEngine: 'youtube',
plugins: [new Plugins.PlayerMoved(client)],
},
new Shoukaku(new Connectors.DiscordJS(client), Nodes, {
moveOnDisconnect: false,
resume: false,
resumeTimeout: 30,
reconnectTries: 2,
restTimeout: 10000,
}),
);
client.on('ready', () => console.log(client?.user?.tag + ' Ready!'));
damonjs.shoukaku.on('ready', (name) => console.log(`Lavalink ${name}: Ready!`));
damonjs.shoukaku.on('error', (name, error) => console.error(`Lavalink ${name}: Error Caught,`, error));
damonjs.shoukaku.on('close', (name, code, reason) =>
console.warn(`Lavalink ${name}: Closed, Code ${code}, Reason ${reason || 'No reason'}`),
);
damonjs.shoukaku.on('debug', (name, info) => console.debug(`Lavalink ${name}: Debug,`, info));
damonjs.on(Events.PlayerCreate, async (player) => {
const channel =
client.channels.cache.get(player.textId) || (await client.channels.fetch(player.textId).catch(() => null));
if (channel && channel.isTextBased()) {
const message = await channel.send({ content: 'Created a player' });
player.data.set('message', message);
}
});
damonjs.on(Events.PlayerStart, async (player, track) => {
const message = player.data.get('message');
message && (await message.edit({ content: `Started playing ${track.title}` }));
});
damonjs.on(Events.PlayerEnd, async (player) => {
const message = player.data.get('message');
message && (await message.edit({ content: `Finished playing` }));
});
damonjs.on(Events.PlayerEmpty, async (player) => {
const message = player.data.get('message');
message && (await message.edit({ content: `Destroyed player due to inactivity.` }));
});
client.on('messageCreate', async (message) => {
const guild = message?.guild;
const channel = message?.channel;
if (client?.user?.id && guild && guild.members?.me && channel?.type === ChannelType.GuildText) {
if (message.content.startsWith('!play')) {
const args = message.content.split(' ');
const query = args.slice(1).join(' ');
const channel = message.member?.voice.channel;
if (!channel) {
await message.reply({ content: 'You need to be in a voice channel to use this command!' });
return;
}
const player = await damonjs.createPlayer({
guildId: message.guild.id,
textId: message.channel.id,
voiceId: channel.id,
volume: 40,
shardId: message.guild.shardId,
});
const result = await player.search(query, { requester: message.author });
if (!result.tracks.length) {
await message.reply({ content: 'No results found!' });
return;
}
if (result.type === SearchResultTypes.Playlist) player.queue.add(result.tracks);
else player.queue.add(result.tracks[0]);
if (!player.playable) await player.play(); // only use player.playable to play because if you use player.playing you can get unexpected bugs
if (player.paused) await player.pause(!player.paused); // if player is paused its gonna play
await message.reply({
content:
result.type === SearchResultTypes.Playlist
? `Queued ${result.tracks.length} from ${result.playlistInfo?.info.name}`
: `Queued ${result.tracks[0].title}`,
});
return;
}
if (message.content.startsWith('!skip')) {
const player = damonjs.players.get(message.guild.id);
if (!player) {
await message.reply({ content: 'No player found!' });
return;
}
await player.skip();
await message.reply({
content: `skipped the current track`,
});
return;
}
if (message.content.startsWith('!previous')) {
const player = damonjs.players.get(message.guild.id);
if (!player) {
await message.reply({ content: 'No player found!' });
return;
}
await player.previous();
await message.reply({
content: `skipped to the previous track`,
});
return;
}
}
});
client.login('');