Exemple #1
0
        private async Task H(CommandInfo command)
        {
            string prefix = Context.Channel is IDMChannel ? Roki.Properties.Prefix : (await _config.GetGuildConfigAsync(Context.Guild.Id)).Prefix;

            if (command == null)
            {
                EmbedBuilder helpEmbed = new EmbedBuilder().WithOkColor()
                                         .WithTitle("Roki Help")
                                         .WithDescription(string.Format(@"Simple guide to find a command:
Use `{0}modules` command to see a list of all modules.
Then use `{0}commands <module>` to see a list of all the commands in that module (e.g. `{0}commands searches`).
After seeing the commands available in that module, you can use `{0}h <command>` to get help for a specific command (e.g. `{0}h weather`).", prefix));

                if (Context.Channel is IDMChannel)
                {
                    await Context.Channel.EmbedAsync(helpEmbed).ConfigureAwait(false);
                }
                else
                {
                    try
                    {
                        IDMChannel dm = await((IGuildUser)Context.User).GetOrCreateDMChannelAsync().ConfigureAwait(false);
                        await dm.EmbedAsync(helpEmbed).ConfigureAwait(false);
                    }
                    catch (Exception)
                    {
                        await Context.Channel.EmbedAsync(helpEmbed).ConfigureAwait(false);
                    }
                }
            }
            else
            {
                await HelpService.SendCommandInfo(command, Context, prefix);
            }
        }
Exemple #2
0
        private static async Task SendNotification(UserData data, SocketMessage msg, int level)
        {
            switch (data.NotificationLocation)
            {
            case 0:
                return;

            case 1:
                await msg.Channel.EmbedAsync(new EmbedBuilder().WithDynamicColor(data.GuildId).WithDescription($"Congratulations {msg.Author.Mention}! You've reached Level {level}"));

                break;

            case 2:
                IDMChannel dm = await msg.Author.GetOrCreateDMChannelAsync().ConfigureAwait(false);

                try
                {
                    await dm.EmbedAsync(new EmbedBuilder().WithDynamicColor(data.GuildId).WithDescription($"Congratulations {msg.Author.Mention}! You've reached Level {level}"));
                }
                catch
                {
                    // user has disabled dms
                }
                break;
            }
        }
        private async Task <bool> GreetDmUser(GreetSettings conf, IDMChannel channel, IGuildUser user)
        {
            var rep = new ReplacementBuilder()
                      .WithDefault(user, channel, (SocketGuild)user.Guild, _client)
                      .Build();

            if (CREmbed.TryParse(conf.DmGreetMessageText, out var embedData))
            {
                rep.Replace(embedData);
                try
                {
                    await channel.EmbedAsync(embedData).ConfigureAwait(false);
                }
                catch
                {
                    return(false);
                }
            }
            else
            {
                var msg = rep.Replace(conf.DmGreetMessageText);
                if (!string.IsNullOrWhiteSpace(msg))
                {
                    try
                    {
                        await channel.SendConfirmAsync(msg).ConfigureAwait(false);
                    }
                    catch
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemple #4
0
        private async Task UpdateXp(SocketMessage message, IGuildChannel channel)
        {
            if (!await _config.XpGainEnabled(channel.Id))
            {
                return;
            }

            using IServiceScope scope = _scopeFactory.CreateScope();
            var context = scope.ServiceProvider.GetRequiredService <RokiContext>();

            GuildConfig guildConfig = await _config.GetGuildConfigAsync(channel.GuildId);

            UserData data = await context.UserData.AsQueryable().Where(x => x.UserId == message.Author.Id && x.GuildId == channel.GuildId).SingleOrDefaultAsync();

            if (data == null)
            {
                if (await context.Users.AsNoTracking().AnyAsync(x => x.Id == message.Author.Id))
                {
                    data = new UserData(message.Author.Id, channel.GuildId);
                }
                else
                {
                    data = new UserData(message.Author.Id, channel.GuildId)
                    {
                        User = new User(message.Author.Username, message.Author.Discriminator, message.Author.AvatarId)
                    };
                }

                await context.UserData.AddAsync(data);
            }

            // temp
            // these are always false
            bool doubleXp = data.Xp < 0;
            bool fastXp   = data.Xp < 0;

            var oldXp = new XpLevel(data.Xp);

            DateTime now = DateTime.UtcNow;

            if (fastXp && now - data.LastXpGain >= TimeSpan.FromSeconds(guildConfig.XpFastCooldown))
            {
                data.Xp += doubleXp ? guildConfig.XpPerMessage * 2 : guildConfig.XpPerMessage;
            }
            else if (DateTime.UtcNow - data.LastXpGain >= TimeSpan.FromSeconds(guildConfig.XpCooldown))
            {
                data.Xp += doubleXp ? guildConfig.XpPerMessage * 2 : guildConfig.XpPerMessage;
            }
            else
            {
                return;
            }

            data.LastXpGain = now;
            var newXp = new XpLevel(data.Xp);

            if (oldXp.Level == newXp.Level)
            {
                await context.SaveChangesAsync();

                return;
            }

            data.LastLevelUp = now;

            await SendNotification(data, message, newXp.Level).ConfigureAwait(false);

            List <XpReward> rewards = await context.XpRewards.AsNoTracking().Where(x => x.GuildId == channel.GuildId && x.Level == newXp.Level).ToListAsync();

            if (rewards.Count > 0)
            {
                var sb = new StringBuilder();
                foreach (XpReward reward in rewards)
                {
                    string[] parsed = reward.Details.Split(';');
                    if (parsed.Length < 2)
                    {
                        continue;
                    }

                    switch (parsed[0])
                    {
                    case "currency":
                    {
                        long amount = long.Parse(parsed[1]);
                        await _currency.AddCurrencyAsync(data.UserId, data.GuildId, channel.Id, message.Id, $"#{reward.Id} XP Level {reward.Level} Reward", amount);

                        sb.AppendLine($"+ `{amount:N0}` {guildConfig.CurrencyIcon}");
                        break;
                    }

                    case "role":
                    {
                        SocketRole role = (channel.Guild as SocketGuild)?.GetRole(ulong.Parse(parsed[1]));
                        if (role == null)
                        {
                            continue;
                        }
                        var guildUser = (IGuildUser)message.Author;
                        await guildUser.AddRoleAsync(role).ConfigureAwait(false);

                        sb.AppendLine(reward.Description);
                        break;
                    }
                    }
                }

                IDMChannel dm = await message.Author.GetOrCreateDMChannelAsync().ConfigureAwait(false);

                try
                {
                    await dm.EmbedAsync(new EmbedBuilder().WithOkColor()
                                        .WithTitle($"Level `{newXp.Level}` Rewards - {channel.Guild.Name}")
                                        .WithDescription("Here are your rewards:\n" + sb))
                    .ConfigureAwait(false);

                    await dm.CloseAsync().ConfigureAwait(false);
                }
                catch
                {
                    // unable to send dm to user
                    // ignored
                }
            }

            await context.SaveChangesAsync();
        }