-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathping-bot.js
More file actions
1146 lines (1072 loc) · 41.3 KB
/
ping-bot.js
File metadata and controls
1146 lines (1072 loc) · 41.3 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Fluxer Example Bot
*
* Demonstrates prefix commands, embeds, DMs, voice join, audio, and video playback.
* DMs: !dm (DM yourself), !dmuser @user [message] (DM another user).
* Guild profile: !setnick [nickname] (change nickname), !setavatar [url] (change guild avatar).
* Voice: !play (joins your VC and plays WebM/Opus audio via youtube-dl-exec). No FFmpeg.
* Video: !playvideo [url] [480p|720p|1080p|1440p|4k] (streams MP4 in your VC; default 720p 30fps).
* Resolution forces FFmpeg path. Set FLUXER_VIDEO_FFMPEG=1 to use FFmpeg without resolution.
* !stop stops playback and leaves.
*
* Usage (from repo root after npm install && npm run build):
* FLUXER_BOT_TOKEN=your_token node examples/ping-bot.js
*
* Optional env: FLUXER_API_URL for custom API base; VOICE_DEBUG=1 for voice connection logs;
* SETAVATAR_DEBUG=1 for guild avatar request/response logs.
*/
import youtubedl from 'youtube-dl-exec';
import {
Client,
Events,
EmbedBuilder,
Routes,
User,
VoiceChannel,
cdnBannerURL,
cdnMemberAvatarURL,
UserFlagsBits,
PermissionFlags,
} from '@fluxerjs/core';
import { getVoiceManager, LiveKitRtcConnection } from '@fluxerjs/voice';
/** Fixed non‑copyrighted track; we get a direct WebM/Opus URL so the voice package can play without FFmpeg. */
const PLAY_URL = 'https://www.youtube.com/watch?v=eVTXPUF4Oz4';
const YTDLP_FORMAT = 'bestaudio[ext=webm][acodec=opus]/bestaudio[ext=webm]/bestaudio';
/** Default MP4 video URL for !playvideo (short public domain clip). Must be direct MP4 (H.264). */
const DEFAULT_VIDEO_URL = 'https://www.w3schools.com/html/mov_bbb.mp4';
/** yt-dlp format for MP4 video (prefer 1080p, then 720p, 360p, then best). */
const YTDLP_VIDEO_FORMAT =
'best[height<=1080][ext=mp4]/best[height<=1080]/22/18/best[ext=mp4]/best';
/** Regex for YouTube and similar sites that yt-dlp supports for video extraction. */
const YOUTUBE_LIKE = /youtube\.com|youtu\.be|yt\.be/i;
async function getStreamUrl(url) {
const result = await youtubedl(
url,
{
getUrl: true,
f: YTDLP_FORMAT,
formatSort: 'acodec:opus',
noWarnings: true,
noPlaylist: true,
},
{ timeout: 15000 },
);
return String(result ?? '').trim();
}
/** Get a direct MP4 video URL from YouTube or similar. Returns null on failure. */
async function getVideoUrl(url) {
const result = await youtubedl(
url,
{
getUrl: true,
f: YTDLP_VIDEO_FORMAT,
noWarnings: true,
noPlaylist: true,
},
{ timeout: 20000 },
);
return String(result ?? '').trim() || null;
}
// ─────────────────────────────────────────────────────────────────────────────
// Configuration
// ─────────────────────────────────────────────────────────────────────────────
const PREFIX = '!';
/** Per-guild play state for auto-reconnect when LiveKit server sends leave. */
const playState = new Map();
function setPlayState(guildId, channel, streamUrl) {
playState.set(guildId, { channel, streamUrl });
}
function clearPlayState(guildId) {
playState.delete(guildId);
}
const BRAND_COLOR = 0x4641d9;
// ─────────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────────
/** Format milliseconds into human-readable uptime. */
function formatUptime(ms) {
const seconds = Math.floor(ms / 1000) % 60;
const minutes = Math.floor(ms / (1000 * 60)) % 60;
const hours = Math.floor(ms / (1000 * 60 * 60)) % 24;
const days = Math.floor(ms / (1000 * 60 * 60 * 24));
const parts = [];
if (days > 0) parts.push(`${days}d`);
if (hours > 0) parts.push(`${hours}h`);
if (minutes > 0) parts.push(`${minutes}m`);
parts.push(`${seconds}s`);
return parts.join(' ');
}
/** Measure REST API latency by timing a lightweight request. */
async function measureApiLatency(client) {
const start = Date.now();
try {
await client.rest.get(Routes.gatewayBot());
} catch {
// Ignore errors; we just want the round-trip time
}
return Date.now() - start;
}
/** Resolve a user ID from the first argument: mention (<@id> or <@!id>) or raw snowflake. Returns null if invalid. */
function resolveUserId(arg, authorId) {
if (!arg) return authorId;
const mentionMatch = arg.match(/^<@!?(\d+)>$/);
if (mentionMatch) return mentionMatch[1];
if (/^\d{17,19}$/.test(arg)) return arg;
return null;
}
// ─────────────────────────────────────────────────────────────────────────────
// Command Handlers
// ─────────────────────────────────────────────────────────────────────────────
const commands = new Map();
commands.set('ping', {
description: 'Check bot and API latency',
async execute(message, client) {
const apiLatency = await measureApiLatency(client);
const embed = new EmbedBuilder()
.setTitle('Pong!')
.setColor(BRAND_COLOR)
.addFields({ name: 'API Latency', value: `\`${apiLatency}ms\``, inline: true })
.setFooter({ text: 'Fluxer Bot' })
.setTimestamp();
await message.reply({ embeds: [embed.toJSON()] });
},
});
commands.set('info', {
description: 'Display bot information',
async execute(message, client) {
const uptime = client.readyAt ? Date.now() - client.readyAt.getTime() : 0;
const apiLatency = await measureApiLatency(client);
const embed = new EmbedBuilder()
.setTitle('Bot Information')
.setColor(BRAND_COLOR)
.setThumbnail(client.user?.avatarURL?.() ?? null)
.addFields(
{ name: 'Username', value: client.user?.username ?? 'Unknown', inline: true },
{ name: 'Guilds', value: `${client.guilds.size}`, inline: true },
{ name: 'Channels', value: `${client.channels.size}`, inline: true },
{ name: 'Uptime', value: formatUptime(uptime), inline: true },
{ name: 'API Latency', value: `${apiLatency}ms`, inline: true },
{ name: 'Node.js', value: process.version, inline: true },
)
.setFooter({ text: 'Powered by @fluxerjs/core' })
.setTimestamp();
await message.reply({ embeds: [embed.toJSON()] });
},
});
commands.set('setnick', {
description: "Change the bot's nickname in this server (!setnick [nickname])",
async execute(message, client, args) {
const guildId = message.guildId;
if (!guildId) {
await message.reply('Use this command in a server.');
return;
}
const guild = client.guilds.get(guildId) ?? (await client.guilds.fetch(guildId));
if (!guild) {
await message.reply('Could not find this server.');
return;
}
const me = guild.members.me ?? (await guild.members.fetchMe());
const newNick = args.join(' ').trim() || null;
try {
await me.edit({ nick: newNick });
await message.reply(
newNick
? `Nickname set to \`${newNick}\` in this server.`
: 'Nickname cleared (showing username again).',
);
} catch {
await message
.reply('Failed to change nickname. The bot may need Change Nickname permission.')
.catch(() => {});
}
},
});
const SETAVATAR_DEBUG =
process.env.SETAVATAR_DEBUG === '1' || process.env.SETAVATAR_DEBUG === 'true';
commands.set('setavatar', {
description: "Change the bot's guild avatar (!setavatar [image URL] or !setavatar clear)",
async execute(message, client, args) {
const guildId = message.guildId;
if (!guildId) {
await message.reply('Use this command in a server.');
return;
}
const guild = client.guilds.get(guildId) ?? (await client.guilds.fetch(guildId));
if (!guild) {
await message.reply('Could not find this server.');
return;
}
const me = guild.members.me ?? (await guild.members.fetchMe());
const arg = args[0]?.toLowerCase();
if (arg === 'clear' || arg === 'reset') {
try {
if (SETAVATAR_DEBUG) {
console.log(
'[setavatar] PATCH /guilds/%s/members/@me with avatar: null (clear)',
guildId,
);
}
await me.edit({ avatar: null });
if (SETAVATAR_DEBUG) {
const updated = await guild.fetchMember(me.id);
console.log('[setavatar] Response: avatar=%s', updated.avatar ?? 'null');
}
await message.reply('Guild avatar cleared. Showing global avatar again.');
} catch (err) {
if (SETAVATAR_DEBUG) console.error('[setavatar] Clear failed:', err);
await message.reply('Failed to clear guild avatar.').catch(() => {});
}
return;
}
const url = args[0]?.trim();
if (!url || (!url.startsWith('http://') && !url.startsWith('https://'))) {
await message.reply(
'Provide an image URL: `!setavatar https://example.com/image.png` or `!setavatar clear` to reset.',
);
return;
}
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 30000);
const res = await fetch(url, { signal: controller.signal });
clearTimeout(timeout);
if (!res.ok) {
await message.reply(`Could not fetch image: ${res.status}`);
return;
}
const contentType = res.headers.get('content-type') ?? 'image/png';
const mime = contentType.split(';')[0].trim();
if (!mime.startsWith('image/')) {
await message.reply('URL must point to an image (png, jpeg, gif, webp).');
return;
}
const buf = Buffer.from(await res.arrayBuffer());
const base64 = buf.toString('base64');
const dataUri = `data:${mime};base64,${base64}`;
if (SETAVATAR_DEBUG) {
console.log(
'[setavatar] PATCH /guilds/%s/members/@me with avatar (dataUri len=%d, mime=%s)',
guildId,
dataUri.length,
mime,
);
}
await me.edit({ avatar: dataUri });
const updated = await guild.fetchMember(me.id);
if (SETAVATAR_DEBUG) {
console.log('[setavatar] Response: avatar=%s', updated.avatar ?? 'null');
}
if (dataUri && !updated.avatar) {
await message.reply(
'Request succeeded but the avatar was not applied. On Fluxer, guild avatars require a premium subscription—bots cannot set guild avatars.',
);
} else {
await message.reply('Guild avatar updated!');
}
} catch (err) {
if (SETAVATAR_DEBUG) console.error('[setavatar] Set failed:', err);
if (err?.name === 'AbortError') {
await message.reply('Timed out fetching image (30s).');
} else {
await message
.reply('Failed to set guild avatar. Check the URL and try again.')
.catch(() => {});
}
}
},
});
commands.set('bme', {
description: "Display guild.members.me (bot's member) info in this server",
async execute(message, client) {
const guildId = message.guildId;
if (!guildId) {
await message.reply('Use this command in a server.');
return;
}
const guild = client.guilds.get(guildId) ?? (await client.guilds.fetch(guildId));
if (!guild) {
await message.reply('Could not find this server.');
return;
}
const me = guild.members.me ?? (await guild.members.fetchMe());
const avatarUrl = me.displayAvatarURL({ size: 256 });
const accentColor = me.accentColor ?? me.user.avatarColor ?? BRAND_COLOR;
const roleNames = me.roles
.filter((id) => id !== guild.id)
.map((id) => guild.roles.get(id)?.name ?? id);
const permNames = [];
try {
for (const [name, bit] of Object.entries(PermissionFlags)) {
if (typeof bit === 'number' && me.permissions.has(bit)) permNames.push(name);
}
} catch {
/* ignore import error */
}
const embed = new EmbedBuilder()
.setTitle('guild.members.me')
.setDescription("Bot's GuildMember in this server")
.setColor(accentColor)
.setThumbnail(avatarUrl)
.addFields(
{ name: 'ID', value: `\`${me.id}\``, inline: true },
{ name: 'Username', value: me.user.username ?? '—', inline: true },
{ name: 'Display name', value: me.displayName ?? '—', inline: true },
{ name: 'Nickname', value: me.nick ?? '*(none)*', inline: true },
{ name: 'Joined', value: me.joinedAt.toISOString(), inline: true },
{
name: 'Roles',
value: roleNames.length ? roleNames.slice(0, 15).join(', ') : '*(none)*',
inline: false,
},
{ name: 'Mute', value: String(me.mute), inline: true },
{ name: 'Deaf', value: String(me.deaf), inline: true },
{
name: 'Permissions (sample)',
value: permNames.length ? permNames.slice(0, 12).join(', ') : '*(none)*',
inline: false,
},
)
.setFooter({ text: `Guild: ${guild.name}` })
.setTimestamp();
await message.reply({ embeds: [embed.toJSON()] });
},
});
commands.set('attachurl', {
description: 'Test file attachment by URL (sends image fetched from URL)',
async execute(message) {
const url = 'https://www.w3schools.com/html/pic_trulli.jpg';
await message.reply({
content: 'File attached from URL:',
files: [{ name: 'trulli.jpg', url }],
});
},
});
/** Get human-readable badge names from user flags (checks common badges that fit in 32-bit). */
function getBadgeNames(flags) {
if (flags == null || typeof flags !== 'number') return [];
const badges = [];
const DISPLAY_FLAGS = [
['Staff', UserFlagsBits.Staff],
['Partner', UserFlagsBits.Partner],
['Bug Hunter', UserFlagsBits.BugHunter],
['Ctp Member', UserFlagsBits.CtpMember],
['Friendly Bot', UserFlagsBits.FriendlyBot],
['Friendly Bot (Manual)', UserFlagsBits.FriendlyBotManualApproval],
];
for (const [name, bit] of DISPLAY_FLAGS) {
if ((flags & bit) === bit) badges.push(name);
}
return badges;
}
function addProfileFields(fields, profile, profileData, prefix = '') {
if (!profile || typeof profile !== 'object') return;
if (profile.pronouns != null && profile.pronouns !== '')
fields.push({
name: prefix + 'Pronouns',
value: String(profile.pronouns).slice(0, 40),
inline: true,
});
if (profile.bio != null && profile.bio !== '')
fields.push({
name: prefix + 'Bio',
value: String(profile.bio).slice(0, 1024) || '—',
});
if (profile.banner != null && profile.banner !== '')
fields.push({ name: prefix + 'Banner', value: 'Set', inline: true });
const accent = profile.accent_color ?? profile.banner_color;
if (accent != null)
fields.push({
name: prefix + 'Accent color',
value: `#${Number(accent).toString(16).padStart(6, '0')}`,
inline: true,
});
if (profile.theme != null)
fields.push({ name: prefix + 'Theme', value: String(profile.theme), inline: true });
const mutualCount = profileData?.mutual_guilds?.length ?? profileData?.mutual_guild_ids?.length;
if (mutualCount != null && mutualCount > 0)
fields.push({ name: prefix + 'Mutual servers', value: String(mutualCount), inline: true });
const connected = profileData?.connected_accounts;
if (connected?.length)
fields.push({
name: prefix + 'Connected accounts',
value:
connected
.map((a) => a.name ?? a.type ?? '?')
.slice(0, 5)
.join(', ') + (connected.length > 5 ? ` (+${connected.length - 5})` : ''),
inline: true,
});
}
commands.set('userinfo', {
description: "Show a user's profile (mention or user ID); no arg = yourself",
async execute(message, client, args) {
const userId = resolveUserId(args[0], message.author.id);
if (!userId) {
await message.reply(
'Provide a user mention (`@user`) or a user ID. Example: `!userinfo @Someone` or `!userinfo 123456789012345678`',
);
return;
}
let data;
try {
data = await client.users.fetchWithProfile(userId, {
guildId: message.guildId ?? undefined,
});
} catch {
await message.reply(
'Could not fetch that user. They may not exist or the ID may be invalid.',
);
return;
}
const {
user,
userData,
globalProfile: globalProfileData,
serverProfile: serverProfileData,
memberData,
} = data;
const globalProfileUserProfile = globalProfileData?.user_profile;
const serverProfileUserProfile = serverProfileData?.user_profile;
const bannerUrl =
serverProfileUserProfile?.banner != null && serverProfileUserProfile.banner !== ''
? cdnBannerURL(userData.id, serverProfileUserProfile.banner, { size: 512 })
: globalProfileUserProfile?.banner != null && globalProfileUserProfile.banner !== ''
? cdnBannerURL(userData.id, globalProfileUserProfile.banner, { size: 512 })
: userData.banner != null && userData.banner !== ''
? cdnBannerURL(userData.id, userData.banner, { size: 512 })
: null;
const accentColor =
serverProfileUserProfile &&
(serverProfileUserProfile.accent_color ?? serverProfileUserProfile.banner_color) != null
? Number(serverProfileUserProfile.accent_color ?? serverProfileUserProfile.banner_color)
: globalProfileUserProfile &&
(globalProfileUserProfile.accent_color ?? globalProfileUserProfile.banner_color) != null
? Number(globalProfileUserProfile.accent_color ?? globalProfileUserProfile.banner_color)
: userData.avatar_color != null
? Number(userData.avatar_color)
: memberData?.accent_color != null
? Number(memberData.accent_color)
: BRAND_COLOR;
// Prefer guild-specific avatar when in a server (like Discord.js member.displayAvatarURL)
const avatarUrl =
message.guildId && memberData?.avatar
? cdnMemberAvatarURL(message.guildId, userData.id, memberData.avatar, { size: 256 })
: user.displayAvatarURL({ size: 256 });
const displayName = userData.global_name ?? userData.username ?? 'User';
const embed = new EmbedBuilder()
.setTitle(`${displayName}'s profile`)
.setAuthor({ name: displayName, iconURL: avatarUrl })
.setColor(accentColor);
if (bannerUrl) embed.setImage(bannerUrl);
const fields = [
{ name: 'Username', value: userData.username ?? '—', inline: true },
{
name: 'Display name',
value: userData.global_name ?? userData.username ?? '—',
inline: true,
},
{ name: 'ID', value: `\`${userData.id}\``, inline: true },
{ name: 'Bot', value: userData.bot ? 'Yes' : 'No', inline: true },
{ name: 'System', value: userData.system ? 'Yes' : 'No', inline: true },
{ name: 'Discriminator', value: userData.discriminator ?? '0', inline: true },
{
name: 'Avatar',
value: userData.avatar
? userData.avatar.startsWith('a_')
? 'Animated'
: 'Set'
: 'Default',
inline: true,
},
{
name: 'Avatar color',
value:
userData.avatar_color != null
? `#${Number(userData.avatar_color).toString(16).padStart(6, '0')}`
: '—',
inline: true,
},
{
name: 'User banner',
value: userData.banner != null && userData.banner !== '' ? 'Set' : '—',
inline: true,
},
];
const flags = userData.flags ?? userData.public_flags;
if (flags != null) {
const badgeNames = getBadgeNames(flags);
fields.push({
name: 'Badges',
value: badgeNames.length ? badgeNames.join(', ') : '—',
inline: false,
});
fields.push({
name: 'Flags (raw)',
value: `\`${flags}\``,
inline: true,
});
}
if (globalProfileData) {
fields.push({ name: '\u200B', value: '**Global Profile**', inline: false });
addProfileFields(fields, globalProfileUserProfile, globalProfileData);
}
if (message.guildId && (serverProfileData || memberData)) {
const guild = client.guilds.get(message.guildId);
const guildName = guild?.name ?? 'this server';
fields.push({
name: '\u200B',
value: `**Server Profile** (${guildName})`,
inline: false,
});
if (serverProfileUserProfile && typeof serverProfileUserProfile === 'object') {
addProfileFields(fields, serverProfileUserProfile, serverProfileData, 'Server ');
}
if (memberData) {
const nick = memberData.nick ?? null;
if (nick)
fields.push({
name: 'Nickname',
value: String(nick).slice(0, 32),
inline: true,
});
if (memberData.joined_at)
fields.push({
name: 'Joined',
value: `<t:${Math.floor(new Date(memberData.joined_at).getTime() / 1000)}:R>`,
inline: true,
});
if (memberData.premium_since)
fields.push({
name: 'Boosting since',
value: `<t:${Math.floor(new Date(memberData.premium_since).getTime() / 1000)}:R>`,
inline: true,
});
if (memberData.communication_disabled_until) {
const until = new Date(memberData.communication_disabled_until);
if (until > new Date())
fields.push({
name: 'Timeout until',
value: `<t:${Math.floor(until.getTime() / 1000)}:F>`,
inline: true,
});
}
if (memberData.roles?.length) {
const roleIds = memberData.roles.filter((id) => id !== message.guildId);
const roleNames = guild
? roleIds.map((id) => guild.roles.get(id)?.name ?? id).slice(0, 20)
: roleIds.slice(0, 20);
const display =
roleNames.length > 0
? roleNames.join(', ') +
(roleIds.length > 20 ? ` (+${roleIds.length - 20} more)` : '')
: '—';
fields.push({ name: 'Roles', value: display.slice(0, 1024) || '—' });
}
if (memberData.avatar != null && memberData.avatar !== '')
fields.push({ name: 'Server avatar', value: 'Set', inline: true });
if (memberData.banner != null && memberData.banner !== '')
fields.push({ name: 'Server banner', value: 'Set', inline: true });
if (memberData.mute !== undefined)
fields.push({ name: 'Muted', value: memberData.mute ? 'Yes' : 'No', inline: true });
if (memberData.deaf !== undefined)
fields.push({ name: 'Deafened', value: memberData.deaf ? 'Yes' : 'No', inline: true });
}
}
embed
.addFields(...fields)
.setFooter({ text: `Requested by ${message.author.username}` })
.setTimestamp();
try {
await message.reply({ embeds: [embed.toJSON()] });
} catch (err) {
const fallback = `**${userData.username ?? userData.global_name ?? 'User'}** (ID: \`${userData.id}\`) — Could not send full embed (${err?.statusCode === 502 ? 'server error' : 'error'}). Try again.`;
await message.reply(fallback).catch(() => {});
}
},
});
const VERIFICATION_LEVELS = ['None', 'Low', 'Medium', 'High', 'Very High'];
const MFA_LEVELS = ['None', 'Elevated'];
const EXPLICIT_CONTENT_FILTERS = ['Disabled', 'Members without roles', 'All members'];
const DEFAULT_NOTIFICATION_LEVELS = ['All messages', 'Only mentions'];
commands.set('serverinfo', {
description: "Show this server's details",
async execute(message, client, args) {
const guildId = args[0] ?? message.guildId;
if (!guildId) {
await message.reply(
'Use this command in a server or provide a guild ID: `!serverinfo [guild_id]`',
);
return;
}
let data;
try {
data = await client.rest.get(Routes.guild(guildId));
} catch {
await message.reply('Could not fetch that server. Check the ID or permissions.');
return;
}
const iconUrl = data.icon
? `https://fluxerusercontent.com/icons/${data.id}/${data.icon}.png?size=256`
: null;
const embed = new EmbedBuilder()
.setTitle(data.name ?? 'Server')
.setColor(BRAND_COLOR)
.setThumbnail(iconUrl)
.addFields(
{ name: 'ID', value: `\`${data.id}\``, inline: true },
{ name: 'Owner ID', value: `\`${data.owner_id ?? '—'}\``, inline: true },
{
name: 'Verification',
value: VERIFICATION_LEVELS[data.verification_level] ?? String(data.verification_level),
inline: true,
},
{
name: 'MFA level',
value: MFA_LEVELS[data.mfa_level] ?? String(data.mfa_level),
inline: true,
},
{
name: 'AFK timeout',
value: data.afk_timeout != null ? `${data.afk_timeout}s` : '—',
inline: true,
},
{ name: 'NSFW level', value: String(data.nsfw_level ?? 0), inline: true },
{
name: 'Explicit content filter',
value:
EXPLICIT_CONTENT_FILTERS[data.explicit_content_filter] ??
String(data.explicit_content_filter ?? 0),
inline: true,
},
{
name: 'Default notifications',
value: DEFAULT_NOTIFICATION_LEVELS[data.default_message_notifications] ?? '—',
inline: true,
},
{
name: 'Vanity URL',
value: data.vanity_url_code ? `/${data.vanity_url_code}` : '—',
inline: true,
},
{
name: 'System channel ID',
value: data.system_channel_id ? `\`${data.system_channel_id}\`` : '—',
inline: true,
},
{
name: 'Rules channel ID',
value: data.rules_channel_id ? `\`${data.rules_channel_id}\`` : '—',
inline: true,
},
{
name: 'AFK channel ID',
value: data.afk_channel_id ? `\`${data.afk_channel_id}\`` : '—',
inline: true,
},
{ name: 'Features', value: data.features?.length ? data.features.join(', ') : '—' },
)
.setFooter({ text: `Requested by ${message.author.username}` })
.setTimestamp();
if (data.banner)
embed.setImage(
`https://fluxerusercontent.com/banners/${data.id}/${data.banner}.png?size=512`,
);
try {
await message.reply({ embeds: [embed.toJSON()] });
} catch {
await message
.reply(`Server: **${data.name}** (ID: \`${data.id}\`). Could not send embed.`)
.catch(() => {});
}
},
});
function resolveRoleIdOrName(arg) {
if (!arg) return null;
const mentionMatch = arg.match(/^<@&(\d+)>$/);
if (mentionMatch) return { type: 'id', value: mentionMatch[1] };
if (/^\d{17,19}$/.test(arg)) return { type: 'id', value: arg };
return { type: 'name', value: arg };
}
commands.set('roleinfo', {
description: "Show a role's details (role ID, mention, or name)",
async execute(message, client, args) {
const guildId = message.guildId;
if (!guildId) {
await message.reply('Use this command in a server.');
return;
}
const resolved = resolveRoleIdOrName(args[0]);
if (!resolved) {
await message.reply(
'Provide a role ID, role mention (`@Role`), or role name. Example: `!roleinfo Moderator`',
);
return;
}
let roles;
try {
roles = await client.rest.get(Routes.guildRoles(guildId));
} catch {
await message.reply('Could not fetch roles for this server.');
return;
}
const roleList = Array.isArray(roles) ? roles : Object.values(roles ?? {});
const role =
roleList.find((r) =>
resolved.type === 'id'
? r.id === resolved.value
: r.name && r.name.toLowerCase() === resolved.value.toLowerCase(),
) ?? null;
if (!role) {
await message.reply(
resolved.type === 'id' ? 'No role found with that ID.' : 'No role found with that name.',
);
return;
}
const color = role.color != null && role.color !== 0 ? role.color : BRAND_COLOR;
const permStr = role.permissions ? String(role.permissions).slice(0, 1024) : '—';
const embed = new EmbedBuilder()
.setTitle(role.name ?? 'Role')
.setColor(color)
.addFields(
{ name: 'ID', value: `\`${role.id}\``, inline: true },
{ name: 'Name', value: role.name ?? '—', inline: true },
{ name: 'Position', value: String(role.position ?? 0), inline: true },
{
name: 'Color',
value:
role.color != null && role.color !== 0
? `#${Number(role.color).toString(16).padStart(6, '0')}`
: 'Default',
inline: true,
},
{ name: 'Hoist', value: role.hoist ? 'Yes' : 'No', inline: true },
{ name: 'Mentionable', value: role.mentionable ? 'Yes' : 'No', inline: true },
{ name: 'Unicode emoji', value: role.unicode_emoji ?? '—', inline: true },
{
name: 'Hoist position',
value: role.hoist_position != null ? String(role.hoist_position) : '—',
inline: true,
},
{ name: 'Permissions', value: permStr },
)
.setFooter({ text: `Requested by ${message.author.username}` })
.setTimestamp();
try {
await message.reply({ embeds: [embed.toJSON()] });
} catch {
await message
.reply(`Role: **${role.name}** (ID: \`${role.id}\`). Could not send embed.`)
.catch(() => {});
}
},
});
commands.set('dm', {
description: 'DM yourself (demo of user.send)',
async execute(message) {
try {
await message.author.send('You requested a DM! This is a direct message from the bot.');
await message.reply('Check your DMs! 📬');
} catch {
await message.reply('Could not DM you. You may have DMs disabled.').catch(() => {});
}
},
});
commands.set('dmuser', {
description: 'DM a user: !dmuser @user [message]',
async execute(message, client, args) {
const userId = resolveUserId(args[0], null);
if (!userId) {
await message.reply('Provide a user mention or ID. Example: `!dmuser @Someone Hello!`');
return;
}
const text = args.slice(1).join(' ') || 'Hello from the bot!';
try {
const userData = await client.rest.get(Routes.user(userId));
const user = new User(client, userData);
await user.send(text);
await message.reply(`Sent DM to **${user.globalName ?? user.username}**.`);
} catch {
await message
.reply('Could not send DM. The user may not exist or may have DMs disabled.')
.catch(() => {});
}
},
});
commands.set('replytest', {
description: 'Test message.reply() - bot replies to your message (shows as reply thread)',
async execute(message) {
await message.reply('This message is a reply to yours! You should see it linked/threaded.');
},
});
commands.set('react', {
description: 'Reply with a message and add reactions',
async execute(message) {
const reply = await message.reply('React below! 👇');
await reply.react('👍');
await reply.react('❤️');
await reply.react('🎉');
},
});
commands.set('editembed', {
description: 'Demonstrate editing an existing message embed',
async execute(message) {
const embed = new EmbedBuilder()
.setTitle('Original Embed')
.setDescription('This embed will be edited in 2 seconds...')
.setColor(BRAND_COLOR)
.setFooter({ text: 'Editing embeds demo' })
.setTimestamp();
const reply = await message.reply({ embeds: [embed.toJSON()] });
// Wait 2 seconds, then edit the embed
await new Promise((r) => setTimeout(r, 2000));
const updatedEmbed = new EmbedBuilder()
.setTitle('Edited Embed')
.setDescription(
'The Fluxer API supports editing message embeds via `message.edit({ embeds: [...] })`.',
)
.setColor(0x57f287)
.addFields(
{ name: 'Original', value: 'First state', inline: true },
{ name: 'Edited', value: 'Updated state', inline: true },
)
.setFooter({ text: 'Embed was successfully edited' })
.setTimestamp();
await reply.edit({ embeds: [updatedEmbed] });
},
});
commands.set('help', {
description: 'List available commands',
async execute(message) {
const fields = [...commands.entries()].map(([name, cmd]) => ({
name: `${PREFIX}${name}`,
value: cmd.description,
inline: true,
}));
const embed = new EmbedBuilder()
.setTitle('Commands')
.setDescription(`Use \`${PREFIX}<command>\` to run a command.`)
.setColor(BRAND_COLOR)
.addFields(...fields)
.setFooter({ text: `Prefix: ${PREFIX}` })
.setTimestamp();
await message.reply({ embeds: [embed.toJSON()] });
},
});
commands.set('play', {
description: 'Join your voice channel and play music (WebM/Opus, no FFmpeg)',
async execute(message, client) {
const guildId = message.guildId;
if (!guildId) {
await message.reply('This command only works in a server.');
return;
}
const voiceManager = getVoiceManager(client);
const voiceChannelId = voiceManager.getVoiceChannelId(guildId, message.author.id);
if (!voiceChannelId) {
await message.reply('Join a voice channel first.');
return;
}
const channel = client.channels.get(voiceChannelId);
if (!(channel instanceof VoiceChannel)) {
await message.reply('Could not find that voice channel.');
return;
}
try {
const streamUrl = await getStreamUrl(PLAY_URL);
if (!streamUrl) {
await message.reply('Could not get stream URL. Is youtube-dl-exec installed?');
return;
}
setPlayState(guildId, channel, streamUrl);
const connection = await voiceManager.join(channel);
connection.on?.('serverLeave', async () => {
const state = playState.get(guildId);
if (!state) return;
console.log('[voice] LiveKit server sent leave; auto-reconnecting...');
try {
const conn = await voiceManager.join(state.channel);
await conn.play(state.streamUrl);
} catch (e) {
console.error('[voice] Auto-reconnect failed:', e);
playState.delete(guildId);
}
});
await connection.play(streamUrl);
await message.reply('Playing in your voice channel.');
} catch (err) {
console.error('Play error:', err);
await message.reply('Failed to join or play.').catch(() => {});
}
},
});
commands.set('playvideo', {
description:
'Stream video in your VC (default 720p; !playvideo [url] [480p|720p|1080p|1440p|4k])',
async execute(message, client, args) {
const guildId = message.guildId;
if (!guildId) {
await message.reply('This command only works in a server.');
return;
}
const voiceManager = getVoiceManager(client);
const voiceChannelId = voiceManager.getVoiceChannelId(guildId, message.author.id);
if (!voiceChannelId) {
await message.reply('Join a voice channel first.');
return;
}
const channel = client.channels.get(voiceChannelId);
if (!(channel instanceof VoiceChannel)) {
await message.reply('Could not find that voice channel.');
return;
}
const firstArg = args[0]?.trim();
const secondArg = args[1]?.trim();
const RESOLUTIONS = ['480p', '720p', '1080p', '1440p', '4k'];
const resolutionPreset = (s) => s && RESOLUTIONS.includes(s.toLowerCase());
let inputUrl;
let resolution;
if (resolutionPreset(firstArg)) {
inputUrl = DEFAULT_VIDEO_URL;
resolution = firstArg.toLowerCase();
} else if (firstArg && (firstArg.startsWith('http://') || firstArg.startsWith('https://'))) {
inputUrl = firstArg;
resolution = resolutionPreset(secondArg) ? secondArg.toLowerCase() : '720p';
} else {
inputUrl = DEFAULT_VIDEO_URL;
resolution = resolutionPreset(secondArg) ? secondArg.toLowerCase() : '720p';
}
try {
let videoUrl = inputUrl;
if (YOUTUBE_LIKE.test(inputUrl)) {