Androz2091 / discord-giveaways

🎉 Complete framework to facilitate the creation of giveaways using discord.js
https://discord-giveaways.js.org
MIT License
334 stars 127 forks source link

How to send the giveaways list for a server? #62

Closed inxec closed 4 years ago

inxec commented 4 years ago

It would be REALLY cool if theres actually a way to list ALL the running giveaways in a server. I saw the "Fetch Giveaways" section in discord-giveaways package, but I dont really understand how it works? Well I mean i do understand the let onServer = client.giveawaysManager.giveaways.filter((g) => g.guildID === "guild id"), but idk how use that to show the running giveaways.

Androz2091 commented 4 years ago

Hello, you can use the following code:

if(message.content === "list"){
    // Get all the giveaways for the current guild
    let giveaways = client.giveawaysManager.giveaways.filter((g) => g.guildID === message.guild.id);
    // Keep the giveaways that are not ended
    let notEndedGiveaways = giveaways.filter((g) => !g.ended);
    message.channel.send(`Here are the current giveaways: ${notEndedGiveaways.map((g) => g.messageID).join(', ')}`);
}

This will send something like:

Here are the current giveaways: 618121508929142833, 618120950386262056, 665941399211868200
inxec commented 4 years ago

Hello, you can use the following code:

if(message.content === "list"){
    // Get all the giveaways for the current guild
    let giveaways = client.giveawaysManager.giveaways.filter((g) => g.guildID === message.guild.id);
    // Keep the giveaways that are not ended
    let notEndedGiveaways = giveaways.filter((g) => !g.ended);
    message.channel.send(`Here are the current giveaways: ${notEndedGiveaways.map((g) => g.messageID).join(', ')}`);
}

This will send something like:

Here are the current giveaways: 618121508929142833, 618120950386262056, 665941399211868200

Is there a way to show the prize?

SimonLeclere commented 4 years ago
const giveaways = client.giveawaysManager.giveaways.filter((g) => g.guildID === message.guild.id);
const notEndedGiveaways = giveaways.filter((g) => !g.ended);

if(notEndedGiveaways.length === 0) return message.channel.send('No giveaway currently active on this server.');

message.channel.send(`Here are the current giveaways : \n${notEndedGiveaways.map((g) => `${g.data.prize} - ${g.messageID}`).join('\n')}`);
inxec commented 4 years ago
const giveaways = client.giveawaysManager.giveaways.filter((g) => g.guildID === message.guild.id);
const notEndedGiveaways = giveaways.filter((g) => !g.ended);

if(notEndedGiveaways.length === 0) return message.channel.send('No giveaway currently active on this server.');

message.channel.send(`Here are the current giveaways : \n${notEndedGiveaways.map((g) => `${g.data.prize} - ${g.messageID}`).join('\n')}`);

Hi! Last question, is there a way to show the user who hosted the giveaway, and the time remaining? ❤ Thanks

SimonLeclere commented 4 years ago

To display the user who hosts the giveaway you can use g.hostedBy, for the time you can subtract g.endAt and g.startAt (i think)

birbified commented 4 years ago

Is there any way you could use .hostedBy without pinging the person?

Androz2091 commented 4 years ago

You could use something like that:

message.channel.send(`Here are the current giveaways : \n${notEndedGiveaways.map((g) => {
    const userID = g.data.hostedBy.split('<@')[0].split('>')[0];
    const user = message.client.users.cache.get(userID);
    return `${user.tag} - ${g.messageID}`;
}).join('\n')}`;

This is not really clean but at this time it's the only way... Or you could use a regular expression but it's not easy to understand.

birbified commented 4 years ago

Thanks!

birbified commented 4 years ago

I thought it was g.hostedBy and not g.data.hostedBy.

birbified commented 4 years ago

How do I make it send a message if there are no active giveaways. I've tried using if statements but then I always get a DiscordAPIError that says: Cannot send a empty message.

SimonLeclere commented 4 years ago
const giveaways = message.client.GiveawaysManager.giveaways.filter((g) => g.guildID === message.guild.id);
const notEndedGiveaways = giveaways.filter((g) => !g.ended);

if(notEndedGiveaways.length === 0) return message.channel.send('No giveaway in progress on this server.');
birbified commented 4 years ago

How do you get the channel?

SimonLeclere commented 4 years ago
const giveaways = client.giveawaysManager.giveaways.filter((g) => g.guildID === message.guild.id);
const notEndedGiveaways = giveaways.filter((g) => !g.ended);

if(notEndedGiveaways.length === 0) return message.channel.send('No giveaway currently active on this server.');

message.channel.send(`Here are the current giveaways : \n${notEndedGiveaways.map((g) => `${g.data.prize} - ${g.messageID} - ${g.channelID}`).join('\n')}`);
Nico105 commented 4 years ago

const giveaways = client.giveawaysManager.giveaways.filter((g) => g.guildID === message.guild.id && client.channels.cache.get(g.channelID));

You all have to use this because otherwise if a channel where a giveaway is hosted in, gets deleted, the command would still display it as active