Refactor Timeout Module
manh21 opened this issue · 3 comments
manh21 commented
dhafitf commented
manh21 commented
Nanti jadi gak bisa nge-timeout pake ID, bisanya pake mention. Gimana?
Udah ada utility function buat dapetin user yang dimention.
Code nya bisa dilihat disini:
Lines 17 to 29 in 314e6ac
misal format command timeout begini ;timeout @manh21 28s
bisa dibuat begini
const timeCalc = {
s: (1),
m: (60),
h: (60 * 60),
d: (60 * 60 * 24)
};
const user = getUserFromMention(args[0]);
if(!user) return; // User tidak ditemukan
const timeType = args[1].slice(-1);
if(!['s', 'm', 'h', 'd'].includes(timeType)) return; // Check format waktu apakah sudah sesuai
const duration = parseInt(args[1].slice(0, -1)) || 0;
const durationMs = Math.floor(duration * (timeCalc[timeType] ?? 1) * 1000); // Convert duration to miliseconds
if(durationMs >= 2419200001 ) return; // check apakah durasi melebihi batas maksimum
Kalo mau dibuat multiple user bisa begini juga
format: ;timeout @manh21 @dhafitf 28s
const timeCalc = {
s: (1),
m: (60),
h: (60 * 60),
d: (60 * 60 * 24)
};
// Ambil user array
const userArr = [];
args.foreach((x, i) => {
if(!args[i+2]) return; // Check dua array ke depan apakah masih ada, last array adalah waktu jadi +2
const user = getUserFromMention(args[0]);
if(!user) return; // bukan user
userArr.push(user);
})
const timeType = args[-1].slice(-1);
if(!['s', 'm', 'h', 'd'].includes(timeType)) return; // Check format waktu apakah sudah sesuai
const duration = parseInt(args[-1].slice(0, -1)) || 0;
const durationMs = Math.floor(duration * (timeCalc[timeType] ?? 1) * 1000); // Convert duration to miliseconds
if(durationMs >= 2419200001 ) return; // check apakah durasi melebihi batas maksimum
dhafitf commented
Lines 17 to 29 in 314e6ac
Sepertinya code diatas hanyalah untuk mendapatkan User. Sedangkan timeout ada di GuildMember. Jadi masih perlu penambahan lagi
let user = getUserFromMention(args[0], client);
if (!user) {
user = args[0];
}
let member;
try {
member = await msg.guild.members.fetch(user);
} catch (error) {
return msg.reply(":x: User not found");
}
Full code kira-kira bakalan kaya gini
const split = msg.content.split(/ +/);
const command = split[0].toLowerCase();
const args = split.slice(1);
if (command === `${process.env.PREFIX}timeout`) {
if (!checkRoles(msg)) return;
let user = getUserFromMention(args[0], client);
if (!user) {
user = args[0];
} // biar bisa pake id
let member;
try {
member = await msg.guild.members.fetch(user);
} catch (error) {
return msg.reply(":x: User not found");
} // untuk mendapatkan GuildMember
if (args.length < 2) {
return msg.reply("Please mention the user & set the time");
} // apabila author tidak mencantumkan user dan juga waktu
if (
member.roles.highest.position >= msg.guild.me.roles.highest.position &&
msg.member.roles.highest.position
) {
return msg.reply(
"You can't do this to user with the same or a higher role."
);
} // untuk mengecek posisi role
const timeCalc = {
s: 1,
m: 60,
h: 60 * 60,
d: 60 * 60 * 24,
};
const timeType = args[1].slice(-1);
if (!["s", "m", "h", "d"].includes(timeType)) return; // Check format waktu apakah sudah sesuai
const duration = parseInt(args[1].slice(0, -1)) || 0;
const durationMs = Math.floor(duration * (timeCalc[timeType] ?? 1) * 1000); // Convert duration to miliseconds
const reason = args.slice(2).join(" ");
const timeoutEmbed = new MessageEmbed()
.setColor("#992d22")
.setDescription(`<@${member.id}> has been timeout`);
member.timeout(durationMs, reason).catch(console.error());
if (durationMs > 2419200001) {
return msg.reply("Maximal timeout is 28d");
}
sendMsg(msg.channel, { embeds: [timeoutEmbed] });
}