Beispiel #1
0
        private async Task OnMessageReceived(SocketMessage arg)
        {
            if (!(arg is SocketUserMessage message))
            {
                return;
            }
            if (message.Source != MessageSource.User)
            {
                return;
            }

            var argPos     = 0;
            var guild      = message.Channel as SocketGuildChannel;
            var socketUser = message.Author as SocketGuildUser;
            var prefix     = await _servers.GetGuildPrefix(guild.Guild.Id) ?? "//";

            var server = await _context.Servers
                         .FindAsync(guild.Guild.Id);

            if (server == null)
            {
                _context.Add(new Server {
                    ServerId = guild.Guild.Id, Prefix = prefix
                });
            }

            if (!message.HasStringPrefix(prefix, ref argPos) && !message.HasMentionPrefix(_client.CurrentUser, ref argPos))
            {
                var discordUser = await _userProfiles.GetOrCreateUserProfile(guild.Guild.Id, message.Author.Id);

                await _userProfiles.ModifyExpAsync(message.Author.Id, (ulong)rand.Next(1, 10));

                var expToNextLevel = (ulong)(5 * Math.Pow(discordUser.Level, 2) + 50 * discordUser.Level + 100);

                if (discordUser.Exp >= expToNextLevel)
                {
                    await _userProfiles.ModifyLevelAsync(discordUser.UserId, discordUser.Level += 1);

                    discordUser.Exp -= expToNextLevel;

                    var channelId = await _servers.GetLevelUpChannelAsync(guild.Guild.Id);

                    if (channelId == 0)
                    {
                        return;
                    }

                    var channel = guild.Guild.GetTextChannel(channelId);
                    if (channel == null)
                    {
                        await _servers.ClearLevelUpChannelAsync(guild.Guild.Id);

                        return;
                    }

                    var levelUpBackground = await _servers.GetLevelUpAsync(guild.Guild.Id);

                    string path = await _images.CreateLevelUpImageAsync(socketUser, discordUser, levelUpBackground);

                    await channel.SendFileAsync(path, null);

                    System.IO.File.Delete(path);
                }

                return;
            }

            var context = new SocketCommandContext(_client, message);
            await _service.ExecuteAsync(context, argPos, _provider);
        }
Beispiel #2
0
        public async Task LevelUp(string option = null, string value = null)
        {
            if (option == null && value == null)
            {
                var fetchedChannelId = await _servers.GetLevelUpChannelAsync(Context.Guild.Id);

                if (fetchedChannelId == 0)
                {
                    await Context.Channel.SendErrorAsync("Error!", "There has not been set a level up channel yet!");

                    return;
                }

                var fetchedChannel = Context.Guild.GetTextChannel(fetchedChannelId);
                if (fetchedChannel == null)
                {
                    await Context.Channel.SendErrorAsync("Error!", "There has not been set a level up channel yet!");

                    await _servers.ClearLevelUpChannelAsync(Context.Guild.Id);

                    return;
                }

                var fetchedLevelUp = await _servers.GetLevelUpAsync(Context.Guild.Id);

                if (fetchedLevelUp != null)
                {
                    await Context.Channel.SendSuccessAsync("Success!", $"The channel used for the level up message is {fetchedChannel.Mention}.\nThe background is set to {fetchedLevelUp}.");
                }
                else
                {
                    await Context.Channel.SendSuccessAsync("Success!", $"The channel used for the level up message is {fetchedChannel.Mention}.");
                }

                return;
            }

            if (option == "channel" && value != null)
            {
                if (!MentionUtils.TryParseChannel(value, out ulong parsedId))
                {
                    await Context.Channel.SendErrorAsync("Error!", "Please pass in a valid channel!");

                    return;
                }

                var parsedChannel = Context.Guild.GetTextChannel(parsedId);
                if (parsedChannel == null)
                {
                    await Context.Channel.SendErrorAsync("Error!", "Please pass in a valid channel!");

                    return;
                }

                await _servers.ModifyLevelUpChannelAsync(Context.Guild.Id, parsedId);

                await Context.Channel.SendSuccessAsync("Success!", $"Successfully modified the level up channel to {parsedChannel.Mention}.");

                return;
            }

            if (option == "background" && value != null)
            {
                if (value == "clear")
                {
                    await _servers.ClearLevelUpAsync(Context.Guild.Id);

                    await Context.Channel.SendSuccessAsync("Success!", "Successfully cleared the level up background for this server.");
                }

                await _servers.ModifyLevelUpAsync(Context.Guild.Id, value);

                await Context.Channel.SendSuccessAsync("Success!", $"Successfully modified the level up background to {value}.");

                return;
            }

            if (option == "clear" && value == null)
            {
                await _servers.ClearLevelUpChannelAsync(Context.Guild.Id);

                await Context.Channel.SendSuccessAsync("Success!", "Successfully cleared the level up channel.");

                return;
            }

            await Context.Channel.SendErrorAsync("Error!", "You did not use this command properly.");
        }