Skip to content

Commit d209eca

Browse files
committed
💖 1.9.7 Auto defer ve daha iyi hata yakalama.
- `interactionDefaults` altına `autoDefer` eklendi. Artık otomatik olarak her interaksiyona cevap verebilirsiniz. - `Underline.config.other` için `Underline.other` yan adı eklendi. - Artık olaylarda ve otomatik tamamlamalarda yaşanan hatalar botu çökertmeyecek. - Artık ototmatik tamamlamada bir çok userErrordan etkileniyor.
1 parent 42013da commit d209eca

File tree

9 files changed

+135
-18
lines changed

9 files changed

+135
-18
lines changed

‎config.js‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = new (require("./types/Config"))({
1616
// Botunuzun varsayılan dili.
1717
defaultLanguage: "tr",
1818
// Diğer ayarlar. Bunun içine ne isterseniz koyabilirsiniz.
19-
// Ulaşmak için "Underline.config.other" objesini kullanabilirsiniz.
19+
// Ulaşmak için "Underline.config.other" ve/veya "Underline.other" objesini kullanabilirsiniz.
2020
other: {
2121

2222
},
@@ -39,25 +39,32 @@ module.exports = new (require("./types/Config"))({
3939
},
4040
// interaksiyon kapalı olduğunda
4141
disabled(interaction, uInteraction, other) {
42+
if (interaction.isAutocomplete()) return [];
4243
interaction.reply({ ephemeral: true, content: other.locale.userErrors.disabled() });
4344
},
4445
// Kullanıcı bottan yasaklı olduğunda.
4546
blocked(interaction, uInteraction, other) {
47+
if (interaction.isAutocomplete()) return [];
4648
interaction.reply({ ephemeral: true, content: other.locale.userErrors.blocked() });
4749
},
4850
// interaksiyon sadece geliştiricilere özel olduğunda.
4951
developerOnly(interaction, uInteraction, other) {
52+
if (interaction.isAutocomplete()) return [];
5053
interaction.reply({ ephemeral: true, content: other.locale.userErrors.developerOnly() });
5154
},
5255
// interaksiyon sadece sunucu sahiplerine özel olduğunda.
5356
guildOwnerOnly(interaction, uInteraction, other) {
57+
if (interaction.isAutocomplete()) return [];
5458
interaction.reply({ ephemeral: true, content: other.locale.userErrors.guildOwnerOnly() });
5559
},
60+
// interaksiyon sadece sunuculara özel olduğunda.
5661
guildOnly(interaction, uInteraction, other) {
62+
if (interaction.isAutocomplete()) return [];
5763
interaction.reply({ ephemeral: true, content: other.locale.userErrors.guildOnly() });
5864
},
5965
// Botun çalışmak için x yertkilerine ihtiyacı olduğunda.
6066
botPermsRequired(interaction, uInteraction, perms, other) {
67+
if (interaction.isAutocomplete()) return [];
6168
interaction.reply({ ephemeral: true, content: other.locale.userErrors.botPermsRequired(perms.join(", ")) });
6269
},
6370
// Kullanıcının interaksiyonu kullanabilmek için x yetkilerine ihtiyacı olduğunda.
@@ -81,7 +88,8 @@ module.exports = new (require("./types/Config"))({
8188
user: []
8289
},
8390
options: [],
84-
defaultPermission: true
91+
defaultPermission: true,
92+
autoDefer: "ephemeral"
8593
},
8694
// Bot ilk açıldığında daha hiçbirşey yüklenmeden önce çalışan fonksiyon. Opsiyonel.
8795
onBeforeLoad(client) {
@@ -122,7 +130,11 @@ module.exports = new (require("./types/Config"))({
122130
//
123131
// Other objesini istediÄŸiniz gibi modifiye edebilirsiniz.
124132
// Nasılsa altakki fonksiyon her event çalışmadan önce çalışır.
125-
async onEvent(eventName, [arg1 , arg2], other) {
133+
async onEvent(eventName, [arg1, arg2], other) {
126134
return true;
127-
}
135+
},
136+
// Olay hatasız bir şekilde çalıştıktan sonra çalışır.
137+
async onAfterEvent(eventName, [arg1, arg2], other) {
138+
139+
},
128140
})

‎globals.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ interface Underline {
55
events: import("discord.js").Collection<string, import("./types/Event") >
66
locales: import("discord.js").Collection<string, import("./types/Locale") >
77
config: import("./types/Config");
8+
other: { [key: string | number]: any; },
9+
utils: typeof import("./other/utils.js");
810
client: import("discord.js").Client;
911
Interaction: typeof import("./types/Interaction"),
1012
SlashCommand: typeof import("./types/SlashCommand"),

‎index.js‎

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require("./other/patchConsoleLog");
22
console.info(`[BİLGİ] Basit Altyapı v${require("./package.json").version} - by Kıraç Armağan Önal`);
33
const config = require("./config");
4+
const utils = require("./other/utils");
45
globalThis.Underline = config.globalObjects;
56
const Discord = require("discord.js");
67
const chillout = require("chillout");
@@ -17,10 +18,12 @@ let localeFiles;
1718
globalThis.Underline = {
1819
...config.globalObjects,
1920
config,
21+
other: config.other,
2022
client,
2123
interactions,
2224
events,
2325
locales,
26+
utils,
2427
Interaction: require('./types/Interaction'),
2528
Event: require('./types/Event'),
2629
SlashCommand: require("./types/SlashCommand"),
@@ -238,9 +241,22 @@ async function load() {
238241
let before = await Underline.config.onEvent(eventName, args, other);
239242
if (!before) return;
240243
args.push(other);
241-
chillout.forEach(events, (event) => {
244+
chillout.forEach(events,
245+
/** @param {import("./types/Event")} event */
246+
(event) => {
242247
if (!event.disabled) {
243-
event.onEvent(...args);
248+
try {
249+
event.onEvent(...args);
250+
Underline.config.onAfterEvent(eventName, args, other);
251+
} catch (err) {
252+
console.error(`[HATA] "${event.id}" idli ve "${eventName}" isimli olayda bir hata oluÅŸtu!`);
253+
if (err.message) console.error(`[HATA] ${err.message}`);
254+
if (err.stack) {
255+
`${err.stack}`.split("\n").forEach((line) => {
256+
console.error(`[HATA] ${line}`);
257+
});
258+
}
259+
}
244260
}
245261
});
246262

@@ -281,6 +297,7 @@ async function unload() {
281297
await chillout.forEach(eventListeners, (el) => {
282298
el.base.off(el.name, el.listener);
283299
})
300+
eventListeners.length = 0;
284301

285302
console.info(`[BILGI] Dil listesi temizleniyor..`);
286303
Underline.locales.clear();
@@ -325,20 +342,60 @@ client.on("interactionCreate", async (interaction) => {
325342

326343
if (!uInter) return;
327344

345+
let other = {};
346+
328347
if (interaction.isAutocomplete()) {
348+
if (uInter.disabled) {
349+
let r = await config.userErrors.disabled(interaction, uInter, other);
350+
interaction.respond(r);
351+
return;
352+
}
353+
if (config.blockedUsers.has(interaction.user.id)) {
354+
let r = await config.userErrors.blocked(interaction, uInter, other);
355+
interaction.respond(r);
356+
return;
357+
}
358+
if (uInter.guildOnly && !interaction.guildId) {
359+
let r = await config.userErrors.guildOnly(interaction, uInter, other);
360+
interaction.respond(r);
361+
return;
362+
}
363+
if (uInter.calculated.developerOnly && !config.developers.has(interaction.user.id)) {
364+
let r = await config.userErrors.developerOnly(interaction, uInter, other);
365+
interaction.respond(r);
366+
return;
367+
}
368+
if (uInter.calculated.guildOwnerOnly && !config.developers.has(interaction.user.id) && interaction.guild.ownerId != interaction.user.id) {
369+
let r = await config.userErrors.guildOwnerOnly(interaction, uInter, other);
370+
interaction.respond(r);
371+
return;
372+
}
373+
if (uInter.guildOnly && (!config.developers.has(interaction.user.id)) && uInter.perms.user.length != 0 && !uInter.perms.user.every(perm => interaction.member.permissions.has(perm))) {
374+
let r = await config.userErrors.userPermsRequired(interaction, uInter, uInter.perms.user, other);
375+
interaction.respond(r);
376+
return;
377+
}
329378
/** @type {Discord.ApplicationCommandOptionChoice} */
330379
let focussed = null;
331380
try { focussed = interaction.options.getFocused(true) } catch { };
332381
let option = uInter.options.find(i => i.autocomplete && i.name == focussed?.name);
333382
if (option) {
334-
let completeResponse = await option.onComplete(interaction, focussed.value);
335-
interaction.respond(completeResponse);
383+
try {
384+
let completeResponse = await option.onComplete(interaction, focussed.value);
385+
interaction.respond(completeResponse);
386+
} catch (err) {
387+
console.error(`[HATA] "${uInter.actionType == "CHAT_INPUT" ? `/${uInter.name.join(" ")}` : `${uInter.name[0]}`}" adlı interaksiyon için otomatik tamamlama çalıştırılırken bir hata ile karşılaşıldı!`)
388+
if (err.message) console.error(`[HATA] ${err.message}`);
389+
if (err.stack) {
390+
`${err.stack}`.split("\n").forEach((line) => {
391+
console.error(`[HATA] ${line}`);
392+
});
393+
}
394+
}
336395
}
337396
return;
338397
}
339398

340-
let other = {};
341-
342399
{
343400
let locale_id = (interaction.user.locale || interaction.locale)?.split("-")[0];
344401
other.locale = (Underline.locales.get(locale_id) || Underline.locales.get(Underline.config.defaultLanguage)).data;
@@ -384,9 +441,30 @@ client.on("interactionCreate", async (interaction) => {
384441
return;
385442
}
386443

444+
if (uInter.autoDefer != "off") {
445+
const newDefer = () => {
446+
console.warn(`[UYARI] "${uInter.actionType == "CHAT_INPUT" ? `/${uInter.name.join(" ")}` : `${uInter.name[0]}`}" adlı interaksiyon için "deferReply" umursanmadı, interaksiyon zaten otomatik olarak bekleme moduna alınmış.`);
447+
};
448+
if (
449+
interaction.isCommand() || interaction.isApplicationCommand() || interaction.isMessageContextMenu() || interaction.isUserContextMenu()
450+
) {
451+
await interaction.deferReply(uInter.autoDefer == "ephemeral" ? { ephemeral: true } : null);
452+
interaction.deferReply = newDefer;
453+
interaction.reply = interaction.editReply;
454+
} else if (
455+
interaction.isButton() || interaction.isSelectMenu()
456+
) {
457+
await utils.nullDefer(interaction);
458+
interaction.deferReply = newDefer;
459+
interaction.reply = interaction.editReply = interaction.followUp = () => {
460+
console.warn(`[UYARI] "${uInter.name[0]}" adlı interaksiyon için "reply" umursanmadı, interaksiyona zaten otomatik olarak boş cevap verilmiş.`);
461+
};
462+
}
463+
}
464+
387465
if (typeof uInter.coolDown == "number") uInter.coolDown = [{
388466
type: "user",
389-
amount: uInter.coolDown,
467+
amount: uInter.coolDown
390468
}];
391469
if (typeof uInter.coolDown == "object" && !Array.isArray(uInter.coolDown)) uInter.coolDown = [uInter.coolDown];
392470

‎other/utils.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
async nullDefer(interaction) {
3+
await interaction.client.api.interactions(interaction.id, interaction.token).callback.post({
4+
data: {
5+
type: 6,
6+
data: {
7+
flags: null,
8+
},
9+
},
10+
});
11+
return true;
12+
}
13+
}

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@types/recursive-readdir": "^2.2.0"
1919
},
2020
"name": "armagan-basit-altyapi",
21-
"version": "1.9.6",
21+
"version": "1.9.7",
2222
"description": "Kullanımı basit ancak bir yandanda içinde birçek özellik barındıran discord bot altyapısı.",
2323
"main": "index.js",
2424
"repository": {

‎readme.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Basit Altyapı (Versiyon 1.9.6) (v13.x)
1+
# Basit Altyapı (Versiyon 1.9.7) (v13.x)
22

33
Kullanımı basit ancak bir yandanda içinde birçek özellik barındıran discord bot altyapısı. Sık sık güncelleme alıyor. (Slash Commands)
44

‎types/Config.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Config {
1818
/** @type {{[key: string|number]: any}} */
1919
other = {};
2020

21-
/** @type {Command} */
21+
/** @type {import("./Interaction").TOmittedInteraction} */
2222
interactionDefaults = {};
2323

2424
/** @type {Set<string> | Array<string>} */
@@ -51,6 +51,9 @@ class Config {
5151
/** @type {(eventName: "applicationCommandCreate" | "applicationCommandDelete" | "applicationCommandUpdate" | "channelCreate" | "channelDelete" | "channelPinsUpdate" | "channelUpdate" | "debug" | "emojiCreate" | "emojiDelete" | "emojiUpdate" | "error" | "guildBanAdd" | "guildBanRemove" | "guildCreate" | "guildDelete" | "guildIntegrationsUpdate" | "guildMemberAdd" | "guildMemberAvailable" | "guildMemberRemove" | "guildMembersChunk" | "guildMemberUpdate" | "guildUnavailable" | "guildUpdate" | "interaction" | "interactionCreate" | "invalidated" | "invalidRequestWarning" | "inviteCreate" | "inviteDelete" | "message" | "messageCreate" | "messageDelete" | "messageDeleteBulk" | "messageReactionAdd" | "messageReactionRemove" | "messageReactionRemoveAll" | "messageReactionRemoveEmoji" | "messageUpdate" | "presenceUpdate" | "rateLimit" | "ready" | "roleCreate" | "roleDelete" | "roleUpdate" | "shardDisconnect" | "shardError" | "shardReady" | "shardReconnecting" | "shardResume" | "stageInstanceCreate" | "stageInstanceDelete" | "stageInstanceUpdate" | "stickerCreate" | "stickerDelete" | "stickerUpdate" | "threadCreate" | "threadDelete" | "threadListSync" | "threadMembersUpdate" | "threadMemberUpdate" | "threadUpdate" | "typingStart" | "userUpdate" | "voiceStateUpdate" | "warn" | "webhookUpdate", args: [], other: {[key:string|number]: any})=>boolean} */
5252
onEvent = async () => { return true; };
5353

54+
/** @type {(eventName: "applicationCommandCreate" | "applicationCommandDelete" | "applicationCommandUpdate" | "channelCreate" | "channelDelete" | "channelPinsUpdate" | "channelUpdate" | "debug" | "emojiCreate" | "emojiDelete" | "emojiUpdate" | "error" | "guildBanAdd" | "guildBanRemove" | "guildCreate" | "guildDelete" | "guildIntegrationsUpdate" | "guildMemberAdd" | "guildMemberAvailable" | "guildMemberRemove" | "guildMembersChunk" | "guildMemberUpdate" | "guildUnavailable" | "guildUpdate" | "interaction" | "interactionCreate" | "invalidated" | "invalidRequestWarning" | "inviteCreate" | "inviteDelete" | "message" | "messageCreate" | "messageDelete" | "messageDeleteBulk" | "messageReactionAdd" | "messageReactionRemove" | "messageReactionRemoveAll" | "messageReactionRemoveEmoji" | "messageUpdate" | "presenceUpdate" | "rateLimit" | "ready" | "roleCreate" | "roleDelete" | "roleUpdate" | "shardDisconnect" | "shardError" | "shardReady" | "shardReconnecting" | "shardResume" | "stageInstanceCreate" | "stageInstanceDelete" | "stageInstanceUpdate" | "stickerCreate" | "stickerDelete" | "stickerUpdate" | "threadCreate" | "threadDelete" | "threadListSync" | "threadMembersUpdate" | "threadMemberUpdate" | "threadUpdate" | "typingStart" | "userUpdate" | "voiceStateUpdate" | "warn" | "webhookUpdate", args: [], other: {[key:string|number]: any})=>any} */
55+
onAfterEvent = async () => { return; };
56+
5457
/** @type {{[key: string|number]: any}} */
5558
globalObjects = {};
5659

@@ -103,7 +106,8 @@ class Config {
103106
user: []
104107
},
105108
options: [],
106-
defaultPermission: true
109+
defaultPermission: true,
110+
autoDefer: "off"
107111
};
108112

109113
if (
@@ -126,6 +130,7 @@ class Config {
126130
if (typeof arg.onInteraction == "function") this.onInteraction = arg.onInteraction;
127131

128132
if (typeof arg.onEvent == "function") this.onEvent = arg.onEvent;
133+
if (typeof arg.onAfterEvent == "function") this.onAfterEvent = arg.onAfterEvent;
129134
}
130135
}
131136

‎types/Interaction.d.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type CustomApplicationCommandOptionData = (
3838
ApplicationCommandNonOptionsData
3939
| ApplicationCommandChannelOptionData
4040
| ApplicationCommandChoicesData
41-
) & { onComplete(interaction: AutocompleteInteraction, value: string | number): ApplicationCommandOptionChoice[] }
41+
) & { onComplete(interaction: AutocompleteInteraction, value: string | number, other: IOther): ApplicationCommandOptionChoice[] }
4242

4343
type cooldown = {
4444
type: cooldownType;
@@ -54,7 +54,7 @@ export class BaseInteraction {
5454
perms?: { bot: PermissionString[], user: UserPermString[] };
5555
onInteraction(interaction: CommandInteraction | ContextMenuInteraction, other: IOther): void;
5656
toJSON(): MessageButton | MessageSelectMenu | undefined;
57-
publishType?: "globalOnly" | "guildOnly" | "all";
57+
publishType?: "globalOnly" | "guildOnly" | "all" | string;
5858
onLoad?(client: Client): void;
5959
coolDowns: Map<string, number>;
6060
description!: string;
@@ -65,6 +65,8 @@ export class BaseInteraction {
6565
options?: CustomApplicationCommandOptionData[];
6666
defaultPermission?: boolean;
6767
actionType?: ApplicationCommandType | "SELECT_MENU" | "BUTTON";
68+
autoDefer?: "off" | "on" | "ephemeral";
69+
calculated?: { [key: string | number]: any };
6870
isSelectMenu(): this is import("./SelectMenu");
6971
isButton(): this is import("./Button");
7072
isChatActionCommand(): this is import("./SlashCommand");
@@ -73,7 +75,7 @@ export class BaseInteraction {
7375
constructor(arg: TInteractionConstructor);
7476
}
7577

76-
export type TOmittedInteraction = Omit<BaseInteraction, "_type" | "coolDowns" | "name" | "onInteraction" | "actionType" | "options" | "toJSON">;
78+
export type TOmittedInteraction = Omit<BaseInteraction, "_type" | "coolDowns" | "name" | "onInteraction" | "actionType" | "options" | "toJSON" | "calculated">;
7779
export type TInteractionConstructor = TOmittedInteraction & ((ActionChatCommand | ActionRightClickCommand | SelectMenu | Button));
7880
type cooldownType = "user" | "member" | "channel" | "guild" | "message" | "any";
7981
export interface IOther {

‎types/Interaction.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class Interaction {
3737

3838
actionType = "CHAT_INPUT"
3939

40+
autoDefer = "off";
41+
42+
calculated = {};
43+
4044
constructor(arg = {}) {
4145
this.name = Array.isArray(arg.name) ? arg.name : [arg.name];
4246
this.actionType = arg.actionType || Underline.config.interactionDefaults.actionType;
@@ -56,6 +60,7 @@ class Interaction {
5660
this.options = arg.options;
5761
this.publishType = arg.publishType ?? "all";
5862
this.defaultPermission = Boolean(arg.defaultPermission ?? Underline.config.interactionDefaults.defaultPermission);
63+
this.autoDefer = arg.autoDefer ?? Underline.config.interactionDefaults.autoDefer;
5964
}
6065
}
6166

0 commit comments

Comments
 (0)