NadekoBot/src/NadekoBot/Modules/Gambling/Gambling.cs

286 lines
12 KiB
C#
Raw Normal View History

using System;
using Discord;
using Discord.Commands;
2016-08-20 14:10:37 +00:00
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using System.Linq;
2016-08-20 14:10:37 +00:00
using System.Threading.Tasks;
using NadekoBot.Services;
using NadekoBot.Services.Database.Models;
using System.Collections.Generic;
namespace NadekoBot.Modules.Gambling
{
[NadekoModule("Gambling", "$")]
public partial class Gambling : NadekoTopLevelModule
{
public static string CurrencyName { get; set; }
public static string CurrencyPluralName { get; set; }
public static string CurrencySign { get; set; }
static Gambling()
{
2017-01-12 00:21:32 +00:00
CurrencyName = NadekoBot.BotConfig.CurrencyName;
CurrencyPluralName = NadekoBot.BotConfig.CurrencyPluralName;
CurrencySign = NadekoBot.BotConfig.CurrencySign;
}
2016-11-26 09:54:33 +00:00
public static long GetCurrency(ulong id)
{
using (var uow = DbHandler.UnitOfWork())
{
return uow.Currency.GetUserCurrency(id);
}
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-20 14:10:37 +00:00
[RequireContext(ContextType.Guild)]
2016-12-17 00:16:14 +00:00
public async Task Raffle([Remainder] IRole role = null)
2016-08-20 14:10:37 +00:00
{
2016-12-17 00:16:14 +00:00
role = role ?? Context.Guild.EveryoneRole;
2016-08-20 14:10:37 +00:00
var members = role.Members().Where(u => u.Status != UserStatus.Offline && u.Status != UserStatus.Unknown);
2016-08-20 14:10:37 +00:00
var membersArray = members as IUser[] ?? members.ToArray();
var usr = membersArray[new NadekoRandom().Next(0, membersArray.Length)];
await Context.Channel.SendConfirmAsync("🎟 "+ GetText("raffled_user"), $"**{usr.Username}#{usr.Discriminator}**", footer: $"ID: {usr.Id}").ConfigureAwait(false);
2016-08-20 14:10:37 +00:00
}
2016-10-08 18:24:34 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-10-19 05:16:47 +00:00
[Priority(0)]
2016-12-17 00:16:14 +00:00
public async Task Cash([Remainder] IUser user = null)
{
if(user == null)
await ConfirmLocalized("has", Format.Bold(Context.User.ToString()), $"{GetCurrency(Context.User.Id)} {CurrencySign}").ConfigureAwait(false);
else
await ReplyConfirmLocalized("has", Format.Bold(user.ToString()), $"{GetCurrency(user.Id)} {CurrencySign}").ConfigureAwait(false);
}
2016-10-08 18:24:34 +00:00
[NadekoCommand, Usage, Description, Aliases]
2016-10-19 05:16:47 +00:00
[Priority(1)]
2016-12-17 00:16:14 +00:00
public async Task Cash(ulong userId)
2016-10-08 18:24:34 +00:00
{
await ReplyConfirmLocalized("has", Format.Code(userId.ToString()), $"{GetCurrency(userId)} {CurrencySign}").ConfigureAwait(false);
2016-10-08 18:24:34 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-17 00:16:14 +00:00
public async Task Give(long amount, [Remainder] IGuildUser receiver)
{
2016-12-16 18:43:57 +00:00
if (amount <= 0 || Context.User.Id == receiver.Id)
return;
2017-01-23 19:41:48 +00:00
var success = await CurrencyHandler.RemoveCurrencyAsync((IGuildUser)Context.User, $"Gift to {receiver.Username} ({receiver.Id}).", amount, false).ConfigureAwait(false);
if (!success)
{
await ReplyErrorLocalized("not_enough", CurrencyPluralName).ConfigureAwait(false);
return;
}
2016-12-16 18:43:57 +00:00
await CurrencyHandler.AddCurrencyAsync(receiver, $"Gift from {Context.User.Username} ({Context.User.Id}).", amount, true).ConfigureAwait(false);
await ReplyConfirmLocalized("gifted", amount + CurrencySign, Format.Bold(receiver.ToString()))
.ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
2016-09-14 12:23:09 +00:00
[RequireContext(ContextType.Guild)]
[OwnerOnly]
[Priority(2)]
2016-12-17 00:16:14 +00:00
public Task Award(int amount, [Remainder] IGuildUser usr) =>
Award(amount, usr.Id);
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
[Priority(1)]
2016-12-17 00:16:14 +00:00
public async Task Award(int amount, ulong usrId)
2016-09-14 12:23:09 +00:00
{
if (amount <= 0)
return;
2016-12-16 18:43:57 +00:00
await CurrencyHandler.AddCurrencyAsync(usrId, $"Awarded by bot owner. ({Context.User.Username}/{Context.User.Id})", amount).ConfigureAwait(false);
await ReplyConfirmLocalized("awarded", amount + CurrencySign, $"<@{usrId}>").ConfigureAwait(false);
2016-09-14 12:23:09 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
[Priority(0)]
2016-12-17 00:16:14 +00:00
public async Task Award(int amount, [Remainder] IRole role)
{
2016-12-17 04:09:04 +00:00
var users = (await Context.Guild.GetUsersAsync())
.Where(u => u.GetRoles().Contains(role))
.ToList();
await Task.WhenAll(users.Select(u => CurrencyHandler.AddCurrencyAsync(u.Id,
2016-12-16 18:43:57 +00:00
$"Awarded by bot owner to **{role.Name}** role. ({Context.User.Username}/{Context.User.Id})",
amount)))
.ConfigureAwait(false);
await ReplyConfirmLocalized("mass_award",
amount + CurrencySign,
Format.Bold(users.Count.ToString()),
Format.Bold(role.Name)).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
2016-12-17 00:16:14 +00:00
public async Task Take(long amount, [Remainder] IGuildUser user)
{
if (amount <= 0)
return;
if (await CurrencyHandler.RemoveCurrencyAsync(user, $"Taken by bot owner.({Context.User.Username}/{Context.User.Id})", amount, true).ConfigureAwait(false))
await ReplyConfirmLocalized("take", amount+CurrencySign, Format.Bold(user.ToString())).ConfigureAwait(false);
2016-10-08 18:24:34 +00:00
else
await ReplyErrorLocalized("take_fail", amount + CurrencySign, Format.Bold(user.ToString()), CurrencyPluralName).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
2016-12-17 00:16:14 +00:00
public async Task Take(long amount, [Remainder] ulong usrId)
{
if (amount <= 0)
return;
if (await CurrencyHandler.RemoveCurrencyAsync(usrId, $"Taken by bot owner.({Context.User.Username}/{Context.User.Id})", amount).ConfigureAwait(false))
await ReplyConfirmLocalized("take", amount + CurrencySign, $"<@{usrId}>").ConfigureAwait(false);
2016-10-08 18:24:34 +00:00
else
await ReplyErrorLocalized("take_fail", amount + CurrencySign, Format.Code(usrId.ToString()), CurrencyPluralName).ConfigureAwait(false);
}
2017-01-22 20:29:41 +00:00
//[NadekoCommand, Usage, Description, Aliases]
//[OwnerOnly]
//public Task BrTest(int tests = 1000)
//{
// var t = Task.Run(async () =>
// {
// if (tests <= 0)
// return;
// //multi vs how many times it occured
// var dict = new Dictionary<int, int>();
// var generator = new NadekoRandom();
// for (int i = 0; i < tests; i++)
// {
// var rng = generator.Next(0, 101);
// var mult = 0;
// if (rng < 67)
// {
// mult = 0;
// }
// else if (rng < 91)
// {
// mult = 2;
// }
// else if (rng < 100)
// {
// mult = 4;
// }
// else
// mult = 10;
// if (dict.ContainsKey(mult))
// dict[mult] += 1;
// else
// dict.Add(mult, 1);
// }
// var sb = new StringBuilder();
// const int bet = 1;
// int payout = 0;
// foreach (var key in dict.Keys.OrderByDescending(x => x))
// {
// sb.AppendLine($"x{key} occured {dict[key]} times. {dict[key] * 1.0f / tests * 100}%");
// payout += key * dict[key];
// }
// try
// {
// await Context.Channel.SendConfirmAsync("BetRoll Test Results", sb.ToString(),
// footer: $"Total Bet: {tests * bet} | Payout: {payout * bet} | {payout * 1.0f / tests * 100}%");
// }
// catch { }
// });
// return Task.CompletedTask;
//}
[NadekoCommand, Usage, Description, Aliases]
2016-12-17 00:16:14 +00:00
public async Task BetRoll(long amount)
{
if (amount < 1)
return;
if (!await CurrencyHandler.RemoveCurrencyAsync(Context.User, "Betroll Gamble", amount, false).ConfigureAwait(false))
{
await ReplyErrorLocalized("not_enough", CurrencyPluralName).ConfigureAwait(false);
return;
}
var rnd = new NadekoRandom().Next(0, 101);
var str = Context.User.Mention + Format.Code(GetText("roll", rnd));
if (rnd < 67)
{
str += GetText("better_luck");
}
else
{
if (rnd < 91)
{
str += GetText("br_win", (amount * NadekoBot.BotConfig.Betroll67Multiplier) + CurrencySign, 66);
await CurrencyHandler.AddCurrencyAsync(Context.User, "Betroll Gamble",
(int) (amount * NadekoBot.BotConfig.Betroll67Multiplier), false).ConfigureAwait(false);
}
else if (rnd < 100)
{
str += GetText("br_win", (amount * NadekoBot.BotConfig.Betroll91Multiplier) + CurrencySign, 90);
await CurrencyHandler.AddCurrencyAsync(Context.User, "Betroll Gamble",
(int) (amount * NadekoBot.BotConfig.Betroll91Multiplier), false).ConfigureAwait(false);
}
else
{
str += GetText("br_win", (amount * NadekoBot.BotConfig.Betroll100Multiplier) + CurrencySign, 100) + " 👑";
await CurrencyHandler.AddCurrencyAsync(Context.User, "Betroll Gamble",
(int) (amount * NadekoBot.BotConfig.Betroll100Multiplier), false).ConfigureAwait(false);
}
}
2016-12-16 21:44:26 +00:00
await Context.Channel.SendConfirmAsync(str).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
2017-03-09 01:17:18 +00:00
public async Task Leaderboard(int page = 1)
{
List<Currency> richest;
using (var uow = DbHandler.UnitOfWork())
{
2017-03-09 01:17:18 +00:00
richest = uow.Currency.GetTopRichest(9, 9 * (page - 1)).ToList();
}
2017-01-23 00:33:07 +00:00
var embed = new EmbedBuilder()
.WithOkColor()
2017-03-09 01:17:18 +00:00
.WithTitle(NadekoBot.BotConfig.CurrencySign +
" " + GetText("leaderboard"))
.WithFooter(efb => efb.WithText(GetText("page", page)));
if (!richest.Any())
{
embed.WithDescription(GetText("no_users_found"));
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
return;
}
2017-01-23 00:33:07 +00:00
for (var i = 0; i < richest.Count; i++)
{
var x = richest[i];
var usr = await Context.Guild.GetUserAsync(x.UserId).ConfigureAwait(false);
var usrStr = usr == null
? x.UserId.ToString()
: usr.Username?.TrimTo(20, true);
var j = i;
embed.AddField(efb => efb.WithName("#" + (j + 1) + " " + usrStr)
.WithValue(x.Amount.ToString() + " " + NadekoBot.BotConfig.CurrencySign)
.WithIsInline(true));
2017-01-23 00:33:07 +00:00
}
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
}
}
}