예제 #1
0
        private static async Task <bool> PermitUserToChannel(IUser user, ITextChannel channel, int numberOfKills = 0)
        {
            var dateTime = DateTime.UtcNow;

            // Add entry to database
            Link link = null;
            var  writtenToDatabase = await Ditto.Database.WriteAsync(uow =>
            {
                link = uow.Links.Add(new Link()
                {
                    Channel = channel,
                    Guild   = channel.Guild,
                    Date    = dateTime,
                    Type    = LinkType.SoloLeveling,
                    Value   = FormatValueToString(user, numberOfKills, 0, dateTime),
                });

                if (link != null)
                {
                    _links.Add(link);
                    return(true);
                }
                return(false);
            });

            if (!writtenToDatabase)
            {
                return(false);
            }

            // Reset to the parent permissions
            await channel.SyncPermissionsAsync().ConfigureAwait(false);

            // Update the channel permissions
            await channel.AddPermissionOverwriteAsync(user, new OverwritePermissions(viewChannel : PermValue.Allow, sendMessages : PermValue.Allow, manageRoles : PermValue.Allow), new RequestOptions()
            {
                RetryMode = RetryMode.AlwaysRetry
            });

            // Add message to the channel pinging the user.
            await channel.SendMessageAsync(
                $"{user.Mention} You now have access to this channel, you are able to edit this channel to grant other users access.\n" +
                $"{channel.Mention} will be monitored for the amount of kills in and the access will be revoked when {(numberOfKills > 0 ? $"you have killed {numberOfKills} monsters" : "the gate is closed")}."
                ).ConfigureAwait(false);

            return(true);
        }