コード例 #1
0
        public static async Task Giveaway(IGuild guild, IMessage message, uint timeInHours, uint money, IRole role = null)
        {
            await message.DeleteAsync();

            string reward = "";

            var emoji = new Emoji("🎊");
            var coin  = Messages.coin;

            if (money == 0 && role == null)
            {
                return;
            }
            if (money == 0 && role != null)
            {
                reward = $"👤 {role.Mention}";
            }
            if (money != 0 && role == null)
            {
                reward = $"{coin} {money}";
            }
            if (money != 0 && role != null)
            {
                reward = $"{coin} {money}\n👤 {role.Mention}";
            }

            var   GuildAccount      = GuildAccounts.GetAccount(guild);
            var   ContextGuild      = guild as SocketGuild;
            ulong GiveawayChannelID = GuildAccount.GiveawayChannelID;
            var   GiveawayChannel   = ContextGuild.GetChannel(GiveawayChannelID) as IMessageChannel;

            uint TimeInMilisecs = timeInHours * 3600000;

            var time  = DateTime.Now;
            var time2 = time.AddHours(TimeInMilisecs);
            var time3 = time2.ToString("HH:mm");

            EmbedBuilder eb = new EmbedBuilder();

            eb.WithAuthor("GIVEAWAY");
            eb.Author.WithIconUrl("https://freeiconshop.com/wp-content/uploads/edd/gift-flat.png");
            eb.WithDescription($"**Nagroda:**\n {reward}\n\n**Wyniki o:** {time3}");
            eb.WithFooter("👇 ZAREAGUJ ABY WZIĄĆ UDZIAŁ!");
            eb.WithColor(Color.Gold);

            RestUserMessage msg = (RestUserMessage)await GiveawayChannel.SendMessageAsync("@everyone", false, eb.Build());

            Global.GiveawayMessageID = msg.Id;
            await msg.AddReactionAsync(emoji);

            await Task.Delay(Convert.ToInt32(TimeInMilisecs));

            await msg.UpdateAsync();

            var    UsersThatReacted = (await msg.GetReactionUsersAsync(emoji)).Where(u => u.IsBot == false).ToArray();
            Random rand             = new Random();
            var    randomuser       = UsersThatReacted[rand.Next(UsersThatReacted.Length)];
            await GiveawayChannel.SendMessageAsync($":confetti_ball: GRATULACJE :confetti_ball:\n{randomuser.Mention} wygrywa.");

            try
            {
                var UserToAddRole = ContextGuild.GetUser(randomuser.Id);
                await UserToAddRole.AddRoleAsync(role);
            }
            catch { }
            try
            {
                var UserAccount = UserAccounts.GetAccount(randomuser);
                UserAccount.MoneyAccount += money;
                UserAccounts.SaveAccounts();
            }
            catch { }
        }