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

209 lines
11 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
2016-08-20 14:10:37 +00:00
using NadekoBot.Attributes;
using NadekoBot.Extensions;
2016-03-26 02:25:03 +00:00
using System;
using System.Linq;
using System.Text;
2016-08-20 14:10:37 +00:00
using System.Threading.Tasks;
using NadekoBot.Services;
2016-08-20 14:10:37 +00:00
//todo DB
namespace NadekoBot.Modules.Gambling
{
2016-08-20 14:10:37 +00:00
[Module("$", AppendSpace = false)]
public partial class Gambling : DiscordModule
{
2016-08-20 14:10:37 +00:00
public Gambling(ILocalization loc, CommandService cmds, IBotConfiguration config, IDiscordClient client) : base(loc, cmds, config, client)
{
}
2016-08-20 14:10:37 +00:00
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task Raffle(IMessage imsg, [Remainder] IRole role = null)
{
var channel = imsg.Channel as ITextChannel;
role = role ?? channel.Guild.EveryoneRole;
var members = (await role.Members()).Where(u => u.Status == UserStatus.Online);
var membersArray = members as IUser[] ?? members.ToArray();
var usr = membersArray[new Random().Next(0, membersArray.Length)];
await imsg.Channel.SendMessageAsync($"**Raffled user:** {usr.Username} (id: {usr.Id})").ConfigureAwait(false);
2016-08-20 14:10:37 +00:00
}
public override void Install(ModuleManager manager)
{
manager.CreateCommands("", cgb =>
{
cgb.CreateCommand(Prefix + "raffle")
2016-07-28 20:26:05 +00:00
.Description($"Prints a name and ID of a random user from the online list from the (optional) role. | `{Prefix}raffle` or `{Prefix}raffle RoleName`")
2016-03-26 02:25:03 +00:00
.Parameter("role", ParameterType.Optional)
.Do(async e =>
{
2016-08-20 14:10:37 +00:00
});
cgb.CreateCommand(Prefix + "$$")
.Description(string.Format("Check how much {0}s a person has. (Defaults to yourself) |`{1}$$` or `{1}$$ @Someone`",
2016-05-13 17:30:11 +00:00
NadekoBot.Config.CurrencyName, Prefix))
.Parameter("all", ParameterType.Unparsed)
.Do(async e =>
{
var usr = e.Message.MentionedUsers.FirstOrDefault() ?? e.User;
var pts = GetUserFlowers(usr.Id);
var str = $"{usr.Name} has {pts} {NadekoBot.Config.CurrencySign}";
await imsg.Channel.SendMessageAsync(str).ConfigureAwait(false);
});
2016-03-26 02:25:03 +00:00
cgb.CreateCommand(Prefix + "give")
.Description(string.Format("Give someone a certain amount of {0}s", NadekoBot.Config.CurrencyName)+ $"|`{Prefix}give 1 \"@SomeGuy\"`")
2016-03-26 02:25:03 +00:00
.Parameter("amount", ParameterType.Required)
.Parameter("receiver", ParameterType.Unparsed)
.Do(async e =>
{
var amountStr = e.GetArg("amount")?.Trim();
long amount;
2016-07-17 19:29:58 +00:00
if (!long.TryParse(amountStr, out amount) || amount <= 0)
2016-03-26 02:25:03 +00:00
return;
var mentionedUser = e.Message.MentionedUsers.FirstOrDefault(u =>
u.Id != NadekoBot.Client.CurrentUser.Id &&
u.Id != e.User.Id);
if (mentionedUser == null)
return;
var userFlowers = GetUserFlowers(e.User.Id);
2016-03-26 02:25:03 +00:00
if (userFlowers < amount)
{
await imsg.Channel.SendMessageAsync($"{e.User.Mention} You don't have enough {NadekoBot.Config.CurrencyName}s. You only have {userFlowers}{NadekoBot.Config.CurrencySign}.").ConfigureAwait(false);
2016-03-26 02:25:03 +00:00
return;
}
2016-07-17 12:11:46 +00:00
await FlowersHandler.RemoveFlowers(e.User, "Gift", (int)amount, true).ConfigureAwait(false);
2016-04-18 21:38:19 +00:00
await FlowersHandler.AddFlowersAsync(mentionedUser, "Gift", (int)amount).ConfigureAwait(false);
2016-03-26 02:25:03 +00:00
await imsg.Channel.SendMessageAsync($"{e.User.Mention} successfully sent {amount} {NadekoBot.Config.CurrencyName}s to {mentionedUser.Mention}!").ConfigureAwait(false);
2016-03-26 02:25:03 +00:00
});
cgb.CreateCommand(Prefix + "award")
2016-07-26 01:39:14 +00:00
.Description($"Gives someone a certain amount of flowers. **Bot Owner Only!** | `{Prefix}award 100 @person`")
2016-04-14 22:17:29 +00:00
.AddCheck(SimpleCheckers.OwnerOnly())
.Parameter("amount", ParameterType.Required)
.Parameter("receiver", ParameterType.Unparsed)
.Do(async e =>
{
var amountStr = e.GetArg("amount")?.Trim();
long amount;
if (!long.TryParse(amountStr, out amount) || amount < 0)
return;
var mentionedUser = e.Message.MentionedUsers.FirstOrDefault(u =>
u.Id != NadekoBot.Client.CurrentUser.Id);
if (mentionedUser == null)
return;
2016-04-18 21:38:19 +00:00
await FlowersHandler.AddFlowersAsync(mentionedUser, $"Awarded by bot owner. ({e.User.Name}/{e.User.Id})", (int)amount).ConfigureAwait(false);
await imsg.Channel.SendMessageAsync($"{e.User.Mention} successfully awarded {amount} {NadekoBot.Config.CurrencyName}s to {mentionedUser.Mention}!").ConfigureAwait(false);
});
cgb.CreateCommand(Prefix + "take")
2016-07-26 01:58:11 +00:00
.Description($"Takes a certain amount of flowers from someone. **Bot Owner Only!** | `{Prefix}take 1 \"@someguy\"`")
2016-04-14 22:17:29 +00:00
.AddCheck(SimpleCheckers.OwnerOnly())
.Parameter("amount", ParameterType.Required)
.Parameter("rektperson", ParameterType.Unparsed)
.Do(async e =>
{
var amountStr = e.GetArg("amount")?.Trim();
long amount;
if (!long.TryParse(amountStr, out amount) || amount < 0)
return;
var mentionedUser = e.Message.MentionedUsers.FirstOrDefault(u =>
u.Id != NadekoBot.Client.CurrentUser.Id);
if (mentionedUser == null)
return;
2016-07-17 10:34:44 +00:00
await FlowersHandler.RemoveFlowers(mentionedUser, $"Taken by bot owner.({e.User.Name}/{e.User.Id})", (int)amount).ConfigureAwait(false);
await imsg.Channel.SendMessageAsync($"{e.User.Mention} successfully took {amount} {NadekoBot.Config.CurrencyName}s from {mentionedUser.Mention}!").ConfigureAwait(false);
});
2016-07-17 10:34:44 +00:00
cgb.CreateCommand(Prefix + "betroll")
.Alias(Prefix + "br")
2016-07-28 20:26:05 +00:00
.Description($"Bets a certain amount of {NadekoBot.Config.CurrencyName}s and rolls a dice. Rolling over 66 yields x2 flowers, over 90 - x3 and 100 x10. | `{Prefix}br 5`")
.Parameter("amount",ParameterType.Required)
2016-07-17 10:34:44 +00:00
.Do(async e =>
{
var amountstr = e.GetArg("amount").Trim();
int amount;
if (!int.TryParse(amountstr, out amount) || amount < 1)
return;
var userFlowers = GetUserFlowers(e.User.Id);
2016-07-17 10:34:44 +00:00
if (userFlowers < amount)
{
await imsg.Channel.SendMessageAsync($"{e.User.Mention} You don't have enough {NadekoBot.Config.CurrencyName}s. You only have {userFlowers}{NadekoBot.Config.CurrencySign}.").ConfigureAwait(false);
2016-07-17 10:34:44 +00:00
return;
}
await FlowersHandler.RemoveFlowers(e.User, "Betroll Gamble", (int)amount, true).ConfigureAwait(false);
2016-07-17 10:34:44 +00:00
var rng = new Random().Next(0, 101);
var str = $"{e.User.Mention} `You rolled {rng}.` ";
if (rng < 67)
{
str += "Better luck next time.";
}
else if (rng < 90)
{
str += $"Congratulations! You won {amount * 2}{NadekoBot.Config.CurrencySign} for rolling above 66";
await FlowersHandler.AddFlowersAsync(e.User, "Betroll Gamble", amount * 2, true).ConfigureAwait(false);
2016-07-17 10:34:44 +00:00
}
else if (rng < 100)
{
str += $"Congratulations! You won {amount * 3}{NadekoBot.Config.CurrencySign} for rolling above 90.";
await FlowersHandler.AddFlowersAsync(e.User, "Betroll Gamble", amount * 3, true).ConfigureAwait(false);
2016-07-17 10:34:44 +00:00
}
else {
2016-07-17 10:34:44 +00:00
str += $"👑 Congratulations! You won {amount * 10}{NadekoBot.Config.CurrencySign} for rolling **100**. 👑";
await FlowersHandler.AddFlowersAsync(e.User, "Betroll Gamble", amount * 10, true).ConfigureAwait(false);
2016-07-17 10:34:44 +00:00
}
await imsg.Channel.SendMessageAsync(str).ConfigureAwait(false);
2016-07-17 10:34:44 +00:00
});
cgb.CreateCommand(Prefix + "leaderboard")
.Alias(Prefix + "lb")
2016-08-02 00:00:52 +00:00
.Description($"Displays bot currency leaderboard | `{Prefix}lb`")
.Do(async e =>
{
var richestTemp = DbHandler.Instance.GetTopRichest();
var richest = richestTemp as CurrencyState[] ?? richestTemp.ToArray();
if (richest.Length == 0)
return;
await imsg.Channel.SendMessageAsync(
richest.Aggregate(new StringBuilder(
$@"```xl
Id $$$
"),
(cur, cs) => cur.AppendLine(
$@"┣━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━┫
{(e.Server.Users.Where(u => u.Id == (ulong)cs.UserId).FirstOrDefault()?.Name.TrimTo(18, true) ?? cs.UserId.ToString()),-20} {cs.Value,5} ")
2016-07-17 10:34:44 +00:00
).ToString() + "┗━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━┛```").ConfigureAwait(false);
});
});
}
public static long GetUserFlowers(ulong userId) =>
Classes.DbHandler.Instance.GetStateByUserId((long)userId)?.Value ?? 0;
}
}