Пример #1
0
        public async Task AddCustReact(string key, [Remainder] string message)
        {
            var channel = Context.Channel as ITextChannel;

            if (!string.IsNullOrWhiteSpace(message) && !string.IsNullOrWhiteSpace(key))
            {
                if ((channel != null || _creds.IsOwner(Context.User)) && (channel == null || ((IGuildUser)Context.User).GuildPermissions.Administrator))
                {
                    var cr = new CustomReaction {
                        GuildId  = channel?.Guild.Id,
                        IsRegex  = false,
                        Trigger  = key,
                        Response = message,
                    };

                    uow.CustomReactions.Add(cr);

                    await uow.SaveChangesAsync(false).ConfigureAwait(false);

                    var eb = new EmbedBuilder().WithOkColor()
                             .WithTitle(GetText("addcustreact_new_cust_react"))
                             .WithDescription($"#{cr.Id}")
                             .AddField(efb => efb.WithName(GetText("addcustreact_trigger")).WithValue(key))
                             .AddField(efb => efb.WithName(GetText("addcustreact_response")).WithValue(message.Length > 1024 ? GetText("addcustreact_redacted_too_long") : message));

                    await Context.Channel.EmbedAsync(eb).ConfigureAwait(false);
                }
                else
                {
                    await ReplyErrorLocalized("addcustreact_insuff_perms").ConfigureAwait(false);
                }
            }
        }
Пример #2
0
        public async Task AddCustReact(string key, [Remainder] string message)
        {
            var channel = Context.Channel as ITextChannel;

            if (string.IsNullOrWhiteSpace(message) || string.IsNullOrWhiteSpace(key))
            {
                return;
            }

            key = key.ToLowerInvariant();

            if ((channel == null && !_creds.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
            {
                await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);

                return;
            }

            var cr = new CustomReaction()
            {
                GuildId  = channel?.Guild.Id,
                IsRegex  = false,
                Trigger  = key,
                Response = message,
            };

            using (var uow = _db.UnitOfWork)
            {
                uow.CustomReactions.Add(cr);

                await uow.CompleteAsync().ConfigureAwait(false);
            }

            if (channel == null)
            {
                Array.Resize(ref _service.GlobalReactions, _service.GlobalReactions.Length + 1);
                _service.GlobalReactions[_service.GlobalReactions.Length - 1] = cr;
            }
            else
            {
                _service.GuildReactions.AddOrUpdate(Context.Guild.Id,
                                                    new CustomReaction[] { cr },
                                                    (k, old) =>
                {
                    Array.Resize(ref old, old.Length + 1);
                    old[old.Length - 1] = cr;
                    return(old);
                });
            }

            await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
                                             .WithTitle(GetText("new_cust_react"))
                                             .WithDescription($"#{cr.Id}")
                                             .AddField(efb => efb.WithName(GetText("trigger")).WithValue(key))
                                             .AddField(efb => efb.WithName(GetText("response")).WithValue(message.Length > 1024 ? GetText("redacted_too_long") : message))
                                             ).ConfigureAwait(false);
        }
        public Task <bool> TryBlockLate(DiscordSocketClient client, IUserMessage message, IGuild guild, IMessageChannel channel, IUser user, string moduleName, string commandName)
        {
            if (guild == null || _creds.IsOwner(user))
            {
                return(Task.FromResult(false));
            }

            var commandCooldowns = CommandCooldowns(guild.Id);
            var commandCooldown  = commandCooldowns.FirstOrDefault(cc => cc.CommandName.Equals(commandName, StringComparison.OrdinalIgnoreCase));

            if (commandCooldown != null)
            {
                var activeCooldownsInGuild = ActiveCooldowns.GetOrAdd(guild.Id, new ConcurrentHashSet <ActiveCooldown>());

                if (activeCooldownsInGuild.Any(ac => ac.UserId == user.Id && ac.Command.Equals(commandName, StringComparison.OrdinalIgnoreCase)))
                {
                    return(Task.FromResult(true));
                }

                activeCooldownsInGuild.Add(new ActiveCooldown()
                {
                    UserId  = user.Id,
                    Command = commandName,
                });
                var _ = Task.Run(async() => {
                    try {
                        await Task.Delay(commandCooldown.Seconds * 1000);
                        activeCooldownsInGuild.RemoveWhere(ac => ac.UserId == user.Id && ac.Command.Equals(commandName, StringComparison.OrdinalIgnoreCase));
                    } catch { }
                });
            }
            return(Task.FromResult(false));
        }
            public async Task WarnEdit(int id, [Remainder] string reason = null)
            {
                var warn = uow.Warnings.Get(id);

                if (_bc.IsOwner(Context.User))
                {
                    if (warn != null)
                    {
                        var oldReason = warn.Reason;
                        warn.Reason = reason;

                        uow.Warnings.Update(warn);
                        await uow.SaveChangesAsync(false);
                        await ConfirmLocalized("userpunish_warnedit_warn_edit", id, (await Context.Guild.GetUserAsync(warn.UserId)).ToString(), string.IsNullOrWhiteSpace(oldReason)? "null" : oldReason, string.IsNullOrWhiteSpace(reason)? "null" : reason).ConfigureAwait(false);
                    }
                    else
                    {
                        await ErrorLocalized("userpunish_warnedit_warn_id_not_found", id).ConfigureAwait(false);
                    }
                }
                else if (Context.User is IGuildUser user && warn.GuildId != user.GuildId)
                {
                    await ErrorLocalized("userpunish_warnedit_warn_edit_perms", id).ConfigureAwait(false);
                }
            }
Пример #5
0
        public async Task TurnToXp(long moneyToSpend, [Remainder] IUser user = null)
        {
            user = user != null && _creds.IsOwner(Context.User) ? user : Context.User;

            if (moneyToSpend >= 0)
            {
                if (moneyToSpend > 0)
                {
                    if (uow.Currency.TryAddCurrencyValue(Context.Guild.Id, user.Id, -moneyToSpend))
                    {
                        var xp = (int)(moneyToSpend * uow.GuildConfigs.For(Context.Guild.Id).TurnToXpMultiplier);
                        uow.LevelModel.AddXP(Context.Guild.Id, user.Id, xp, Context.Channel.Id);
                        await uow.SaveChangesAsync(false).ConfigureAwait(false);

                        await ReplyConfirmLocalized(user == Context.User? "ttxp_turned_self" : "ttxp_turned_other", moneyToSpend, CurrencySign, xp, user.ToString()).ConfigureAwait(false);
                    }
                    else
                    {
                        await ReplyErrorLocalized(user == Context.User? "ttxp_error_no_money_self" : "ttxp_error_no_money_other", user.ToString()).ConfigureAwait(false);
                    }
                }
                else
                {
                    await ReplyErrorLocalized("ttxp_error_zero_value", CurrencySign).ConfigureAwait(false);
                }
            }
            else
            {
                await ReplyErrorLocalized("ttxp_error_negative_value").ConfigureAwait(false);
            }
        }
Пример #6
0
        public async Task DeletePlaylist([Remainder] int id)
        {
            var success = false;

            try
            {
                using (var uow = _db.UnitOfWork)
                {
                    var pl = uow.MusicPlaylists.GetById(id);

                    if (pl != null)
                    {
                        if (_creds.IsOwner(Context.User) || pl.AuthorId == Context.User.Id)
                        {
                            uow.MusicPlaylists.Remove(pl);
                            await uow.CompleteAsync().ConfigureAwait(false);

                            success = true;
                        }
                    }
                }

                if (!success)
                {
                    await ReplyErrorLocalizedAsync("playlist_delete_fail").ConfigureAwait(false);
                }
                else
                {
                    await ReplyConfirmLocalizedAsync("playlist_deleted").ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                _log.Warn(ex);
            }
        }
Пример #7
0
        public async Task BirthdaySet(IBirthDate bd)
        {
            var bdm = uow.BirthDates.GetUserBirthDate(Context.User.Id);

            if (_botCreds.IsOwner(Context.User) || (bdm?.Year) == null && (bdm == null || bdm.Year != null || bdm.Day == bd.Day && bdm.Month == bd.Month))
            {
                uow.BirthDates.SetBirthDate(Context.User.Id, bd);
                await uow.SaveChangesAsync(false).ConfigureAwait(false);

                await ReplyConfirmLocalized("birthdayset_set", bd.ToString()).ConfigureAwait(false);

                if (bd.IsBirthday(DateTime.Now) && (bdm == null || !bdm.IsBirthday(DateTime.Now)))
                {
                    var gc = uow.GuildConfigs.For(Context.Guild.Id);

                    if (gc.BirthdaysEnabled)
                    {
                        var bdmChId = gc.BirthdayMessageChannelId;

                        if (bdmChId != null)
                        {
                            var bdmCh = await Context.Guild.GetTextChannelAsync(bdmChId.Value).ConfigureAwait(false);

                            if (bdmCh != null)
                            {
                                await bdmCh.SendMessageAsync(string.Format(gc.BirthdayMessage, Context.User.Mention)).ConfigureAwait(false);
                            }
                        }

                        var roleId = gc.BirthdayRoleId;

                        if (roleId != null)
                        {
                            var role = Context.Guild.GetRole(roleId.Value);

                            if (role != null)
                            {
                                await((SocketGuildUser)Context.User).AddRoleAsync(role).ConfigureAwait(false);
                            }
                        }
                    }
                }
            }
            else
            {
                await ReplyErrorLocalized("birthdayset_set_before").ConfigureAwait(false);
            }
        }
Пример #8
0
        public async Task TurnToXp(long moneyToSpend, [Remainder] IUser user = null)
        {
            user = user != null && _creds.IsOwner(Context.User) ? user : Context.User;
            if (moneyToSpend < 0)
            {
                await ReplyErrorLocalized("ttxp_error_negative_value").ConfigureAwait(false);

                return;
            }

            if (moneyToSpend == 0)
            {
                await ReplyErrorLocalized("ttxp_error_zero_value", CurrencySign).ConfigureAwait(false);

                return;
            }

            using (var uow = _db.UnitOfWork)
            {
                if (!uow.Currency.TryUpdateState(user.Id, -moneyToSpend))
                {
                    if (user == Context.User)
                    {
                        await ReplyErrorLocalized("ttxp_error_no_money_self").ConfigureAwait(false);
                    }
                    else
                    {
                        await ReplyErrorLocalized("ttxp_error_no_money_other", user.ToString()).ConfigureAwait(false);
                    }
                    return;
                }

                var xp = (int)(moneyToSpend * uow.GuildConfigs.For(Context.Guild.Id, set => set).TurnToXpMultiplier);
                uow.LevelModel.AddXp(Context.Guild.Id, user.Id, xp, Context.Channel.Id);
                if (user == Context.User)
                {
                    await ReplyConfirmLocalized("ttxp_turned_self", moneyToSpend, CurrencySign, xp)
                    .ConfigureAwait(false);
                }
                else
                {
                    await ReplyConfirmLocalized("ttxp_turned_other", user.ToString(), moneyToSpend, CurrencySign, xp)
                    .ConfigureAwait(false);
                }
                await uow.CompleteAsync().ConfigureAwait(false);
            }
        }
Пример #9
0
        public async Task HowToVerify(bool dm = true, bool delete = true)
        {
            delete = !_creds.IsOwner(Context.User) || delete;
            var text = Service.GetVerificationTutorialText(Context.Guild.Id);

            if (string.IsNullOrWhiteSpace(text))
            {
                (await ReplyErrorLocalized("tutorial_not_set").ConfigureAwait(false)).DeleteAfter(60);
                return;
            }
            var ch = dm ? await Context.User.GetOrCreateDMChannelAsync().ConfigureAwait(false) : Context.Channel;

            var msg = await ch.SendConfirmAsync(GetText("tutorial"), text).ConfigureAwait(false);

            if (delete && !dm)
            {
                msg.DeleteAfter(120);
            }
        }
Пример #10
0
            public async Task DeletePlaylist([Leftover] int id)
            {
                var success = false;

                try
                {
                    using (var uow = _db.GetDbContext())
                    {
                        var pl = uow.MusicPlaylists.GetById(id);

                        if (pl != null)
                        {
                            if (_creds.IsOwner(ctx.User) || pl.AuthorId == ctx.User.Id)
                            {
                                uow.MusicPlaylists.Remove(pl);
                                await uow.SaveChangesAsync();

                                success = true;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Warning(ex, "Error deleting playlist");
                }

                if (!success)
                {
                    await ReplyErrorLocalizedAsync("playlist_delete_fail").ConfigureAwait(false);
                }
                else
                {
                    await ReplyConfirmLocalizedAsync("playlist_deleted").ConfigureAwait(false);
                }
            }
Пример #11
0
 private bool AdminInGuildOrOwnerInDm() => (ctx.Guild == null && _creds.IsOwner(ctx.User)) ||
 (ctx.Guild != null && ((IGuildUser)ctx.User).GuildPermissions.Administrator);
Пример #12
0
 private bool AdminInGuildOrOwnerInDm() => (Context.Guild == null && _creds.IsOwner(Context.User)) ||
 (Context.Guild != null && ((IGuildUser)Context.User).GuildPermissions.ManageEmojis);
Пример #13
0
 public Task <bool> TryBlockEarly(IGuild guild, IUserMessage userMessage, bool realExecution = true)
 => Task.FromResult(!_creds.IsOwner(userMessage.Author) && (guild != null && BlacklistedGuilds.Contains(guild.Id) || BlacklistedChannels.Contains(userMessage.Channel.Id) || BlacklistedUsers.Contains(userMessage.Author.Id)));