bellshade/wpu_bot

Refactor Timeout Module

manh21 opened this issue · 3 comments

Screenshot_1173
Screenshot_1177
Screenshot_1175
Screenshot_1176

Mentok di 17.

Sebenarnya bisa dikurangin lagi, tapi ngorbanin ini

if (!user) {
  userId = args[0];
} else {
  userId = user.id;
}

Nanti jadi gak bisa nge-timeout pake ID, bisanya pake mention. Gimana?

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:

function getUserFromMention(mention) {
if (!mention) return false;
if (mention.startsWith("<@") && mention.endsWith(">")) {
mention = mention.slice(2, -1);
if (mention.startsWith("!")) {
mention = mention.slice(1);
}
return new Discord.Client().users.cache.get(mention);
}
}

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

function getUserFromMention(mention) {
if (!mention) return false;
if (mention.startsWith("<@") && mention.endsWith(">")) {
mention = mention.slice(2, -1);
if (mention.startsWith("!")) {
mention = mention.slice(1);
}
return new Discord.Client().users.cache.get(mention);
}
}

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] });
  }