Giter Club home page Giter Club logo

Comments (3)

dhafitf avatar dhafitf commented on July 19, 2024

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?

from wpu_bot.

manh21 avatar manh21 commented on July 19, 2024

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

from wpu_bot.

dhafitf avatar dhafitf commented on July 19, 2024

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

from wpu_bot.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.